【问题标题】:Elasticsearch bool query must match single field single term, and another field with OR termsElasticsearch bool 查询必须匹配单个字段单个术语,以及另一个字段与 OR 术语
【发布时间】:2017-12-29 05:12:28
【问题描述】:

我想查找一个 name 包含“Bob”并且有一个位于“paducah”或“smyrna”的位置的文档。

这是我现在拥有的:

query: {
  bool: {
    must: [
      { match: { name: 'bob' } }, 
      { match: { location: ['paducah', 'smyrna'] } }
    ],
  },
},

我知道问题出在 location 数组中,因为如果我将其更改为没有数组的单个元素,查询就可以正常工作。

This is the closest answer i could find.

它不起作用,我收到以下错误:

[term] 格式错误的查询,应为 [END_OBJECT],但找到了 [FIELD_NAME]

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    下面的呢:

    {
      "query": {
        "bool": {
          "must": [
            { "term": { "name": "bob" }, 
            "bool": {
              "should": [
                {"term": {"location": "paducah"}},
                {"term": {"location": "smyrna"}}
              ]
            }
            }
          ]
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试这个查询:

      {
          "query": {
            "bool": {
              "must": [
                { "match": { "name": "bob" } }
              ],
              "should": [
                  { "match": { "location": "paducah" }},
                  { "match": { "location": "smyrna"   }}
                ],
                "minimum_should_match": 1
            }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 2022-12-10
        • 1970-01-01
        • 2021-05-03
        • 1970-01-01
        • 1970-01-01
        • 2014-05-12
        相关资源
        最近更新 更多