pysolr
pysolr copied to clipboard
Unable to retrieve results using pysolr.search() with grouping
I have
- [x] Tested with the latest release
- [ ] Tested with the current master branch
- [x] Searched for similar existing issues
Expected behavior
I expect to retrieve search results grouped by the specified field (sno) based on the provided query. The results should be similar to what I see in the Solr UI.
Actual behavior
No search results are returned. The results variable is empty.
Steps to reproduce the behavior
- Use pysolr library to connect to Solr instance.
- Execute the following search query with grouping in Python:
results = solr.search(query, **{"group": "true", "group_field": "sno", "group_offset": 0,
"group_limit": 1, "group_sort": "date desc", "group_ngroups": "true", "rows": 1000})
Alternatively, I tried the query below which gives me:
_(HTTP 400): [Reason: invalid boolean value: True]_-
results = solr.search(query, **{_"group":True_, "group_field": "sno", "group_offset": 0,
"group_limit": 1, "group_sort": "date desc", _"group_ngroups": True,_ "rows": 1000})
Configuration
- Operating system version: Windows 10
- Search engine version:
- Python version: 3.8
- pysolr version: 3.9.0
I've looked into this. There are two problems here: First pysolr converted python boolean true to True with a capital 'T', which solr won't take. Second issue is the grouping field parameter is called group.field not group_field. You can do a search here https://archive.apache.org/dist/lucene/solr/ref-guide/apache-solr-ref-guide-4.9.pdf
Unfortunately the code seems a bit tangled for me but it is not too hard to fix.
Can someone contribute a working example for a perceived problem? pysolr doesn't do type-conversion at all: the search method passes the values through to safe_urlencode which on Python 3 calls the stdlib urlencode function.
That means that your values like "true" are not going to be modified but you do need to convert things which are not strings or numbers:
>>> pysolr.safe_urlencode({"group": "true", "group2": True, "group.field": "test_id"})
'group=true&group2=True&group.field=test_id'
>>> solr.search("title_full:*", **{"group": "true", "group2": True, "group.field": "test_id"})
<pysolr.Results object at 0x1076fcd90>
# Solr log: path=/select params={q=title_full:*&wt=json&group2=True&group.field=test_id&group=true}
pysolr tends to stay out of the type conversion business because it would require knowledge of your Solr instance and its configuration to know which fields should be converted and which should be left alone, and that knowledge lives at the application level.