Browse Source

HUE-5149 [metadata] Add highlighting to the result records

Romain Rigaux 9 years ago
parent
commit
938bd3c

+ 7 - 4
desktop/core/src/desktop/templates/assist.mako

@@ -924,7 +924,7 @@ from metadata.conf import has_navigator
       overflow:hidden;
     }
 
-    .nav-autocomplete-item-link em {
+    .nav-autocomplete-item-link em, .result-entry em {
       font-style: normal;
       font-weight: bold;
     }
@@ -1026,13 +1026,16 @@ from metadata.conf import has_navigator
           </div>
           <div class="doc-col" data-bind="css: { 'doc-col-no-desc' : !hasDescription }">
             <!-- ko if: typeof click !== 'undefined' -->
-            <a class="pointer" data-bind="click: click, text: parentPath + ' ' + originalName" target="_blank" ></a>
+            <a class="pointer" data-bind="click: click, html: hue_name" target="_blank" ></a>
             <!-- /ko -->
             <!-- ko if: typeof click === 'undefined' -->
             <a class="pointer" data-bind="attr: { 'href': link }, text: originalName" target="_blank" ></a>
             <!-- /ko -->
-            <div class="nav-search-tags" data-bind="foreach: tags"><div data-bind="text: $data"></div></div>
-            <!-- ko if: hasDescription -->
+            <div class="nav-search-tags" data-bind="foreach: tags">
+              <div data-bind="text: $data"></div>
+            </div>
+            <div class="doc-desc" data-bind="html: hue_description"></div>
+            <!-- ko if: hasDescription && ! hue_description -->
               <div class="doc-desc" data-bind="text: originalDescription"></div>
             <!-- /ko -->
           </div>

+ 15 - 9
desktop/libs/metadata/src/metadata/navigator_api.py

@@ -84,6 +84,8 @@ def search_entities(request):
 
   entities = api.search_entities(query_s, limit=limit, offset=offset, sources=sources)
 
+  _augment_highlighting(query_s, entities)
+
   response = {
     'entities': entities,
     'count': len(entities),
@@ -218,7 +220,14 @@ def search_entities_interactive(request):
       if ':' in query_s and not response['facets'][fname]:
         del response['facets'][fname]
 
-  # Highlighting
+  _augment_highlighting(query_s, response.get('results'))
+
+  response['status'] = 0
+
+  return JsonResponse(response)
+
+
+def _augment_highlighting(query_s, records):
   fs = {}
   ts = []
   for term in query_s.split():
@@ -230,15 +239,16 @@ def search_entities_interactive(request):
       if term.strip('*'):
         ts.append(term.strip('*'))
 
-  for record in response.get('results'):
+  for record in records:
     record['hue_description'] = ''
     record['hue_name'] = (record.get('parentPath', '').replace('/', '.') + '.').lstrip('.')
     name = record.get('originalName', '')
     for term in ts:
       name = _highlight(term, name)
-      for tag in record.get('tags', []):
-        if re.match(term, tag):
-          record['hue_description'] += ' tags:%s' % _highlight(term, tag)
+      if record.get('tags'):
+        for tag in record['tags']:
+          if re.match(term, tag):
+            record['hue_description'] += ' tags:%s' % _highlight(term, tag)
     for fname, fval in fs.iteritems(): # e.g. owner:<em>hu</em>e
       if record.get(fname, ''):
         record['hue_description'] += ' %s:%s' % (fname, _highlight(fval, record[fname]))
@@ -247,10 +257,6 @@ def search_entities_interactive(request):
     record['hue_name'] = escape(record['hue_name']).replace('&lt;em&gt;', '<em>').replace('&lt;/em&gt;', '</em>')
     record['hue_description'] = escape(record['hue_description']).replace('&lt;em&gt;', '<em>').replace('&lt;/em&gt;', '</em>')
 
-  response['status'] = 0
-
-  return JsonResponse(response)
-
 
 def _highlight(pattern, string):
   return re.sub('(%s)' % pattern, '<em>\\1</em>', string, count=1)