Django Haystack index on multiple fields from same model

自闭症网瘾萝莉.ら 提交于 2019-12-13 14:28:51

问题


I am trying to embed elasticsearch in my Django app using django-haystack. I am trying to implement a user search. My user model is this:

class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=255, unique=True)
    name = models.CharField(max_length=63, blank=True)
    email = models.EmailField(blank=True, unique=True)
    status = models.CharField(max_length=255, blank=True, null=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    joined = models.DateTimeField(auto_now_add=True, null=True)

Now, I want to search on name and username fields. I have created the following search_indexes.py:

class UserIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, model_attr='name')
    username = indexes.CharField(model_attr='username')

    def get_model(self):
        return MyUser

    def get_updated_field(self):
        return "joined"

However, when I perform a search, I only get the results matching the name field. What am I doing wrong here? Is there some other way to do this?

Thanks in advance.


回答1:


The way django-haystack works, the data inside the document=True field is used for a general search, and any other fields are used for individual filtering. So in your case, a search will only use the name field. To fix this, you'll need to use a template that specifies all the fields to be used in a search. First of all, use:

text = indexes.EdgeNgramField(document=True, use_template=True)

Then you’ll need to create a new template inside your template directory called search/indexes/myapp/myuser_text.txt and place the following inside:

{{ object.username }}
{{ object.name }}

See http://django-haystack.readthedocs.org/en/latest/tutorial.html#handling-data for full reference



来源:https://stackoverflow.com/questions/28272214/django-haystack-index-on-multiple-fields-from-same-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!