瀏覽代碼

HUE-3294 [search] Add more than one dimension to formula widget

Romain Rigaux 9 年之前
父節點
當前提交
0c3382a
共有 2 個文件被更改,包括 36 次插入24 次删除
  1. 23 4
      apps/search/src/search/templates/common_search.mako
  2. 13 20
      desktop/libs/libsolr/src/libsolr/api.py

+ 23 - 4
apps/search/src/search/templates/common_search.mako

@@ -1975,6 +1975,7 @@ ${ dashboard.layout_skeleton() }
   <!-- ko if: $root.getFacetFromQuery(id()).has_data() -->
   <div class="row-fluid" data-bind="with: $root.getFacetFromQuery(id())">
     <div data-bind="with: $root.collection.getFacetById($parent.id())">
+      <span class="facet-field-label">${ _('Metric') }</span>
       <span data-bind="template: { name: 'metric-form', data: properties.aggregate }"></span>
       <span data-bind="template: { name: 'facet-toggle2' }"></span>
     </div>
@@ -1985,11 +1986,29 @@ ${ dashboard.layout_skeleton() }
 
 
 <script type="text/html" id="metric-form">
-  <div data-bind="visible: $root.isEditing" style="margin-bottom: 20px">
-    <span class="facet-field-label">${ _('Metric') }</span>
-    <select data-bind="options: HIT_OPTIONS, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-medium"></select>
+  <div data-bind="visible: $root.isEditing" style="margin-bottom: 20px">    
+    <!-- ko if: ['sum', 'avg', 'mul', 'unique'].indexOf($data.function()) != -1 -->
+      <select data-bind="options: HIT_OPTIONS, optionsText: 'label', optionsValue: 'value', value: $data.function" class="input-medium"></select>
+    <!-- /ko -->
+    
+    <!-- ko if: $data.function() == 'field' -->
+      <select data-bind="selectize: $root.collection.template.fieldsNames, options: $root.collection.template.fieldsNames, value: value, optionsCaption: '${ _ko('Field...') }'" class="hit-options" style="margin-bottom: 0"></select>
+    <!-- /ko -->
+
+    <!-- ko if: $data.function() == 'constant' -->
+      <input data-bind="value: value" class="hit-options" style="margin-bottom: 0"></select>
+    <!-- /ko -->
+
+    <a href="javascript: void(0)" data-bind="click: function() {
+        $parent.ops.pop($data); }
+      ">
+      <i class="fa fa-minus" title="${ _('Delete') }"></i>
+    </a>
+
     <br/>
-    <a href="javascript: void(0)" data-bind="click: function() { $data.ops.push(ko.mapping.fromJS({'function': 'unique', 'ops': []})); }">
+    <a href="javascript: void(0)" data-bind="click: function() {
+        $data.ops.push(ko.mapping.fromJS({'function': 'mul', 'ops': [{'function': 'field', 'value': 'price', 'ops': []}, {'function': 'constant', 'value': '2', 'ops': []}]})); }
+      ">
       <i class="fa fa-plus" title="${ _('Add formula operation') }"></i>
     </a>
     <!-- ko foreach: ops() -->

+ 13 - 20
desktop/libs/libsolr/src/libsolr/api.py

@@ -594,7 +594,6 @@ class SolrApi(object):
     except RestException, e:
       raise PopupException(e, title=_('Error while accessing Solr'))
 
-
   def _get_params(self):
     if self.security_enabled:
       return (('doAs', self._user ),)
@@ -605,28 +604,22 @@ 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):
-    props = {
-        'field': facet['field'],
-        'aggregate': facet['properties']['aggregate']['function'] #if 'properties' in facet else facet['aggregate']
-    }
+    f = facet['properties']['aggregate']
+    
+    if not f['ops']:
+      f['ops'] = [{u'function': u'field', 'value': facet['field'], u'ops': []}]
+    
+    return self.__get_aggregate_function(f)
 
-    if  facet['properties']['aggregate']['ops']:
-      a = self.__get_aggregate_function()
-      return ''
+  def __get_aggregate_function(self, f):
+    if f['function'] == 'field' or f['function'] == 'constant':
+      return f['value']
     else:
-      return '%(aggregate)s(%(field)s)' % props 
-#     if props['aggregate'] == 'percentile':
-#       return 'percentile(%(field)s,50)' % props
-#     else:
-#       return '%(aggregate)s(%(field)s)' % props
-  
-  def __get_aggregate_function(self):
-    pass
+      fields = []
+      for _f in f['ops']:
+        fields.append(self.__get_aggregate_function(_f))
+      return '%s(%s)' % (f['function'], ','.join(fields))
     
-  # 'aggregate': {'function': 'count', 'ops': []}
-  # avg(stars)                      # {'function': 'avg', 'ops': []}
-  # avg(mul(stars,1))               # {'function': 'avg', 'ops': [{'function': 'mul', 'ops': ['stars', 1]}]}
-
   def _get_range_borders(self, collection, query):
     props = {}
     GAPS = {