[英]How to exclude unrelated search results from django-haystack and Elasticsearch?
I'm trying to search for products based on their keywords. 我正在尝试根据关键字搜索产品。 My
haystack
index is as follows: 我的
haystack
索引如下:
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
name= indexes.CharField(model_attr='name')
text = indexes.CharField(document=True) # contains keywords associated with the product
In this case, the field "text" contains a group of keywords associated with the product. 在这种情况下,字段“文本”包含一组与产品关联的关键字。 For instance, here is a sample product index:
例如,这是一个示例产品索引:
name: "Tide Detergent"
text: "laundry household shopping cleaning supplies"
When I search for laundry
, Tide Detergent
appears on the search, but so do other irrelevant results, such as products that have lawn
or laugh
in the text
. 当我搜索
laundry
,搜索中会出现“ Tide Detergent
”,但其他不相关的结果Tide Detergent
出现,例如在text
中有lawn
或laugh
产品。 So it looks like elasticsearch
is searching not just for laundry
but variations of the word also. 因此,看起来
elasticsearch
在搜索laundry
,还在搜索单词的变体。
Here is what my search query looks like: 我的搜索查询如下所示:
qs = SearchQuerySet().models(Product).filter(content__exact='laundry')
My question is: how can I force haystack
or elasticsearch
to search strictly for my input keyword and ignore variations of them? 我的问题是:如何强制
haystack
或elasticsearch
严格搜索我的输入关键字,而忽略它们的变体? In other words, how can ensure that haystack searches for only laundry
and exclude any other terms? 换句话说,如何确保干草堆仅搜索
laundry
并排除任何其他术语?
I found an answer. 我找到了答案。 I used a raw
elasticsearch
query instead of going directly through haystack
. 我使用了原始的
elasticsearch
查询,而不是直接通过haystack
。 In that query, I used a constant_score
query which will search for exact terms, not fuzzy terms in addition. 在该查询中,我使用了
constant_score
查询,该查询将搜索精确术语,而不是模糊术语。
{
"query":{
"query":{
"constant_score":{
"filter":{
"bool":{
"must":[
{"bool":{
"must":[
{
"term": {
"text":"laundry"
}
}
]
}
}
]
}
}
}
}
}
}
Here's more info about the constant_score
query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html 以下是有关
constant_score
查询的更多信息: https : //www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.