Diego dos Santos
Diego dos Santos

Reputation: 128

Haystack: KeyError in the elasticsearch_backend module

I'm using Django + Haystack + Elasticsearch.

When I send a request to this view

from haystack.views import FacetedSearchView

from .models import Object


class ObjectView(FacetedSearchView):

    def extra_context(self):
        extra = super(ObjectView, self).extra_context()

        if not self.results:
            extra['objects'] = Object.objects.all()
        else:
            searchqueryset = self.form.search()
            results = [ result.pk for result in searchqueryset ]
            extra['facets'] = self.results.facet_counts()
            extra['objects'] = Object.objects.filter(pk__in=results)
            extra['results'] = self.results
        return extra

this error is raised:

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/views.py", line 49, in __call__
    return self.create_response()

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/views.py", line 129, in create_response
    (paginator, page) = self.build_page()

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/views.py", line 106, in build_page
    self.results[start_offset:start_offset + self.results_per_page]

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/query.py", line 266, in __getitem__
    self._fill_cache(start, bound)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/query.py", line 164, in _fill_cache
    results = self.query.get_results(**kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/__init__.py", line 485, in get_results
    self.run(**kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py", line 942, in run
    results = self.backend.search(final_query, **search_kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/__init__.py", line 26, in wrapper
    return func(obj, query_string, *args, **kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py", line 521, in search
    distance_point=kwargs.get('distance_point'), geo_sort=geo_sort)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py", line 571, in _process_results
    raw_suggest = raw_results['suggest']['suggest']

KeyError: 'suggest'

A curious fact: the problem occurs only when the project is under production settings, even when I haven't changed a single thing involving Haystack or Elasticsearch in the settings_production module(except for the URL key).

project/settings_production.py

'URL': 'http://0.0.0.0:9200/'

In production, I'm using nothing more than a simple FastCGI.

And here's what really bothers me: sometimes I get no errors on this view, and everything works just fine...

Please, someone has an idea of what's going on?

Thanks a lot!

UPDATE:

SO, I setup my whole project in another computer. After some tests I verified:

  1. this problem is not related to my production settings like I described above;

  2. the error is not raised when the elasticsearch service is stopped;

  3. if the service is running:

    • when the method Object.objects.all() returns some QuerySet results, I got no errors;
    • when the method Object.objects.all() returns an empty QuerySet, the problem persists;

I guess this is some kind of bug in the Haystack's elasticsearch_backend module.

Still, i'm not sure.

Upvotes: 0

Views: 593

Answers (1)

skoczen
skoczen

Reputation: 1518

Yup, it's a bug in haystack. I've put in a pull request, but in the meantime, options to get running are:

  1. Set INCLUDE_SPELLING in your haystack settings to False, or
  2. Use our fork: https://github.com/greenkahuna/django-haystack

Upvotes: 2

Related Questions