Search code examples
pythondjangodjango-haystack

Django Haystack index on multiple fields from same model


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.


Solution

  • 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