gpt4 book ai didi

elasticsearch - 在ElasticSearch中如何过滤某些单词的结果集?

转载 作者:行者123 更新时间:2023-12-03 01:46:51 24 4
gpt4 key购买 nike

我是Elasticsearch的新手。我建立了一个包含不同电子产品及其附件的数据库。我试图从该数据库中使用项目名称搜索所有笔记本电脑,笔记本电脑和计算机。但是,它还返回一些配件,例如笔记本电脑背包或笔记本电脑 shell 等。我在查询中使用must_not子句,但它并不能真正消除不必要的结果。有人可以建议我做错了什么吗?还是应该改善下面列出的查询?

30 res = es.search( index=Index, body={
31 "query": {
32 "filtered": {
33 "query": {
34 "match_all": {}
35 },
36 "filter": {
37 "bool": {
38 "must": [
39 {
40 "terms": {
41 "name": [
42 "laptop","notebook","computer"
43 ],
44 "execution" : "or"
45 }
46 }
47 ],
48 "must_not": [
49 {
50 "term": {
51 "name": "Backpack"
52 }
53 }
54 ]
55 }
56 }
57 }
58 }
59
60 })

提前致谢。
请注意,我正在尝试使用python和elasticsearch python库。

最佳答案

当您将文档放入索引时,ElasticSearch会将分析器应用于原始值,该值会将您的输入分成多个词项,并对其应用一些过滤器。似乎默认分析器也将lowercase token 过滤器应用于您的输入,因此结果 token 使用小写字母。您可以尝试像这样使用带有小写字母的查询

{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"terms": {
"name": [
"laptop",
"notebook",
"computer"
],
"execution": "or"
}
}
],
"must_not": [
{
"term": {
"name": "backpack"
}
}
]
}
}
}
}
}

或者您可以尝试使用 match查询代替 term查询
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"terms": {
"name": [
"laptop",
"notebook",
"computer"
],
"execution": "or"
}
}
],
"must_not": [
{
"match": {
"name": "Backpack"
}
}
]
}
}
}
}
}

第二个选项可能会稍微慢一些,但是如果您的关键字不是标记词,这将为其他输入提供预期结果

关于elasticsearch - 在ElasticSearch中如何过滤某些单词的结果集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43216426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com