Skip to content

Commit

Permalink
[xs, search] #1455 bug fix for extras not showing
Browse files Browse the repository at this point in the history
up when searching with all_fields=1.

Schema updated so that extra values will be 
returned from Solr, and added to package_dict['extras']
in the search results.
  • Loading branch information
johnglover committed Nov 28, 2011
1 parent c23821b commit 05b675a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ckan/config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@
<field name="linked_from" type="text" indexed="true" stored="false" multiValued="true"/>
<field name="child_of" type="text" indexed="true" stored="false" multiValued="true"/>
<field name="parent_of" type="text" indexed="true" stored="false" multiValued="true"/>
<field name="extras_*" type="text" indexed="true" stored="false" multiValued="true"/>

<field name="indexed_ts" type="date" indexed="true" stored="true" default="NOW" multiValued="false"/>

<dynamicField name="extras_*" type="text" indexed="true" stored="true" multiValued="false"/>
<dynamicField name="*" type="string" indexed="true" stored="false"/>
</fields>

Expand Down
10 changes: 10 additions & 0 deletions ckan/lib/search/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ def run(self, query):
self.count = response.get('numFound', 0)
self.results = response.get('docs', [])

# get any extras and add to 'extras' dict
for result in self.results:
extra_keys = filter(lambda x: x.startswith('extras_'), result.keys())
extras = {}
for extra_key in extra_keys:
value = result.pop(extra_key)
extras[extra_key[len('extras_'):]] = value
if extra_keys:
result['extras'] = extras

# if just fetching the id or name, return a list instead of a dict
if query.get('fl') in ['id', 'name']:
self.results = [r.get(query.get('fl')) for r in self.results]
Expand Down
19 changes: 9 additions & 10 deletions ckan/tests/lib/test_solr_package_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,15 @@ def test_0_basic(self):
self._do_search(u'bcd', 'b', 1)
self._do_search(u'"cde abc"', 'c', 1)

def test_1_partial_matches(self):
# TODO: solr is not currently set up to allow partial matches
# and extras are not saved as multivalued so these
# tests will fail. Make multivalued or remove these?
from ckan.tests import SkipTest
raise SkipTest

self._do_search(u'abc', ['a', 'c'], 2)
self._do_search(u'cde', 'c', 1)
self._do_search(u'abc cde', 'c', 1)
def test_1_extras_in_all_fields(self):
response = search.query_for(model.Package).run({'q': 'abc', 'fl': '*'})
assert response['count'] == 2

results = response['results']
for result in results:
assert 'extras' in result.keys(), result
assert 'department' in result['extras'], result['extras']
assert result['extras']['department'] in ['abc', 'cde abc'], result['extras']['department']

class TestRank(TestController):
@classmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pylons import config
from ckan import plugins, model
from ckan import model
import ckan.lib.search as search
from ckan.tests import CreateTestData, setup_test_search_index
from test_solr_package_search import TestSearchOverall
Expand Down

0 comments on commit 05b675a

Please sign in to comment.