Skip to content

Commit

Permalink
allow package_search to return specified fields
Browse files Browse the repository at this point in the history
  • Loading branch information
fanjinfei committed Feb 7, 2017
1 parent 28234dc commit 895b7ed
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions ckan/logic/action/get.py
Expand Up @@ -1831,10 +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.
if fl='id' or 'name', only this field in the dataset dictionary will
be returned.
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 @@ -1878,9 +1877,11 @@ def package_search(context, data_dict):
data_source = 'validated_data_dict'
data_dict.pop('use_default_schema', None)

# return a list of package ids
if data_dict.get('fl') not in ('id', 'name'):
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 @@ -1907,9 +1908,12 @@ def package_search(context, data_dict):
# Add them back so extensions can use them on after_search
data_dict['extras'] = extras

if data_dict.get('fl') in ['id', 'name']:
if result_fl:
for package in query.results:
results.append( {data_dict.get('fl'):package} )
if package.get('extras'):
package.update(package['extras'] )
package.pop('extras')
results.append(package)
else:
for package in query.results:
# get the package object
Expand Down

0 comments on commit 895b7ed

Please sign in to comment.