I'm attempting to use django-haystack + whoosh in my Django application. My index class looks like this
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
abstract = indexes.CharField(model_attr='abstract')
def get_model(self):
return Article
def index_queryset(self, using=None):
return self.get_model().objects.all()
and my model looks like this:
class Article(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(User)
abstract = models.CharField(max_length=500, blank=True)
full_text = models.TextField(blank=True)
proquest_link = models.CharField(max_length=200, blank=True, null=True)
ebsco_link = models.CharField(max_length=200, blank=True, null=True)
def __unicode__(self):
return self.title
In my template, I'm using an ajax search field to query the Article models and return the results in the same page. Essentially, the ajax fires off a HttpPost request containing the search text to the view. In the view I want to get all Article object's whose abstract field contains the search text sent via HttpPost. In my view, I'm getting the search text and then attempting to get the models like
search_text = request.POST['search_text']
articles = SearchQuerySet().filter(abstract=search_text)
but it doesn't return any results. If I call
articles = SearchQuerySet().all()
it will return the 12 model objects in the local test DB. However, the filter function doesn't return any results. What I'm looking to do is the equivalent of
articles= Article.objects.filter(abstract__contains=search_text)
Any suggestions? Thank you
After some digging, I updated my index class to look like this:
There's something wrong with using .filter() on attributes of type indexes.CharField in django-haystack 2.1.0. Maybe somebody can provide more detail, but this is what works for me.