简体   繁体   English

django haystack Whoosh-SearchQuerySet()。models(ModelName)不会缩小结果范围

[英]django haystack Whoosh - SearchQuerySet().models(ModelName) not narrowing results

I have 3 models with 3 indexes like (Modelname1Index, ModelName2Index, Modelname3Index) in search_indexes.py. 我在search_indexes.py中具有3个具有3个索引的模型,例如(Modelname1Index,ModelName2Index,Modelname3Index)。

SearchQuerySet().filter is search from all the three, but I want to search each of them separately. SearchQuerySet()。filter是从所有三个搜索,但是我想分别搜索它们。

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. 因此,.models(thirdmodel)返回空结果,但是.models(其他两个模型中的任何一个)返回整个结果。

Unfortunately, .models() filter is not working correctly with the latest builds of Haystack and Whoosh. 不幸的是, .models()过滤器无法与最新版本的Haystack和.models()一起正常使用。

You can fix this problem by downgrading your Haystack to 2.0.0 and Whoosh to 2.4.1. 您可以通过将Haystack降级为2.0.0,将Whoosh降级为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. 另请注意,使用HAYSTACK_SEARCH_RESULTS_PER_PAGE在尝试在配置中使用HAYSTACK_SEARCH_RESULTS_PER_PAGE时出现错误,因此最好避免使用此设置。
That's not too terrible, though. 不过,这并不可怕。

This is based partlyon James Lims answer , but this should work for any versions of Haystack and Whoosh. 这部分基于James Lims的答案 ,但这对任何版本的Haystack和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: 然后在任何地方SearchQuerySet使用MySearchQuerySet代替:

MySearchQuery().filter(name="foo").models(my_models.bar,my_models.baz)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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