Search code examples
djangoelasticsearchdjango-haystack

NgramField returning resutls based on substring of the query term


I have a Warehouse Model which is getting index as follows

class WarehouseIndex(SearchIndex, Indexable):
    """
    SearchIndex Class that stored indexes for Model Warehouse
    """
    text = CharField(document=True, use_template=True)
    search_auto = NgramField()
    ....

   def get_model(self):
        return WareHouse

In my shell I am running the following sqs query.

>>> sqs = SearchQuerySet().models(WareHouse)
>>> sqs.filter(customers=3).filter(search_auto='pondicherry')

This returns result consisting of results that do not have exact term pondicherry it also provides me some results that match terms like ich, che, ndi, etc.

I have even tried using __exact and Exact but all return the same result?

EDIT: Index mapping, Index Setting

How can I avoid this and provide result only of term pondicherry?


Solution

  • As @Val has stated in the above answer, the error was because search_analyzer and indexed_analyzer are same which caused the issue,

    As we all know haystack is very inflexible in setting up the basic elasticsearch configuration, I installed elasticstack and in my setting.py changed the backend to it's elasticsearch_backend as suggest and additionally added the following 2 configurations

    # elasticslack setting
    ELASTICSEARCH_DEFAULT_ANALYZER = 'snowball'
    ELASTICSEARCH_DEFAULT_NGRAM_SEARCH_ANALYZER = 'standard'   
    

    this seemed to solve my problem.