瀏覽代碼

HUE-7503 [search] Keep sorting toggles in sync between facets of same dimension

Romain Rigaux 8 年之前
父節點
當前提交
0e6d7edade

+ 1 - 1
desktop/libs/dashboard/src/dashboard/api.py

@@ -369,7 +369,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
     'limit': 10,
     'mincount': 1,
     'isDate': False,
-    'aggregate': {'function': 'count', 'formula': '', 'generated_formula': '', 'percentiles': [{'value': 50}]}
+    'aggregate': {'function': 'unique', 'formula': '', 'generated_formula': '', 'percentiles': [{'value': 50}]}
   }
 
   if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):

+ 33 - 1
desktop/libs/dashboard/src/dashboard/static/dashboard/js/search.ko.js

@@ -1358,7 +1358,7 @@ var Collection = function (vm, collection) {
     vm.search();
   };
 
-  self.toggleSortFacet2 = function (facet_field, event) {
+  self.toggleSortFacet2 = function (widget, facet_field) {
     var sortField = facet_field.properties ? facet_field.properties.sort : facet_field.sort;
 
     if (sortField() == 'desc') {
@@ -1367,6 +1367,16 @@ var Collection = function (vm, collection) {
       sortField('desc');
     }
 
+    // Update facets of same dimension to be consistent
+    if (self.engine() == 'solr') {
+      $.each(self.getDimensionFacets(widget, facet_field), function(index, facet) {
+        if (facet != facet_field) {
+          var sortField = facet.properties ? facet.properties.sort : facet.sort;
+          sortField('default');
+        }
+      })
+    }
+
     if (facet_field.properties && facet_field.properties.type && facet_field.properties.type() == 'range-up') {
       vm.query.removeFilter(ko.mapping.fromJS({'id': facet_field.id})); // Reset filter query
     }
@@ -1374,6 +1384,28 @@ var Collection = function (vm, collection) {
     vm.search();
   };
 
+  self.getDimensionFacets = function (widget, facet_field) {
+    var facets = [widget];
+    var facetFound = widget == facet_field;
+
+    $.each(widget.properties.facets(), function(index, facet) {
+      if (facet.aggregate.function() == 'count') {    	
+    	if (facetFound) {
+    	  return false;
+    	} else {
+    	  facets = [facet];
+    	}
+      }
+      if (facet == facet_field) {
+        facetFound = true;
+      } else {
+    	facets.push(facet);
+      }
+    });
+
+    return facets;
+  };
+
   self.toggleRangeFacet = function (facet_field, event) { // Deprecated after Hue 4
     vm.query.removeFilter(ko.mapping.fromJS({'id': facet_field.id})); // Reset filter query
 

+ 5 - 4
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -574,10 +574,11 @@ ${ dashboard.layout_skeleton(suffix='search') }
     <!-- ko if: widgetType() != 'hit-widget' -->
     <div class="facet-field-cnt">
       <span class="facet-field-label facet-field-label-fixed-width">${ _('Sorting') }</span>
-      <a href="javascript: void(0)" title="${ _('Toggle sort order') }" data-bind="click: $root.collection.toggleSortFacet2">
-        <i class="fa" data-bind="css: { 'fa-caret-down': properties.sort() == 'desc', 'fa-caret-up': properties.sort() == 'asc' }"></i>
+      <a href="javascript: void(0)" title="${ _('Toggle sort order') }" data-bind="click:  function() { $root.collection.toggleSortFacet2($data, $data); }">
+        <i class="fa" data-bind="css: { 'fa-caret-down': properties.sort() == 'desc', 'fa-caret-up': properties.sort() == 'asc', 'fa-sort': properties.sort() == 'default' }"></i>
         <span data-bind="visible: properties.sort() == 'desc'">${_('descending')}</span>
         <span data-bind="visible: properties.sort() == 'asc'">${_('ascending')}</span>
+        <span data-bind="visible: $parent.sort() == 'default'">${_('default')}</span>
       </a>
     </div>
     <!-- /ko -->
@@ -2099,11 +2100,11 @@ ${ dashboard.layout_skeleton(suffix='search') }
       <!-- ko if: $data.function() != 'field' && $data.metrics -->
       <div class="facet-field-cnt">
         <span class="facet-field-label facet-field-label-fixed-width">${ _('Sorting') }</span>
-        <a href="javascript: void(0)" title="${ _('Toggle sort order') }" data-bind="click: function() { $root.collection.toggleSortFacet2($parent); }">
+        <a href="javascript: void(0)" title="${ _('Toggle sort order') }" data-bind="click: function() { $root.collection.toggleSortFacet2($parents[1], $parent); }">
           <i class="fa" data-bind="css: { 'fa-caret-down': $parent.sort() == 'desc', 'fa-caret-up': $parent.sort() == 'asc', 'fa-sort': $parent.sort() == 'default' }"></i>
           <span data-bind="visible: $parent.sort() == 'desc'">${_('descending')}</span>
           <span data-bind="visible: $parent.sort() == 'asc'">${_('ascending')}</span>
-          <span data-bind="visible: $parent.sort() == 'default'"></span>
+          <span data-bind="visible: $parent.sort() == 'default'">${_('default')}</span>
         </a>
       </div>
 

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

@@ -143,11 +143,20 @@ 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':
+#           sort = {'count': facet['sort']}
+          sort = {'count': facet['properties']['sort']}
+          for i, agg in enumerate(self._get_dimension_aggregates(facet['properties']['facets'])):
+            if agg['sort'] != 'default':
+              agg_function = self._get_aggregate_function(agg)
+              sort = {'agg_%02d_%02d:%s' % (1, i, agg_function): agg['sort']}
+          print sort
+          if sort.get('count') == 'default':
+            sort['count'] = 'desc'
           _f = {
               'field': facet['field'],
               'limit': int(facet['properties'].get('limit', 10)) + (1 if facet['widgetType'] == 'text-facet-widget' else 0),
               'mincount': int(facet['properties']['mincount']),
-              'sort': {'count': facet['properties']['sort']},
+              'sort': sort,
           }
 
           if facet['properties']['domain'].get('blockParent') or facet['properties']['domain'].get('blockChildren'):
@@ -265,6 +274,8 @@ class SolrApi(object):
         if agg['sort'] != 'default':
           agg_function = self._get_aggregate_function(agg)
           sort = {'agg_%02d_%02d:%s' % (dim, i, agg_function): agg['sort']}
+      if sort.get('count') == 'default':
+        sort['count'] = 'desc'
 
       _f[f_name] = {
           'type': 'terms',
@@ -276,6 +287,7 @@ class SolrApi(object):
           'sort': sort
           #'prefix': '' # Forbidden on numeric fields
       }
+
       if widget['widgetType'] == 'tree2-widget' and facets[-1]['aggregate']['function'] != 'count':
         _f['subcount'] = self._get_aggregate_function(facets[-1])
 
@@ -928,7 +940,13 @@ class SolrApi(object):
 
 
   def _get_dimension_aggregates(self, facets):
-    return [agg for agg in facets[0:] if agg['aggregate']['function'] != 'count']
+    aggregates = []
+    for agg in facets[0:]:
+      if agg['aggregate']['function'] != 'count':
+        aggregates.append(agg)
+      else:
+        return aggregates
+    return aggregates
 
 
   def _get_nested_fields(self, collection):