Skip to content

Commit

Permalink
Merge pull request #29 from deniszgonjanin/canada-v2.0
Browse files Browse the repository at this point in the history
expose solr range facets in a basic way
  • Loading branch information
deniszgonjanin committed Jul 3, 2013
2 parents 5ef0435 + c51cfea commit 178d290
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion ckan/controllers/package.py
Expand Up @@ -198,7 +198,12 @@ def pager_url(q=None, page=None):
and len(value) and not param.startswith('_'):
if not param.startswith('ext_'):
fields.append((param, value))
fq += ' %s:"%s"' % (param, value)

#if value starts with [, assume range facet filter query
if value.startswith("["):
fq += ' %s:%s' % (param, value)
else:
fq += ' %s:"%s"' % (param, value)
fields_grouped.setdefault(param, []).append(value)
else:
search_extras[param] = value
Expand Down Expand Up @@ -261,6 +266,7 @@ def pager_url(q=None, page=None):
)
c.facets = query['facets']
c.search_facets = query['search_facets']
c.facet_ranges = query['facet_ranges']
c.page.items = query['results']
except SearchError, se:
log.error('Dataset search error: %r', se.args)
Expand Down
9 changes: 6 additions & 3 deletions ckan/lib/search/query.py
Expand Up @@ -15,7 +15,8 @@
VALID_SOLR_PARAMETERS = set([
'q', 'fl', 'fq', 'rows', 'sort', 'start', 'wt', 'qf', 'bf', 'boost',
'facet', 'facet.mincount', 'facet.limit', 'facet.field',
'extras', 'fq_list', 'tie', 'defType', 'mm'
'extras', 'fq_list', 'tie', 'defType', 'mm',
'facet.range.end', 'facet.range', 'facet.range.gap', 'facet.range.start'
])

# for (solr) package searches, this specifies the fields that are searched
Expand Down Expand Up @@ -349,8 +350,7 @@ def run(self, query):
# http://wiki.apache.org/solr/DisMaxQParserPlugin#mm_.28Minimum_.27Should.27_Match.29
query['mm'] = query.get('mm', '2<-1 5<80%')
query['qf'] = query.get('qf', QUERY_FIELDS)



conn = make_connection()
log.debug('Package query: %r' % query)
try:
Expand Down Expand Up @@ -385,6 +385,9 @@ def run(self, query):
self.facets = data.get('facet_counts', {}).get('facet_fields', {})
for field, values in self.facets.iteritems():
self.facets[field] = dict(zip(values[0::2], values[1::2]))

self.facet_ranges = data.get('facet_counts', {}).get('facet_ranges', {})

except Exception, e:
log.exception(e)
raise SearchError(e)
Expand Down
2 changes: 2 additions & 0 deletions ckan/logic/action/get.py
Expand Up @@ -1303,6 +1303,7 @@ def package_search(context, data_dict):

count = query.count
facets = query.facets
facet_ranges = query.facet_ranges
else:
count = 0
facets = {}
Expand All @@ -1311,6 +1312,7 @@ def package_search(context, data_dict):
search_results = {
'count': count,
'facets': facets,
'facet_ranges': facet_ranges,
'results': results,
'sort': data_dict['sort']
}
Expand Down

0 comments on commit 178d290

Please sign in to comment.