gpt4 book ai didi

elasticsearch - Elasticsearch 如何使query_string匹配精确短语

转载 作者:行者123 更新时间:2023-12-02 22:16:28 25 4
gpt4 key购买 nike

我需要 query_string 仅在完全相同时才匹配。

根据弹性documentation 关于查询字符串查询:

空格不被视为运算符,这意味着纽约市将“按原样”传递给为该字段配置的分析器。如果该字段是关键字字段,则分析器将创建单个术语 new york city,查询构建器将在查询中使用该术语。如果您想单独查询每个术语,您需要在这些术语周围添加显式运算符(例如,new AND york AND city)。

我创建了一个索引 testingindex 并添加了随机数据:

  • 版纳夫
  • cd 测试 af
  • 测试光盘
  • 电视
  • 测试AB

帖子:

POST testingindex/_doc/5
{
"name":"banna af"
}

搜索:

GET testingindex/_search?explain
{
"size": 10,
"query": {
"bool": {
"must": [
{
"query_string": {
"fuzziness": 0,
"phrase_slop": 0,
"default_operator": "OR",
"minimum_should_match": "99%",
"fields": [
"name"
],
"query":"(testing af) OR (banna af)"
}
}
]
}
}
}

结果:

"hits" : [
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "6",
"_score" : 2.0794415,
"_source" : {
"name" : "banna af"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.8630463,
"_source" : {
"name" : "cd testing af"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.6931472,
"_source" : {
"name" : "testing cd"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "5",
"_score" : 0.5753642,
"_source" : {
"name" : "af television"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "testing ab"
}
}
]

如果我将运算符更改为:

"default_operator": "AND",

我得到了正确的结果。

但是如果我将查询更改为:

    "query":"(testing af) OR (banna af) OR (badfadfaf)"

我没有得到任何结果,我仍然需要返回匹配的结果。

如何让 cd testing afbanna af 成为唯一返回的结果?

最佳答案

只需将术语本身用双引号括起来(您必须将其转义)以获得完全匹配并删除 minimum_should_match 属性 - 简化的查询如下所示:

GET testingindex/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"name"
],
"query":"(\"testing af\") OR (\"banna af\") OR (\"badfadfaf\")"
}
}
]
}
}
}

产量:

"hits" : {
"total" : 2,
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "qmD-EWoBqkB-aMRpwfuE",
"_score" : 1.3862944,
"_source" : {
"name" : "banna af"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "q2D_EWoBqkB-aMRpFPtX",
"_score" : 0.5753642,
"_source" : {
"name" : "cd testing af"
}
}
]
}

关于elasticsearch - Elasticsearch 如何使query_string匹配精确短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55646622/

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