简体   繁体   中英

Django Haystack & Whoosh Search Working, But SearchQuerySet Return 0 Results

Edit: More info at bottom of post...

Original Question:

I seem to be having the same problem as in this (unresolved) question: django-haystack + Whoosh SearchQuerySet().all() always None

I've set up Haystack with Whoosh on my Django project and all was working fine at first (SearchQuerySet used to return results), but after an aborted attempt to create a new custom search form (rolled back from git) it appears that indexing and the original search page still all work fine, but now SearchQuerySet() always returns 0 results!

Running:

manage.py rebuild_index --verbosity=2

Correctly shows:

Indexing 14 assets
    indexed 1 - 14 of 14 (worker PID: 1234).

These indexed assets can then all be correctly searched on from the original search form.

However, opening a Django shell and running:

from haystack.query import SearchQuerySet
SearchQuerySet().all().count()

Always returns 0 !

Relevant pip freeze :

  • Python 3.5.2
  • Django 1.9.3
  • django-haystack 2.5.0
  • Whoosh 2.7.4

/myapp/search_indexes.py:

from haystack import indexes
from .models import Asset

class AssetIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.NgramField(document=True, use_template=True)
    asset_description = indexes.CharField(model_attr='asset_description')
    manufacturer = indexes.CharField(model_attr='asset_manufacturer')

    def get_model(self):
        return Asset

    def no_query_found(self):
        return self.searchqueryset.exclude(content='foo')

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

/myapp/templates/search/indexes/myapp/asset_text.txt:

{{ object.asset_description }}
{{ object.asset_details }}
{{ object.asset_manufacturer }}
{{ object.asset_model }}
... etc.

/myapp/urls.py:

urlpatterns = [
    ....
    url(r'^search/', include('haystack.urls')),
    ....
]

EDIT:

So digging in the Haystack source code I've found out where the 0 is coming from, but not why!

/myvenv/Lib/site-packages/hackstack/query.py

class SearchQuerySet(object):
    ...

    def __len__(self):
        if self._result_count is None:
            self._result_count = self.query.get_count()

        # Some backends give weird, false-y values here. Convert to zero.
        if not self._result_count:
            self._result_count = 0

    # This needs to return the actual number of hits, not what's in the cache.
    return self._result_count - self._ignored_result_count

    ....

Changing the 0 to any int makes SearchQuerySet always return that int, but I still don't know why if not self._result_count would be true...

It looks like this a bug in haystack that has already been reported but has yet to be addressed:

https://github.com/django-haystack/django-haystack/issues/1021

Unfortunately if the "text" index field is Ngram or EdgeNgram SearchQuerySet().count() and SearchQuerySet().all().count() will return 0 unless you specify a filter, eg SearchQuerySet().all().exclude(content='thisshouldnotmatchanythingintheindex').count() returns the total number of indexed objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM