Highlighting is working again
This commit is contained in:
parent
a6e7670973
commit
ccde83ff8c
|
@ -241,13 +241,13 @@ class XapianSearchBackendTestCase(TestCase):
|
|||
# results = self.sb.search('index', narrow_queries=set(['name:david1']))
|
||||
# self.assertEqual(results['hits'], 1)
|
||||
|
||||
# def test_highlight(self):
|
||||
# self.sb.update(self.msi, self.sample_objs)
|
||||
# self.assertEqual(len(self.xapian_search('')), 3)
|
||||
#
|
||||
# self.assertEqual(self.sb.search('', highlight=True), {'hits': 0, 'results': []})
|
||||
# self.assertEqual(self.sb.search('Index', highlight=True)['hits'], 3)
|
||||
# self.assertEqual([result.highlighted['text'] for result in self.sb.search('Index', highlight=True)['results']], ['<em>Index</em>ed!\n1', '<em>Index</em>ed!\n2', '<em>Index</em>ed!\n3'])
|
||||
def test_highlight(self):
|
||||
self.sb.update(self.msi, self.sample_objs)
|
||||
self.assertEqual(len(self.xapian_search('')), 3)
|
||||
|
||||
self.assertEqual(self.sb.search(xapian.Query(), highlight=True), {'hits': 0, 'results': []})
|
||||
self.assertEqual(self.sb.search(xapian.Query('indexed'), highlight=True)['hits'], 3)
|
||||
self.assertEqual([result.highlighted['text'] for result in self.sb.search(xapian.Query('indexed'), highlight=True)['results']], ['<em>indexed</em>!\n1', '<em>indexed</em>!\n2', '<em>indexed</em>!\n3'])
|
||||
|
||||
def test_spelling_suggestion(self):
|
||||
self.sb.update(self.msi, self.sample_objs)
|
||||
|
@ -259,8 +259,12 @@ class XapianSearchBackendTestCase(TestCase):
|
|||
self.assertEqual(self.sb.search(xapian.Query('indxed'))['hits'], 0)
|
||||
self.assertEqual(self.sb.search(xapian.Query('indxed'))['spelling_suggestion'], 'indexed')
|
||||
|
||||
self.assertEqual(self.sb.search(xapian.Query('indx'))['hits'], 0)
|
||||
self.assertEqual(self.sb.search(xapian.Query('indx'), spelling_query='indexy')['spelling_suggestion'], 'indexed')
|
||||
self.assertEqual(self.sb.search(xapian.Query('foo'))['hits'], 0)
|
||||
self.assertEqual(self.sb.search(xapian.Query('foo'), spelling_query='indexy')['spelling_suggestion'], 'indexed')
|
||||
|
||||
self.assertEqual(self.sb.search(xapian.Query('XNAMEdavid'))['hits'], 0)
|
||||
self.assertEqual(self.sb.search(xapian.Query('XNAMEdavid'))['spelling_suggestion'], 'david1')
|
||||
|
||||
|
||||
# def test_more_like_this(self):
|
||||
# self.sb.update(self.msi, self.sample_objs)
|
||||
|
|
|
@ -301,10 +301,10 @@ class SearchBackend(BaseSearchBackend):
|
|||
|
||||
for match in matches:
|
||||
app_label, module_name, pk, model_data = pickle.loads(match.document.get_data())
|
||||
if highlight and (len(query_string) > 0):
|
||||
if highlight:
|
||||
model_data['highlighted'] = {
|
||||
self.content_field_name: self._do_highlight(
|
||||
model_data.get(self.content_field_name), query_string
|
||||
model_data.get(self.content_field_name), query
|
||||
)
|
||||
}
|
||||
results.append(
|
||||
|
@ -461,9 +461,9 @@ class SearchBackend(BaseSearchBackend):
|
|||
|
||||
return (content_field_name, schema_fields)
|
||||
|
||||
def _do_highlight(self, content, text, tag='em'):
|
||||
def _do_highlight(self, content, query, tag='em'):
|
||||
"""
|
||||
Highlight `text` in `content` with html `tag`.
|
||||
Highlight `query` terms in `content` with html `tag`.
|
||||
|
||||
This method assumes that the input text (`content`) does not contain
|
||||
any special formatting. That is, it does not contain any html tags
|
||||
|
@ -473,10 +473,11 @@ class SearchBackend(BaseSearchBackend):
|
|||
`content` -- Content to search for instances of `text`
|
||||
`text` -- The text to be highlighted
|
||||
"""
|
||||
for term in [term.replace('*', '') for term in text.split()]:
|
||||
if term not in self.RESERVED_WORDS:
|
||||
term_re = re.compile(re.escape(term), re.IGNORECASE)
|
||||
content = term_re.sub('<%s>%s</%s>' % (tag, term, tag), content)
|
||||
for term in query:
|
||||
for match in re.findall('[^A-Z]+', term): # Ignore field identifiers
|
||||
match_re = re.compile(match, re.I)
|
||||
content = match_re.sub('<%s>%s</%s>' % (tag, term, tag), content)
|
||||
|
||||
return content
|
||||
|
||||
def _do_field_facets(self, results, field_facets):
|
||||
|
@ -627,7 +628,12 @@ class SearchBackend(BaseSearchBackend):
|
|||
else:
|
||||
return database.get_spelling_suggestion(spelling_query)
|
||||
|
||||
return ' '.join([database.get_spelling_suggestion(term) for term in query])
|
||||
term_list = []
|
||||
for term in query:
|
||||
for match in re.findall('[^A-Z]+', term): # Ignore field identifiers
|
||||
term_list.append(database.get_spelling_suggestion(match))
|
||||
|
||||
return ' '.join(term_list)
|
||||
|
||||
def _database(self, writable=False):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue