Skip to content

Commit 27def75

Browse files
committed
make solr backend accept both shortened and full-form highlighting options
1 parent 131cab6 commit 27def75

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

haystack/backends/solr_backend.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ def build_search_kwargs(self, query_string, sort_by=None, start_offset=0, end_of
191191
# autoprefix highlighter options with 'hl.', all of them start with it anyway
192192
# this makes option dicts shorter: {'maxAnalyzedChars': 42}
193193
# and lets some of options be used as keyword arguments: `.highlight(preserveMulti=False)`
194-
kwargs.update({'hl.' + key: highlight[key] for key in highlight.keys()})
194+
kwargs.update({
195+
key if key.startswith("hl.") else ('hl.' + key): highlight[key]
196+
for key in highlight.keys()
197+
})
195198

196199
if self.include_spelling is True:
197200
kwargs['spellcheck'] = 'true'

test_haystack/solr_tests/test_solr_backend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,18 @@ def test_search(self):
390390
self.assertEqual(self.sb.search('Index', highlight=True)['hits'], 3)
391391
self.assertEqual([result.highlighted['text'][0] for result in self.sb.search('Index', highlight=True)['results']], ['<em>Indexed</em>!\n1', '<em>Indexed</em>!\n2', '<em>Indexed</em>!\n3'])
392392

393+
# shortened highlighting options
393394
highlight_dict = {'simple.pre':'<i>', 'simple.post': '</i>'}
394395
self.assertEqual(self.sb.search('', highlight=highlight_dict), {'hits': 0, 'results': []})
395396
self.assertEqual(self.sb.search('Index', highlight=highlight_dict)['hits'], 3)
396397
self.assertEqual([result.highlighted['text'][0] for result in self.sb.search('Index', highlight=highlight_dict)['results']],
397398
['<i>Indexed</i>!\n1', '<i>Indexed</i>!\n2', '<i>Indexed</i>!\n3'])
398399

400+
# full-form highlighting options
401+
highlight_dict = {'hl.simple.pre':'<i>', 'hl.simple.post': '</i>'}
402+
self.assertEqual([result.highlighted['text'][0] for result in self.sb.search('Index', highlight=highlight_dict)['results']],
403+
['<i>Indexed</i>!\n1', '<i>Indexed</i>!\n2', '<i>Indexed</i>!\n3'])
404+
399405
self.assertEqual(self.sb.search('Indx')['hits'], 0)
400406
self.assertEqual(self.sb.search('indax')['spelling_suggestion'], 'index')
401407
self.assertEqual(self.sb.search('Indx', spelling_query='indexy')['spelling_suggestion'], 'index')

0 commit comments

Comments
 (0)