Skip to content

Instantly share code, notes, and snippets.

@cooncesean
Created June 8, 2012 00:11
Show Gist options
  • Save cooncesean/2892533 to your computer and use it in GitHub Desktop.
Save cooncesean/2892533 to your computer and use it in GitHub Desktop.
How To Use Haystack to Add Results Based on a MultiValueField
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=200, default='')
class Article(models.Model):
name = models.CharField(max_length=200, default='')
text = models.TextField(default='')
from haystack import indexes
from myapp.models import Article
class ArticleIndex(indexes.Indexable):
name = indexes.CharField()
text = indexes.CharField(indexed=False, document=True)
authors = indexes.MultiValueField()
def get_model(self):
return Article
def prepare_authors(self, obj):
return [(author.name) for author in obj.authors.all()]
from haystack.query import SearchQuerySet
term = 'foo'
# I know how to filter the results by author
SearchQuerySet().filter(name=term).filter(authors=term)
# How do I search BOTH the `name` and `authors` fields using the same term?
SearchQuerySet().filter(name=term) | SearchQuerySet().filter(authors=term)
@anupkalburgi
Copy link

What search engine are you using as back-end i am using whoosh, It's not working for me, no results returned.

@bartoszzolkiewski
Copy link

I know this is a veeery old one, but for future generations I will post the answer: use filter_or like this
SearchQuerySet().filter_or(name=term).filter_or(authors=term)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment