NgramField根据查询项的子字符串返回resutls [英] NgramField returning resutls based on substring of the query term

查看:153
本文介绍了NgramField根据查询项的子字符串返回resutls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仓库模型,其索引如下

 类WarehouseIndex(SearchIndex,可索引):
$
SearchIndex存储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 确切的但都返回相同的结果?



编辑:索引映射索引设置



如何避免这种情况,并提供仅限于 pondicherry

解决方案

正如@Val在上面的答案中所说的那样,错误是因为search_analyzer和indexed_analyzer是一样的,导致了这个问题, >

我们都知道 haystack 在设置基本弹性搜索配置方面非常不灵活,我all elasticstack ,而在我的setting.py中,将后端更改为 elasticsearch_backend 作为建议,并另外添加以下2个配置

 #elasticslack设置
ELASTICSEARCH_DEFAULT_ANALYZER ='雪球'
ELASTICSEARCH_DEFAULT_NGRAM_SEARCH_ANALYZER ='标准'

这似乎解决了我的问题。 >

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?

解决方案

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.

这篇关于NgramField根据查询项的子字符串返回resutls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆