Skip to content

Commit d754a29

Browse files
committed
Add option to use collapse & expand query parser for solr grouping
1 parent dadd79b commit d754a29

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

utils/search/backends/solr555pysolr.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -676,21 +676,34 @@ def search_sounds(self, textual_query='', query_fields=None, query_filter='', fi
676676

677677
# Configure grouping
678678
if group_by_pack:
679-
query.set_group_field(group_field="pack_grouping" if not similar_to else "pack_grouping_child") # We name the fields differently to avoid solr conflicts with matches of both child and parent docs
680-
query.set_group_options(
681-
group_func=None,
682-
group_query=None,
683-
group_rows=10, # TODO: if limit is lower than rows and start=0, this should probably be equal to limit
684-
group_start=0,
685-
group_limit=num_sounds_per_pack_group, # This is the number of documents that will be returned for each group.
686-
group_offset=0,
687-
group_sort=None,
688-
group_sort_ingroup=None,
689-
group_format='grouped',
690-
group_main=False,
691-
group_num_groups=True,
692-
group_cache_percent=0,
693-
group_truncate=group_counts_as_one_in_facets)
679+
use_collapse_expand_query_parser = True
680+
if use_collapse_expand_query_parser:
681+
current_filter = query.params.get('fq', '')
682+
field_name = "pack_grouping" if not similar_to else "pack_grouping_child"
683+
group_by_pack_filter = f'{{!collapse field={field_name}}}'
684+
if current_filter:
685+
query.params['fq'] = [current_filter, group_by_pack_filter]
686+
else:
687+
query.params['fq'] = [group_by_pack_filter]
688+
query.params['fl'] = query.params['fl'] + f',{field_name}'
689+
query.params['expand'] = True
690+
query.params['expand.rows'] = 0
691+
else:
692+
query.set_group_field(group_field="pack_grouping" if not similar_to else "pack_grouping_child")
693+
query.set_group_options(
694+
group_func=None,
695+
group_query=None,
696+
group_rows=10, # TODO: if limit is lower than rows and start=0, this should probably be equal to limit
697+
group_start=0,
698+
group_limit=num_sounds_per_pack_group, # This is the number of documents that will be returned for each group.
699+
group_offset=0,
700+
group_sort=None,
701+
group_sort_ingroup=None,
702+
group_format='grouped',
703+
group_main=False,
704+
group_num_groups=True,
705+
group_cache_percent=0,
706+
group_truncate=group_counts_as_one_in_facets)
694707

695708
# Do the query!
696709
# Note: we create a SearchResults with the same members as SolrResponseInterpreter (the response from .search()).

utils/search/backends/solr_common.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ def make_solr_query_url(solr_query_params, debug=False):
294294

295295
class SolrResponseInterpreter:
296296
def __init__(self, response, next_page_query=None):
297+
if "grouped" in response and "expanded" in response:
298+
raise SearchEngineException("Response contains both grouped and expanded results, this is not supported")
299+
297300
if "grouped" in response:
298301
grouping_field = list(response["grouped"].keys())[0]
299302
self.docs = [{
@@ -307,6 +310,19 @@ def __init__(self, response, next_page_query=None):
307310
self.num_rows = len(self.docs)
308311
self.num_found = response["grouped"][grouping_field]["ngroups"]
309312
self.non_grouped_number_of_results = response["grouped"][grouping_field]["matches"]
313+
elif "expanded" in response:
314+
collapse_field = 'pack_grouping_child' if 'pack_grouping_child' in response['responseHeader']['params']['fl'] else 'pack_grouping'
315+
self.docs = [{
316+
'id': doc['id'],
317+
'score': doc['score'],
318+
'n_more_in_group': response['expanded'][doc[collapse_field]]['numFound'] if doc[collapse_field] in response['expanded'] else 0,
319+
'group_docs': doc,
320+
'group_name': doc[collapse_field]
321+
} for doc in response["response"]["docs"]]
322+
self.start = int(response["response"]["start"])
323+
self.num_rows = len(self.docs)
324+
self.num_found = response["response"]["numFound"] # Note that using "Collapse and Expand" for grouping we can't get the total number of uncollapsed matches
325+
self.non_grouped_number_of_results = -1
310326
else:
311327
self.docs = response["response"]["docs"]
312328
self.start = int(response["response"]["start"])

0 commit comments

Comments
 (0)