2

I'm currently adding search functionality to my Django application using django-haystack v2.0.0-beta and Whoosh as the back end. Creating the index and returning the search results works fine so far. Now I want to enable the highlighting feature but I don't get it to work.

I'm using a highly customized setup for which the haystack documentation is not a great help. My Django application is a pure AJAX application, i.e., all requests between client and server are handled asynchronously by using jQuery and $.ajax(). That's why I have written a custom Django view that creates the haystack search queryset manually and dumps the search results into a JSON object. All of this works fine, but the addition of highlighting does not work. Here is my code that I have so far:

search_indexes.py

class CrawledWebpageIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return CrawledWebpage # This is my Django model

forms.py

class HaystackSearchForm(forms.Form):

    q = forms.CharField(
        max_length=100,
        label='Enter your search query')

views.py (I adopted some code from this post as it looked reasonable to me but it's probably wrong)

def return_search_results_ajax(request):
    haystack_search_form = HaystackSearchForm(request.POST)
    response = {}

    if haystack_search_form.is_valid():
        search_query = haystack_search_form.cleaned_data['q']
        sqs = SearchQuerySet().filter(content=search_query)
        highlighted_search_form = HighlightedSearchForm(request.POST, searchqueryset=sqs, load_all=True)
        search_results = highlighted_search_form.search()

        # Here I extract those fields of my model that should be displayed as results
        webpage_urls = [result.object.url for result in search_results[:10]]
        response['webpage_urls'] = webpage_urls

    return HttpResponse(json.dumps(response), mimetype='application/json')

This code works fine as far as the search results are returned properly. But when I try to access the highlighted text snippet for a search result, for example for the first one:

print search_results[0].highlighted

Then I always get an empty string as the result: {'text': ['']}

Can anyone help me to get the highlighting feature working? Thank you very much in advance.

Community
  • 1
  • 1
pemistahl
  • 9,304
  • 8
  • 45
  • 75
  • Hello again! Which haystack backend are you using? – Emily Dec 20 '12 at 15:55
  • @Emily I'm using Whoosh as the backend. I already said that in my original post. I know that Whoosh supports highlighting, so it's not a wrong backend that causes my problem. – pemistahl Dec 20 '12 at 16:08
  • Sorry, missed that in the question. – Emily Dec 20 '12 at 16:11
  • 1
    It seems you're not the only one with this problem? https://github.com/toastdriven/django-haystack/issues/310 https://github.com/toastdriven/django-haystack/issues/273 https://github.com/toastdriven/django-haystack/issues/582 – Emily Dec 20 '12 at 16:18

2 Answers2

3

It looks like this is possibly a Haystack bug that has gone unresolved for a long time: http://github.com/toastdriven/django-haystack/issues/310 http://github.com/toastdriven/django-haystack/issues/273 http://github.com/toastdriven/django-haystack/issues/582

As an alternative, you could use Haystack's highlighting functionality instead of Whoosh's to highlight the results yourself. For example, once you get your search results in sqs, you could do

from haystack.utils import Highlighter
highlighter = Highlighter(search_query)
print highlighter.highlight(sqs[0].text)

to get the highlighted text of the first result. See http://django-haystack.readthedocs.org/en/latest/highlighting.html for the documentation.

Emily
  • 5,869
  • 1
  • 22
  • 15
  • Indeed, I should have browsed through the open issues on GitHub first. It's obviously not my fault then. I've just tried out Haystack's highlighting functionality and it works fine. So I will go with that. Again, thanks a lot, Emily! :) – pemistahl Dec 20 '12 at 17:06
0

I'm not familiar with Haystack but could it be because you're using HaystackSearchForm in one place and HighlightedSearchForm in another?

cerberos
  • 7,705
  • 5
  • 41
  • 43
  • Maybe, but I don't think so at the moment because the search results are returned properly. If you know a way to accomplish the same with `HighlightedSearchForm` only, I'm curious. – pemistahl Dec 20 '12 at 16:11
  • It's the same as `SearchForm` but adds highlighting, read the [HighlightedSearchForm](https://django-haystack.readthedocs.org/en/latest/views_and_forms.html?highlight=HighlightedSearchForm#highlightedsearchform) docs. – cerberos Dec 20 '12 at 16:15
  • You don't say. ;) I already did that. If it had helped me, I wouldn't have asked here. – pemistahl Dec 20 '12 at 16:19
  • Actually, Haystack provides forms which your form should inherit from instead of `forms.form`. I imagine you want `HighlightedSearchForm`. – cerberos Dec 20 '12 at 16:21