问题
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
?
回答1:
It seems to be related to this open issue
This is because your search_auto
ngram field has the same index and search analyzer and hence your search term pondicherry
also gets ngramed at search time. The only way to fix this is to set a different search_analyzer
for your search_auto
field, standard
would be a good fit.
You can change your search_auto
field mapping with this:
curl -XPUT localhost:9200/haystack/_mapping/modelresult -d '{
"properties": {
"search_auto": {
"type": "string",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
}
}
}'
回答2:
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.
来源:https://stackoverflow.com/questions/36542965/ngramfield-returning-resutls-based-on-substring-of-the-query-term