0

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
Rohan Garg
  • 35
  • 5
  • You're not requesting any facets - you'll need at least one `facet.field` as well. – MatsLindh Jun 06 '18 at 12:23
  • adding { 'facet' : 'to' } still does not change anything – Rohan Garg Jun 06 '18 at 12:27
  • I'm not sure what you're trying to say - you need to request at least one `facet.field`. Set `'facet': 'true', 'facet.field': ''`. – MatsLindh Jun 06 '18 at 12:30
  • It is still giving the same output as before, adding 'facet.field' is not showing the facets in the search. even in the original code, it should return an empty dictionary. – Rohan Garg Jun 06 '18 at 12:37

1 Answers1

2

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': {}}
MatsLindh
  • 49,529
  • 4
  • 53
  • 84