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

HUE-7831 [search] Offer more like results

Easily get sets of records similar to other records (e.g. which wines are similar to a certain wine)

In the grid result:
- Select a set of fields to compare similarities against
- Select 'More like this'
- Each result will get a sublist of similar results
Romain Rigaux 8 жил өмнө
parent
commit
900048e

+ 7 - 0
desktop/libs/dashboard/src/dashboard/models.py

@@ -105,6 +105,8 @@ class Collection2(object):
       props['collection']['engine'] = 'solr'
     if 'leafletmap' not in props['collection']['template']:
       props['collection']['template']['leafletmap'] = {'latitudeField': None, 'longitudeField': None, 'labelField': None}
+    if 'moreLikeThis' not in props['collection']['template']:
+      props['collection']['template']['moreLikeThis'] = False
     if 'timeFilter' not in props['collection']:
       props['collection']['timeFilter'] = {
         'field': '',
@@ -687,6 +689,11 @@ def augment_response(collection, query, response):
       doc['externalLink'] = link
       doc['details'] = []
       doc['hueId'] = smart_unicode(doc.get(id_field, ''))
+      if 'moreLikeThis' in response and response['moreLikeThis'][doc['hueId']].get('numFound'):
+        _doc = response['moreLikeThis'][doc['hueId']]
+        doc['_childDocuments_'] = _doc['docs']
+        doc['numFound'] = _doc['numFound']
+        del response['moreLikeThis'][doc['hueId']]
 
   highlighted_fields = response.get('highlighting', {}).keys()
   if highlighted_fields and not query.get('download'):

+ 4 - 0
desktop/libs/dashboard/src/dashboard/static/dashboard/js/search.ko.js

@@ -609,6 +609,9 @@ var Collection = function (vm, collection) {
 
   self.template.fieldsSelected.subscribe(function () {
     vm.search();
+    if (self.template.moreLikeThis) {
+      self.template.moreLikeThis(false);
+    }
   });
   if (typeof self.template.extracode == 'undefined') {
     self.template.extracode = ko.observable();
@@ -2355,6 +2358,7 @@ var SearchViewModel = function (collection_json, query_json, initial_json) {
           childRecords.push(record);
         });
         doc['childDocuments'] = ko.observable(childRecords);
+        doc['numFound'] = ko.observable(item.numFound);
       }
       if (!template.isGridLayout()) {
         // fix the fields that contain dots in the name

+ 8 - 2
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -1145,13 +1145,19 @@ ${ dashboard.layout_skeleton(suffix='search') }
   ${ _(' results') }
 
   <span data-bind="visible: $root.isEditing()">
-    ${ _('Show') }
+    - ${ _('Show') }
     <span class="spinedit-cnt">
       <input type="text" data-bind="spinedit: ($parent.template ? $parent : $root.collection).template.rows, valueUpdate:'afterkeydown'" style="text-align: center; margin-bottom: 0" />
     </span>
     ${ _('results per page') }
   </span>
 
+  <!-- ko if: $parent == $root.collection && $root.collection.engine() == 'solr'-->
+  <span data-bind="visible: $root.collection.template.fieldsSelected().length > 0, click: function() {$root.collection.template.moreLikeThis(!$root.collection.template.moreLikeThis()); $root.search(); }" title="${ _('Show similar records based on the selected fields') }">
+    - <a href="javascript: void(0)"><span data-bind="text: $root.collection.template.moreLikeThis() ? '${ _ko('Hide') }' : '${ _ko('Show') }'"></span></a> ${ _('More like this') }
+  </span>
+  <!-- /ko -->
+
   <a href="javascript: void(0)" title="${ _('Next') }">
     <span data-bind="click: $root.collection.toggleSortColumnGridLayout"></span>
     <i class="fa fa-chevron-right" data-bind="
@@ -1499,7 +1505,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
                   <a href="javascript:void(0)" data-bind="click: toggleDocDetails">
                     <i class="fa" data-bind="css: {'fa-caret-right' : ! doc.showDetails(), 'fa-caret-down': doc.showDetails()}"></i>
                     <!-- ko if: doc.childDocuments != undefined -->
-                    &nbsp(<span data-bind="text: doc.childDocuments().length"></span>)
+                    &nbsp(<span data-bind="text: doc.numFound || doc.childDocuments().length"></span>)
                     <!-- /ko -->
                   </a>
                 </td>

+ 15 - 0
desktop/libs/libsolr/src/libsolr/api.py

@@ -230,6 +230,21 @@ class SolrApi(object):
     if nested_fields:
       fl += urllib.unquote(utf_quoter(',[child parentFilter="%s"]' % ' OR '.join(nested_fields)))
 
+    if collection['template']['moreLikeThis'] and fl != ['*']: # Potential conflict with nested documents
+      id_field = collection.get('idField', 'id')
+      params += (
+        ('mlt', 'true'),
+        ('mlt.fl', fl.replace(',%s' % id_field, '')),
+        ('mlt.mintf', 1),
+        ('mlt.mindf', 1),
+        ('mlt.maxdf', 50),
+        ('mlt.maxntp', 1000),
+        ('mlt.count', 10),
+        #('mlt.minwl', 1),
+        #('mlt.maxwl', 1),
+      )
+      fl = '*'
+
     params += (('fl', fl),)
 
     params += (