Explorar o código

HUE-3294 [search] Add form to support percentiles aggregates

Romain Rigaux %!s(int64=9) %!d(string=hai) anos
pai
achega
f18cd5d

+ 12 - 2
apps/search/src/search/templates/common_search.mako

@@ -1987,7 +1987,7 @@ ${ dashboard.layout_skeleton() }
 
 <script type="text/html" id="metric-form">
   <div data-bind="visible: $root.isEditing" style="margin-bottom: 20px">    
-    <!-- ko if: ['sum', 'avg', 'mul', 'unique'].indexOf($data.function()) != -1 -->
+    <!-- ko if: ['sum', 'avg', 'mul', 'unique', 'percentile'].indexOf($data.function()) != -1 -->
       <select data-bind="options: HIT_OPTIONS, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-medium"></select>
     <!-- /ko -->
     
@@ -1995,6 +1995,16 @@ ${ dashboard.layout_skeleton() }
       <select data-bind="selectize: $root.collection.template.fieldsNames, options: $root.collection.template.fieldsNames, value: value, selectizeOptions: {create: true}, optionsCaption: '${ _ko('Field...') }'" class="hit-options" style="margin-bottom: 0"></select>
     <!-- /ko -->
 
+    <!-- ko if: $data.function() == 'percentile' -->
+      <!-- ko foreach: percentiles() -->
+        <input type="number" class="input-mini" data-bind="value: value"/>
+        <a href="javascript: void(0)" data-bind="click: function() { $parent.percentiles.push(ko.mapping.fromJS({'value': 50})); }">
+          <i class="fa fa-plus" title="${ _('Add') }"></i>
+        </a>
+        <i class="fa fa-minus" title="${ _('Delete') }"></i>
+      <!-- /ko -->
+    <!-- /ko -->
+
     <a href="javascript: void(0)" data-bind="click: function() {
         $parent.ops.pop($data); }
       ">
@@ -2003,7 +2013,7 @@ ${ dashboard.layout_skeleton() }
 
     <br/>
     <a href="javascript: void(0)" data-bind="click: function() {
-        $data.ops.push(ko.mapping.fromJS({'function': 'mul', 'ops': [{'function': 'field', 'value': 'price', 'ops': []}, {'function': 'field', 'value': '2', 'ops': []}]})); }
+        $data.ops.push(ko.mapping.fromJS({'function': 'mul', 'ops': [{'function': 'field', 'value': 'price', 'ops': []}, {'function': 'field', 'value': '1', 'ops': []}]})); }
       ">
       <i class="fa fa-plus" title="${ _('Add formula operation') }"></i>
     </a>

+ 2 - 8
apps/search/src/search/views.py

@@ -570,15 +570,9 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     'limit': 10,
     'mincount': 0,
     'isDate': False,
-    'aggregate': {'function': 'unique', 'ops': []}
+    'aggregate': {'function': 'unique', 'ops': [], 'percentiles': [{'value': 50}]}
   }
 
-  # avg(stars)                      # {'function': 'avg', 'ops': []}
-  # avg(mul(stars,1))               # {'function': 'avg', 'ops': [{'function': 'mul', 'ops': ['stars', 1]}]}
-  # avg(mul(stars,1,max(f2)))       # {'function': 'avg', 'ops': [{'function': 'mul', 'ops': ['stars', 1, {'function': 'max', 'ops': ['f2']}]}]}
-  # avg(percentile(stars,50,100))   # {'function': 'avg', 'ops': [{'function': 'percentile', 'ops': ['stars', 50, 100]}]}
-  # percentile(mul(stars,2),50)     # {'function': 'percentile', 'ops': [{'function': 'mul', 'ops': ['stars', 2]}, extra: [50]]}
-
   if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):
     facet_type = 'pivot'
   elif widget_type == 'gradient-map-widget':
@@ -607,7 +601,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
         facet_type = 'function'
       else:
         facet_type = 'nested'
-      properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'aggregate': {'function': 'unique', 'ops': []}}
+      properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'aggregate': {'function': 'unique', 'ops': [], 'percentiles': [{'value': 50}]}}
       properties['facets'] = []
       properties['domain'] = {'blockParent': [], 'blockChildren': []}
       if widget_type == 'pie2-widget':

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

@@ -605,7 +605,7 @@ class SolrApi(object):
 
   def _get_aggregate_function(self, facet):
     f = facet['properties']['aggregate']
-    
+
     if not f['ops']:
       f['ops'] = [{'function': 'field', 'value': facet['field'], 'ops': []}]
     
@@ -618,6 +618,8 @@ class SolrApi(object):
       fields = []
       for _f in f['ops']:
         fields.append(self.__get_aggregate_function(_f))
+      if f['function'] == 'percentile':
+        fields.extend(map(lambda a: str(a), [_p['value'] for _p in f['percentiles']]))
       return '%s(%s)' % (f['function'], ','.join(fields))
     
   def _get_range_borders(self, collection, query):