[Answer]-Matching only some words in query using Django Haystack

1👍

Use EdgeNgramField for example in your search index file:

class AppIndex(indexes.SearchIndex, indexes.Indexable):

    ngram_text = indexes.EdgeNgramField()

    def prepare(self, obj):
        """Add the content of text field from final prepared data into ngram_text field
        """
        prepared_data = super(AppIndex, self).prepare(obj)
        prepared_data['ngram_text'] = prepared_data['text']
        return prepared_data

Then query on that field (Make sure to first do rebuild_index and update_index after doing above changes):

products = SearchQuerySet().models(Product).filter(ngram_text=q)

Leave a comment