具有多个 or 条件的弹性搜索的匹配查询

Roh*_*eer 0 elasticsearch

我有三个字段statustype并且searchstatus我想要的是搜索包含等于NEWstatus等于IN PROGRESStype等于abctype等于xyzsearch包含(部分匹配)的数据。

我的电话如下所示 -

{
 "query": {
  "bool" : {
      "must" : [{
                "match": {
                "status": {
                    "query": "abc",
                    }
                }
                }, {
                    "match": {
                    "type": {
                        "query": "NEW",
                        }
                    }
                },{
                    "query_string": {
                        "query": "*abc*",    /* for partial search */
                        "fields": ["title", "name"]
                    }
                }]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Den*_*Ich 5

嵌套你的布尔查询。我认为你缺少的是:

"bool": { "should": [ 
   { "match": { "status": "abc" } }, 
   { "match": { "status": "xyz" } } 
]}
Run Code Online (Sandbox Code Playgroud)

这是一个必须匹配 should 子句之一的查询,因为只给出了 should 子句。

编辑解释差异:

{
   "query": {
      "bool": {
         "must": [
            {
               "bool": {
                  "should": [
                     {
                        "match": {
                           "status": "abc"
                        }
                     },
                     {
                        "match": {
                           "status": "xyz"
                        }
                     }
                  ]
               }
            },
            {
               "terms": {
                  "type": [
                     "NEW",
                     "IN_PROGRESS"
                  ]
               }
            },
            {
               "query_string": {
                  "query": "*abc*",
                  "fields": [
                     "title",
                     "name"
                  ]
               }
            }
         ]
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

所以你在顶部有一个 boolquery。3 个内部查询中的每一个都必须为真。

  1. 第一个是嵌套 boolquery,如果 status 匹配 abc 或 xyz,则该查询为 true。
  2. 如果类型完全匹配 NEW 或 IN_PROGRESS,则第二个为 true - 请注意此处的差异。第一个也将匹配 ABC 或 aBc 或可能匹配“abc XYZ”,具体取决于您的分析器。您可能想要两者的条款。
  3. 第三个是你以前拥有的。