How to do facet search with Pysolr

1.9k Views Asked by At

when we use curl or urlopen with facet to execute queries, we get a nested dictionary with 3 elements 1. responseHeader 2. response 3. facet_counts

I want to show the facet_counts while using Pysolr search. It just shows the 'response' value of the query output. I'm trying the following code, please help.

import pysolr
conn = pysolr.Solr('http://localhost:8983/solr/')
result = conn.search('enron', **{
    'fl' : 'body',
    'facet' : 'on'
})   
for r in result:
    print r
1

There are 1 best solutions below

0
On BEST ANSWER

When you're iterating over the result variable, you're iterating over pysolr's own Results object (and not directly over the JSON structure as shown by Solr).

import pysolr
import pprint

conn = pysolr.Solr('http://localhost:8080/solr/corename')
result = conn.search('*:*', **{
    'fl': 'content',
    'facet': 'true',
    'facet.field': 'field_name'
})   

pprint.pprint(result.facets)

Any facets will be present under the facets property of this results object.

The example above outputs:

{'facet_dates': {},
 'facet_fields': {'field_name': ['value', 54439, 'value2', 21179]},
 'facet_intervals': {},
 'facet_queries': {},
 'facet_ranges': {}}