abisson
abisson

Reputation: 4425

Haystack __contains not finding substrings passed through GET [ElasticSearch backeng]

I have the following model:

class ProductIndex(CelerySearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    number = indexes.CharField()
    description = indexes.CharField(model_attr='description')

In my webpage, I perform an AJAX call to a function that should return to me the products that have their description containing some words the used types. For example, if I have a product with a description "Apple Macbook Pro", and the user types "book", I want to return that result.

I perform the following:

q = request.GET['q']
results = SearchQuerySet().models(Product).filter(description__contains=q)

However, like I said, I do have for example a product with the description:

Macbook and the user types book, it will not work. However, Macbook will work.

Finally, if I print q, it returns u'book'.

Upvotes: 0

Views: 654

Answers (1)

Stephen Paulger
Stephen Paulger

Reputation: 5343

You could use indexes.EdgeNgramField instead of indexes.CharField. This is suggested in the autocomplete documentation (http://django-haystack.readthedocs.org/en/v2.0.0/autocomplete.html).

You shouldn't need to change the querying code.

EDIT:

To make it case insensitive you could do something like this

class ProductIndex(CelerySearchIndex, indexes.Indexable):
    ... fields here ...
    def prepare_description(self, object):
        return object.description.lower()

and when querying use lower() on q

Upvotes: 5

Related Questions