damon
damon

Reputation: 15128

Django Haystack exact filtering

I have a haystack search which has the following SearchIndex:

class GrantIndex(indexes.SearchIndex):
    """
    This provides the search index for the Grant application.
    """
    text = indexes.CharField(document=True, use_template=True)
    year = indexes.IntegerField(model_attr='year__year')
    date = indexes.DateField(model_attr='date')
    program = indexes.CharField(model_attr='program__area')
    grantee = indexes.CharField(model_attr='grantee')
    amount = indexes.IntegerField(model_attr='amount')
site.register(Grant, GrantIndex)

If I want to search filtering out any programs that ARE NOT 'Health', I run the following query:

from haystack.query import SearchQuerySet

sqs = SearchQuerySet()
sqs = sqs.filter(program='Health')

Unfortunately, this also produces objects from the program 'Health\Other' and 'Health\Cardiovascular'. How do I stop the search from allowing those other programs in?

I run Ubuntu 9.10 with Xapian as my search back-end.

Upvotes: 5

Views: 2655

Answers (5)

Jorge Leitao
Jorge Leitao

Reputation: 20123

Disclaimer: I'm the maintainer of Xapian-Haystack.

I believe this happens because Xapian-Haystack was using a term generator that was escaping special characters like /.

So, in your case, "Health\Other" was being indexed as "health" and "other". This was recently fixed in the master branch of Xapian-Haystack, see e.g. here.

Upvotes: 0

ambi
ambi

Reputation: 1316

For solr backend I need to use _exact (just one underline instead of two).

Upvotes: 0

soField
soField

Reputation: 2696

use "prepare_data" for program field and get rid of health\blabla things

Upvotes: 0

Emil Stenström
Emil Stenström

Reputation: 14086

You've problably solved the problem already, but I just stumbled over the same problem with the Whoosh backend. Maybe the Xapian and Whoosh backends behave the same? Seems Whoosh is stemming all CharFields by default, and searching inside them with some kind of contains-query. Switching to a custom backend, without stemming enabled on CharFields, fixed this issue for me.

Hopefully this will push someone else in the right direction.

Upvotes: 3

Dmitry Shevchenko
Dmitry Shevchenko

Reputation: 32404

you can use field lookups as described here.

sqs = sqs.filter(program__exact='Health')

Upvotes: 1

Related Questions