Skip to content

Commit

Permalink
HUE-5149 [metadata] Add highlighting to the result records
Browse files Browse the repository at this point in the history
  • Loading branch information
romainr committed Oct 26, 2016
1 parent c9298b6 commit 938bd3c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
11 changes: 7 additions & 4 deletions desktop/core/src/desktop/templates/assist.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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>
Expand Down
24 changes: 15 additions & 9 deletions desktop/libs/metadata/src/metadata/navigator_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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():
Expand All @@ -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]))
Expand All @@ -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)
Expand Down

0 comments on commit 938bd3c

Please sign in to comment.