Эх сурвалжийг харах

HUE-3294 [search] Add metric functions to the nested facets

Romain Rigaux 9 жил өмнө
parent
commit
3b32792f2e

+ 28 - 1
apps/search/src/search/static/search/js/search.ko.js

@@ -626,7 +626,7 @@ var Collection = function (vm, collection) {
     if (typeof facet.properties.facets != 'undefined') {
       $.each(facet.properties.facets(), function (index, pivotFacet) {
         if (pivotFacet.aggregate) {
-          pivotFacet.aggregate.subscribe(function () {
+          pivotFacet.aggregate.function.subscribe(function () {
             vm.search();
           });
         }
@@ -820,6 +820,33 @@ var Collection = function (vm, collection) {
     }
   }
 
+  self.addPivotFacetValue2 = function(facet) {
+    var pivot = null;
+
+    pivot = ko.mapping.fromJS({
+      'field': ko.mapping.toJS(facet.properties.facets_form.field),
+      'limit': ko.mapping.toJS(facet.properties.facets_form.limit),
+      'mincount': ko.mapping.toJS(facet.properties.facets_form.mincount),
+      'aggregate': ko.mapping.toJS(facet.properties.facets_form.aggregate),
+    });
+      
+      facet.properties.facets_form.field(null);
+      facet.properties.facets_form.limit(5);
+      facet.properties.facets_form.mincount(1);
+      
+      facet.properties.facets_form.aggregate.function('count');
+      facet.properties.facets_form.aggregate.ops.removeAll();
+      facet.properties.facets_form.aggregate.percentiles(ko.mapping.fromJS({'value': 50}));
+
+    if (pivot != null) {
+      pivot.aggregate.function.subscribe(function() {
+        vm.search();
+      });
+      facet.properties.facets.push(pivot);
+      vm.search();
+    }
+  }
+
   self.removePivotFacetValue = function(facet) {
     facet['pivot_facet'].properties.facets.remove(facet['value']);
 

+ 6 - 6
apps/search/src/search/templates/common_search.mako

@@ -585,7 +585,7 @@ ${ dashboard.layout_skeleton() }
         <div class="facet-field-cnt">
           <span class="spinedit-cnt">
             <span class="facet-field-label">${ _('Metric') }</span>
-            <select data-bind="options: HIT_OPTIONS, optionsText: 'label', optionsValue: 'value', value: aggregate" class="hit-options"></select>
+            <span data-bind="template: { name: 'metric-form', data: aggregate }"></span>
           </span>
         </div>
 
@@ -618,7 +618,7 @@ ${ dashboard.layout_skeleton() }
         <strong class="hit-title" data-bind="text: field, attr: {'title': field}"></strong>
         <span class="spinedit-cnt">
           <span class="facet-field-label">${ _('Metric') }</span>
-          <select data-bind="options: HIT_OPTIONS, optionsText: 'label', optionsValue: 'value', value: aggregate" class="hit-options"></select>
+          <span data-bind="template: { name: 'metric-form', data: aggregate }"></span>
         </span>
         <span class="spinedit-cnt">
           <span class="facet-field-label">
@@ -657,7 +657,7 @@ ${ dashboard.layout_skeleton() }
 
   <div class="filter-box" data-bind="visible: $root.isEditing() && properties.facets().length < 5" style="opacity: 0.7">
     <div class="title" style="border: 1px dashed #d8d8d8; border-bottom: none">
-      <a data-bind="visible: ko.toJSON(properties.facets_form.field), click: $root.collection.addPivotFacetValue" class="pull-right" href="javascript:void(0)">
+      <a data-bind="visible: ko.toJSON(properties.facets_form.field), click: $root.collection.addPivotFacetValue2" class="pull-right" href="javascript:void(0)">
         <i class="fa fa-plus"></i> ${ _('Add') }
       </a>
       <select data-bind="selectize: $root.collection.template.fieldsNames, options: $root.collection.template.fieldsNames, value: properties.facets_form.field, optionsCaption: '${ _ko('Field...') }'" class="hit-options" style="margin-bottom: 0"></select>
@@ -669,7 +669,7 @@ ${ dashboard.layout_skeleton() }
           <span class="facet-field-label">
             ${ _('Metric') }
           </span>
-          <select data-bind="options: HIT_OPTIONS, optionsText: 'label', optionsValue: 'value', value: properties.facets_form.aggregate" class="hit-options"></select>
+          <span data-bind="template: { name: 'metric-form', data: properties.facets_form.aggregate }"></span>
         </span>
       </div>
 
@@ -1987,9 +1987,9 @@ ${ dashboard.layout_skeleton() }
 
 <script type="text/html" id="metric-form">
   <div data-bind="visible: $root.isEditing" style="margin-bottom: 20px">    
-    <!-- ko if: ['sum', 'avg', 'unique', 'percentile', 'mul', 'add', 'sub', 'ms'].indexOf($data.function()) != -1 -->
+    ##<!-- ko if: ['sum', 'avg', 'unique', 'percentile', 'mul', 'add', 'sub', 'ms'].indexOf($data.function()) != -1 -->
       <select data-bind="options: HIT_OPTIONS, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-medium"></select>
-    <!-- /ko -->
+    ##<!-- /ko -->
     
     <!-- ko if: $data.function() == 'field' -->
       <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>

+ 7 - 4
desktop/libs/libsolr/src/libsolr/api.py

@@ -249,8 +249,8 @@ class SolrApi(object):
   def _n_facet_dimension(self, widget, _f, facets, dim):
     facet = facets[0]
     f_name = 'd%s' % dim
-
-    if facet['aggregate'] == 'count':
+    print facets
+    if facet['aggregate']['function'] == 'count':
       _f['facet'] = {
           f_name: {
               'type': 'terms',
@@ -259,7 +259,7 @@ class SolrApi(object):
               'mincount': int(facet['mincount'])
           }
       }
-      if widget['widgetType'] == 'tree2-widget' and facets[-1]['aggregate'] != 'count':
+      if widget['widgetType'] == 'tree2-widget' and facets[-1]['aggregate']['function'] != 'count':
         _f['subcount'] = self._get_aggregate_function(facets[-1])
       if len(facets) > 1: # Get n+1 dimension
         self._n_facet_dimension(widget, _f['facet'][f_name], facets[1:], dim+1)
@@ -604,7 +604,10 @@ class SolrApi(object):
     return 'OR'.join([q_template % (q['q'] or EMPTY_QUERY.get()) for q in query['qs']]).encode('utf-8')
 
   def _get_aggregate_function(self, facet):
-    f = facet['properties']['aggregate']
+    if 'properties' in facet:
+      f = facet['properties']['aggregate'] # Level 1 facet
+    else:
+      f = facet['aggregate']
 
     if not f['ops']:
       f['ops'] = [{'function': 'field', 'value': facet['field'], 'ops': []}]