Skip to content

Commit

Permalink
allow package_search to return id/name only instead of full dataset d…
Browse files Browse the repository at this point in the history
…ictionary
  • Loading branch information
fanjinfei committed Feb 2, 2017
1 parent 56ab0a6 commit 28234dc
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions ckan/logic/action/get.py
Expand Up @@ -1833,6 +1833,8 @@ def package_search(context, data_dict):
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.
'''
# sometimes context['schema'] is None
schema = (context.get('schema') or
Expand Down Expand Up @@ -1875,8 +1877,10 @@ 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)
if data_dict.get('fl') not in ('id', 'name'):
data_dict['fl'] = 'id {0}'.format(data_source)

# Remove before these hit solr FIXME: whitelist instead
include_private = asbool(data_dict.pop('include_private', False))
Expand All @@ -1903,21 +1907,25 @@ 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 data_dict.get('fl') in ['id', 'name']:
for package in query.results:
results.append( {data_dict.get('fl'):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

0 comments on commit 28234dc

Please sign in to comment.