简体   繁体   English

Haystack SearchQuerySet不会在具有一个字符的CharField上进行过滤(Whoosh / django-haystack)

[英]Haystack SearchQuerySet won't filter on a CharField with one character (Whoosh / django-haystack)

I'm using Django 1.5.1 with django-haystack 2.1.0 and the whoosh 2.5.2 backend: 我正在使用Django 1.5.1和django-haystack 2.1.0以及飞快移动的2.5.2后端:

models.py: models.py:

GENDER_CHOICES = (
    (u'M', u'Male'),
    (u'F', u'Female'),
)

class Applicant(models.Model):

    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    first_name = models.CharField(max_length=64)
    last_name = models.CharField(max_length=64)

search_indexes.py: search_indexes.py:

class ApplicantIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True,use_template=True)
    gender = indexes.CharField(model_attr="gender")

search template 搜索模板

{{ object.first_name }}
{{ object.last_name }}

In the django shell i'm trying following: 在django shell我正在尝试以下:

>>> from haystack.query import SearchQuerySet

>>> sqs=SearchQuerySet()
>>> sqs
[<SearchResult: tooldb.applicant (pk=u'1')>, <SearchResult: tooldb.applicant (pk=u'2')>]

>>> sqs[0].gender
u'M'     #<-- So this seems to be indexed

#but when i try:
>>> sqs.filter(gender='M')
[]     #<-- I get nothing ... ?

I tried it with other CharFields without choices and max_lenght > 1, no problem at all, haystack filters like it should. 我尝试了其他CharFields没有选择和max_lenght> 1,没有任何问题,像它应该干草堆过滤器。

What am I missing? 我错过了什么?

Okay i think i got it.. 好吧,我想我明白了..

It seems like haystack in combination with whoosh doesn't work with single char queries at all . 这似乎是在用嗖单个字符查询不起作用组合大海捞针。
This is kind of annoying cause SearchQuerySet should be implemented in the way Django's QuerySet works. 这有点令人讨厌,因为SearchQuerySet应该以Django的QuerySet的工作方式实现。 ( https://django-haystack.readthedocs.org/en/v2.1.0/searchqueryset_api.html#why-follow-queryset ) https://django-haystack.readthedocs.org/en/v2.1.0/searchqueryset_api.html#why-follow-queryset

QuerySet's filter method works fine with one character as query, SearchQuerySet just returns nothing. QuerySet的过滤方法适用于一个字符作为查询,SearchQuerySet只返回任何内容。

This should be documented somewhere... 这应记录在某个地方......

Here is my solution: 这是我的解决方案:

class ApplicantIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    gender = indexes.CharField()

    def prepare_gender(self, obj):
        return obj.gender*3

Now you can filter like: 现在您可以过滤:

sqs.filter(gender='MMM')

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

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