Pārlūkot izejas kodu

[search] Support excluding values in nested facet counts

Romain Rigaux 10 gadi atpakaļ
vecāks
revīzija
0445bacb67

+ 18 - 12
apps/search/src/search/models.py

@@ -576,7 +576,13 @@ class Collection2(object):
 
 
 def get_facet_field(category, field, facets):
-  facets = filter(lambda facet: facet['type'] == category and '%(field)s-%(id)s' % facet == field, facets)
+  if category in ('nested', 'function'):
+    id_pattern = '%(id)s'
+  else:
+    id_pattern = '%(field)s-%(id)s'
+
+  facets = filter(lambda facet: facet['type'] == category and id_pattern % facet == field, facets)
+
   if facets:
     return facets[0]
   else:
@@ -712,7 +718,7 @@ def augment_solr_response(response, collection, query):
   if response and response.get('facets'):
     for facet in collection['facets']:
       category = facet['type']
-      name = NAME % facet
+      name = facet['id'] # Nested facets can only have one name
 
       if category == 'function' and name in response['facets']:
         value = response['facets'][name]
@@ -827,7 +833,7 @@ def _augment_stats_2d(name, facet, counts, selected_values):
   fq_fields = []
   fq_values = []
   fq_filter = []
-  _selected_values = []
+  _selected_values = [f['value'] for f in selected_values.get(facet['id'], [])]
   _fields = [facet['field']] + [facet['field'] for facet in facet['properties']['facets']]
 
   return __augment_stats_2d(counts, facet['field'], fq_fields, fq_values, fq_filter, _selected_values, _fields)
@@ -855,15 +861,15 @@ def __augment_stats_2d(counts, label, fq_fields, fq_values, fq_filter, _selected
 
 
 def _get_augmented(count, val, label, fq_values, fq_fields, fq_filter, _selected_values):
-    return {
-        "count": count,
-        "value": val,
-        "cat": label,
-        'selected': fq_values in _selected_values,
-        'exclude': all([f['exclude'] for f in fq_filter if f['value'] == val]),
-        'fq_fields': fq_fields,
-        'fq_values': fq_values,
-    }
+  return {
+      "count": count,
+      "value": val,
+      "cat": label,
+      'selected': fq_values in _selected_values,
+      'exclude': all([f['exclude'] for f in fq_filter if f['value'] == val]),
+      'fq_fields': fq_fields,
+      'fq_values': fq_values,
+  }
 
 
 def _augment_pivot_nd(facet_id, counts, selected_values, fields='', values=''):

+ 3 - 8
desktop/libs/libsolr/src/libsolr/api.py

@@ -171,11 +171,6 @@ class SolrApi(object):
               ('facet.field', '{!key=%(key)s ex=%(id)s f.%(field)s.facet.limit=%(limit)s f.%(field)s.facet.mincount=%(mincount)s}%(field)s' % keys),
           )
         elif facet['type'] == 'nested':
-          props = {
-              'key': '%(field)s-%(id)s' % facet
-          }
-          props.update(facet)
-
           _f = {
               'field': facet['field'],
               'limit': int(facet['properties'].get('limit', 10)) + (1 if facet['widgetType'] == 'facet-widget' else 0),
@@ -193,6 +188,7 @@ class SolrApi(object):
             _f.update({
                 'type': 'terms',
                 'field': facet['field'],
+                'excludeTags': facet['id']
             })
 
           if facet['properties']['facets']:
@@ -212,10 +208,9 @@ class SolrApi(object):
                   'd2': self._get_aggregate_function(facet['properties']['facets'][0])
               }
 
-          json_facets['%(key)s' % props] = _f
+          json_facets[facet['id']] = _f
         elif facet['type'] == 'function':
-          key = '%(field)s-%(id)s' % facet
-          json_facets[key] = self._get_aggregate_function(facet)
+          json_facets[facet['id']] = self._get_aggregate_function(facet)
         elif facet['type'] == 'pivot':
           if facet['properties']['facets'] or facet['widgetType'] == 'map-widget':
             fields = facet['field']