Two bugs related to Django search: when there is no search term: "reduce () an empty sequence with no initial value" For any search period: No results

I am getting two errors related to searching in Django (version 1.7, Windows 7, 32-bit) using AJAX and haystack.query.SearchQuerySet

.

First, when there is nothing in the search box (or just type in a space):

Exception Value: reduce() of empty sequence with no initial value

      

The problem string is a string SearchQuerySet().autocomplete(...)

in this view (function in views.py

):

def search_titles(request):
    articles = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
    return render_to_response('ajax_search.html', {'articles' : articles})

      

( SearchQuerySet

is part of haystack.query

.) Traceback:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/articles/search/

Django Version: 1.7c2
Python Version: 3.4.1
Installed Applications:
('article',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.messages',
 'django.contrib.sessions',
 'django.contrib.staticfiles',
 'django.contrib.formtools',
 'userprofile',
 'whoosh',
 'haystack')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "c:\applications\programming\python_341\Lib\site-packages\django\core\handlers\base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test\article\views.py" in search_titles
  111.     articles = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
File "c:\applications\programming\python_341\Lib\site-packages\haystack\query.py" in autocomplete
  463.         return clone.filter(six.moves.reduce(operator.__and__, query_bits))

Exception Type: TypeError at /articles/search/
Exception Value: reduce() of empty sequence with no initial value

      

Another problem is that when there is any text in the search box, it doesn't crash, but it never finds anything.

SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))

      

always returns an empty article object ( print(str(articles))

is []

).


Related settings in settings.py

:

WHOOSH_INDEX = os.path.join(BASE_DIR, "whoosh/")

HAYSTACK_CONNECTIONS =  {
    "default":  {
        "ENGINE" : "haystack.backends.whoosh_backend.WhooshEngine",
        "PATH" : WHOOSH_INDEX,
    },
}

      

In the application urls.py

:

url(r'^search/$', 'article.views.search_titles'),

      

Also, here search_indexes.py

:

from haystack import indexes
from article.models import Article

class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    pub_date = indexes.DateTimeField(model_attr='pub_date')

    content_auto = indexes.EdgeNgramField(model_attr='title')

    def get_model(self):
        return Article

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        print("ai.6  self.get_model().objects.all()=" + str(self.get_model().objects.all()) + "")
        return self.get_model().objects.all()

      

And the output is python manage.py rebuild_index

:

[R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test]python manage.py
rebuild_index

WARNING: This will irreparably remove EVERYTHING from your search index in connection 'd
efault'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` c
ommand.
Are you sure you wish to continue? [y/N] y
Removing all documents from your index because you said so.
All documents removed.
ai.6  self.get_model().objects.all()=[<Article: Test1 Titlee>, <Article: Test2 Title>, <
Article: Test3 Title>, <Article: Article 4 Title>, <Article: 5>, <Article: 1348 article
title>, <Article: bla>]
Indexing 7 articles

[R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test]

      

+3


source to share


1 answer


This answer is intended to be a reference for anyone getting similar errors with django-1.9 and haystack-2.4.1. I have the same errors. Found an answer working in this stackoverflow link - " Django 1.9 / Haystack 2.4.1" Model not found for SearchResult and " "

Basically the stop-tray library 2.4.1 has some issues fixed in the github source code. pip install

can be executed from github:



pip install -e git+git://github.com/django-haystack/django-haystack.git#egg=django-haystack
pip freeze --local git+git://github.com/django-haystack/django-haystack.git#egg=django-haystack > requirements.txt

      

0


source







All Articles