弹性搜索中使用通配符的部分搜索

2024-09-28 23:31:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在弹性搜索中使用通配符搜索数组值

enter image description here

{
    "query": {
        "wildcard": {
            "short_message": {
                "value": "*nne*",
                "boost": 1.0,
                "rewrite": "constant_score"
            }
        }
    }
}

我正在搜索“短消息”,它对我有用。 但我想搜索“messages.message”,它不起作用

{
    "query": {
        "wildcard": {
            "messages.message": {
                "value": "*nne*",
                "boost": 1.0,
                "rewrite": "constant_score"
            }
        }
    }
}

我还想搜索数组中的多个字段

例如:- 字段:[“messages.message”、“messages.subject”、“messages.email\u search”]

那就有可能给我最好的解决方案

提前谢谢


Tags: messagevalue数组query弹性wildcardshortscore
1条回答
网友
1楼 · 发布于 2024-09-28 23:31:07

似乎您正在为messages使用^{}数据类型

为此,您需要使用^{}

POST <your_index_name>/_search
{
  "query": {
    "nested": {
      "path": "messages",
      "query": {
        "wildcard": {
          "messages.message": {
            "value": "*nne*",
            "boost": 1
          }
        }
      }
    }
  }
}

对于多字段查询,您可能可以使用query_string进行查询,因此基本上您的解决方案是在nested query中使用query_string

查询字符串:

POST <your_index_name>/_search
{
  "query": {
    "nested": {
      "path": "messages",
      "query": {
        "query_string": {
          "fields": ["messages.message", "messages.subject"],
          "query": "*nne*",
          "boost": 1
        }
      }
    }
  }
}

查询DSL

您也可以使用wildcard使用Query DSL使用wildcard,但同样,您需要为每个字段添加多个查询子句,出于性能原因,我怀疑通配符查询不支持多字段查询

POST <your_index_name>/_search
{
  "query": {
    "nested": {
      "path": "messages",
      "query": {
        "bool": {
          "should": [
            {
              "wildcard": { 
                "messages.message": {
                  "value": "*nne*",
                  "boost": 1
                }
              }
            },
            {
              "wildcard": {
                "messages.subject": {
                  "value": "*nne*",
                  "boost": 1
                }
              }
            }
          ]
        }
      }
    }
  }
}

请注意,通配符搜索是不可取的,因为它必须执行大量的正则表达式操作,并且会影响获得响应的延迟。相反,我建议您查看Ngram Tokenizer,这样您就可以使用简单的匹配查询来获得所需的结果

让我知道这是否有帮助

相关问题 更多 >