Elasticsearch bool查询必须匹配单个字段单个术语,而另一个字段具有OR术语

时间:2017-07-23 15:13:15

标签: elasticsearch

我想找到一个名称的文档,其中包含' Bob'并且有一个位置,位于' paducah'或者'士麦那'。

这就是我现在所拥有的:

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

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

This is the closest answer i could find

它没有用,我收到以下错误:

  

[term]格式错误的查询,预计[END_OBJECT]但找到[FIELD_NAME]

2 个答案:

答案 0 :(得分:2)

您可以尝试此查询:

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

答案 1 :(得分:0)

以下内容如何:

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