Skip to content

Commit fc0ae30

Browse files
author
Steve Byerly
committed
merge master > tymofij/solr-hl-options
2 parents f5abe0b + 27def75 commit fc0ae30

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

haystack/backends/solr_backend.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ def build_search_kwargs(self, query_string, sort_by=None, start_offset=0, end_of
189189
kwargs['hl.fragsize'] = '200'
190190

191191
if isinstance(highlight, dict):
192-
kwargs.update(highlight)
192+
# autoprefix highlighter options with 'hl.', all of them start with it anyway
193+
# this makes option dicts shorter: {'maxAnalyzedChars': 42}
194+
# and lets some of options be used as keyword arguments: `.highlight(preserveMulti=False)`
195+
kwargs.update({
196+
key if key.startswith("hl.") else ('hl.' + key): highlight[key]
197+
for key in highlight.keys()
198+
})
193199

194200
if self.include_spelling is True:
195201
kwargs['spellcheck'] = 'true'

test_haystack/solr_tests/test_solr_backend.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,22 @@ 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
394+
highlight_dict = {'simple.pre':'<i>', 'simple.post': '</i>'}
395+
self.assertEqual(self.sb.search('', highlight=highlight_dict), {'hits': 0, 'results': []})
396+
self.assertEqual(self.sb.search('Index', highlight=highlight_dict)['hits'], 3)
397+
self.assertEqual([result.highlighted['text'][0] for result in self.sb.search('Index', highlight=highlight_dict)['results']],
398+
['<i>Indexed</i>!\n1', '<i>Indexed</i>!\n2', '<i>Indexed</i>!\n3'])
399+
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+
405+
self.assertEqual(self.sb.search('Indx')['hits'], 0)
406+
self.assertEqual(self.sb.search('indax')['spelling_suggestion'], 'index')
407+
self.assertEqual(self.sb.search('Indx', spelling_query='indexy')['spelling_suggestion'], 'index')
408+
393409
self.assertEqual(self.sb.search('', facets={'name': {}}), {'hits': 0, 'results': []})
394410
results = self.sb.search('Index', facets={'name': {}})
395411
self.assertEqual(results['hits'], 3)

0 commit comments

Comments
 (0)