我有一个仓库模型,它的索引如下
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
在我的 shell 中,我正在运行以下sqs查询。
>>> sqs = SearchQuerySet().models(WareHouse)
>>> sqs.filter(customers=3).filter(search_auto='pondicherry')
这将返回结果,其中包含没有确切术语
pondicherry
的结果,它还为我提供了一些与ich
,che
,ndi
等类似的术语匹配的结果。我什至尝试使用
__exact
和Exact
,但都返回相同的结果?编辑:Index mapping,Index Setting
如何避免这种情况,仅提供
pondicherry
字词的结果? 最佳答案
似乎与此open issue有关
这是因为您的search_auto
ngram字段具有相同的索引和搜索分析器,因此您的搜索字词pondicherry
在搜索时也会得到ngram。解决此问题的唯一方法是为search_analyzer
字段设置不同的search_auto
,standard
非常合适。
您可以使用以下方法更改search_auto
字段映射:
curl -XPUT localhost:9200/haystack/_mapping/modelresult -d '{
"properties": {
"search_auto": {
"type": "string",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
}
}
}'