Вопрос

I have 3 models with 3 indexes like (Modelname1Index, ModelName2Index, Modelname3Index) in search_indexes.py.

SearchQuerySet().filter is search from all the three, but I want to search each of them separately.

So, I tried doing like:

SearchQuerySet().filter(text=query).models(required_model_name)

But, it is returning results from the whole search(from all models), not from single model mentioned.

Also, when doing rebuild index, the no objects indexed for third model as that model's doesnt have any rows.

So, .models(thirdmodel) is returning empty result, but .models(any of the two other models) returning the whole result.

Это было полезно?

Решение

Unfortunately, .models() filter is not working correctly with the latest builds of Haystack and Whoosh.

You can fix this problem by downgrading your Haystack to 2.0.0 and Whoosh to 2.4.1.

Also note, that with Whoosh you will get a bug when trying to use HAYSTACK_SEARCH_RESULTS_PER_PAGE in your configuration, so you better avoid this setting.
That's not too terrible, though.

Другие советы

This is based partlyon James Lims answer, but this should work for any versions of Haystack and Whoosh. Unfortunately neither party is really coming to the rescue on this, but the below solution doesn't seem to be too bad.

class MySearchQuerySet(SearchQuerySet):
    def models(self,*mods):
        # We have to redefine this because Whoosh & Haystack don't play well with model filtering
        from haystack.utils import get_model_ct
        mods = [get_model_ct(m) for m in mods]
        return self.filter(django_ct__in=mods)

Then where ever SearchQuerySet use MySearchQuerySet instead:

MySearchQuery().filter(name="foo").models(my_models.bar,my_models.baz)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top