Skip to content

Commit

Permalink
Merge pull request #3427 from fanjinfei/master
Browse files Browse the repository at this point in the history
allow package_search to return id/name instead of full dataset dictionary
  • Loading branch information
amercader committed Mar 14, 2017
2 parents 5739eb8 + dc94a5e commit 3efd082
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
50 changes: 31 additions & 19 deletions ckan/logic/action/get.py
Expand Up @@ -1831,8 +1831,9 @@ def package_search(context, data_dict):
fl
The parameter that controls which fields are returned in the solr
query cannot be changed. CKAN always returns the matched datasets as
dictionary objects.
query.
fl can be None or a list of result fields, such as ['id', 'extras_custom_field'].
if fl = None, datasets are returned as a list of full dictionary.
'''
# sometimes context['schema'] is None
schema = (context.get('schema') or
Expand Down Expand Up @@ -1875,8 +1876,12 @@ def package_search(context, data_dict):
else:
data_source = 'validated_data_dict'
data_dict.pop('use_default_schema', None)
# return a list of package ids
data_dict['fl'] = 'id {0}'.format(data_source)

result_fl = data_dict.get('fl')
if not result_fl:
data_dict['fl'] = 'id {0}'.format(data_source)
else:
data_dict['fl'] = ' '.join(result_fl)

# Remove before these hit solr FIXME: whitelist instead
include_private = asbool(data_dict.pop('include_private', False))
Expand All @@ -1903,21 +1908,28 @@ def package_search(context, data_dict):
# Add them back so extensions can use them on after_search
data_dict['extras'] = extras

for package in query.results:
# get the package object
package_dict = package.get(data_source)
## use data in search index if there
if package_dict:
# the package_dict still needs translating when being viewed
package_dict = json.loads(package_dict)
if context.get('for_view'):
for item in plugins.PluginImplementations(
plugins.IPackageController):
package_dict = item.before_view(package_dict)
results.append(package_dict)
else:
log.error('No package_dict is coming from solr for package '
'id %s', package['id'])
if result_fl:
for package in query.results:
if package.get('extras'):
package.update(package['extras'] )
package.pop('extras')
results.append(package)
else:
for package in query.results:
# get the package object
package_dict = package.get(data_source)
## use data in search index if there
if package_dict:
# the package_dict still needs translating when being viewed
package_dict = json.loads(package_dict)
if context.get('for_view'):
for item in plugins.PluginImplementations(
plugins.IPackageController):
package_dict = item.before_view(package_dict)
results.append(package_dict)
else:
log.error('No package_dict is coming from solr for package '
'id %s', package['id'])

count = query.count
facets = query.facets
Expand Down
1 change: 1 addition & 0 deletions ckan/logic/schema.py
Expand Up @@ -587,6 +587,7 @@ def default_autocomplete_schema():
def default_package_search_schema():
schema = {
'q': [ignore_missing, unicode],
'fl': [ignore_missing, list_of_strings],
'fq': [ignore_missing, unicode],
'rows': [ignore_missing, natural_number_validator],
'sort': [ignore_missing, unicode],
Expand Down
8 changes: 8 additions & 0 deletions ckan/tests/logic/action/test_get.py
Expand Up @@ -852,6 +852,14 @@ def test_search(self):
eq(search_result['results'][0]['title'], 'Rivers')
eq(search_result['count'], 1)

def test_search_fl(self):
factories.Dataset(title='Rivers', name='test_ri')
factories.Dataset(title='Lakes')

search_result = helpers.call_action('package_search', q='rivers', fl=['title', 'name'])

eq(search_result['results'], [{'title': 'Rivers', 'name': 'test_ri'}])

def test_search_all(self):
factories.Dataset(title='Rivers')
factories.Dataset(title='Lakes')
Expand Down

0 comments on commit 3efd082

Please sign in to comment.