Created
August 19, 2015 09:09
-
-
Save seddonym/7f4a6bcd67d711ceb2fd to your computer and use it in GitHub Desktop.
How to override faulty behaviour of Haystack highlighting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from haystack.utils.highlighting import Highlighter as FaultyHighlighter | |
class Highlighter(FaultyHighlighter): | |
"""Highlighter that fixes the issue with highlighting parts of words: | |
https://github.com/django-haystack/django-haystack/issues/378 | |
""" | |
def find_highlightable_words(self): | |
word_positions = {} | |
lower_text_block = self.text_block.lower() | |
for word in self.query_words: | |
# Use a regular expression to search by whole words | |
# \b corresponds to a word boundary | |
matches = re.finditer(r'\b%s\b' % word, lower_text_block) | |
if matches: | |
word_positions[word] = [match.start() for match in matches] | |
return word_positions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HAYSTACK_CUSTOM_HIGHLIGHTER = 'myapp.highlighting.Highlighter' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this sollution.