简体   繁体   中英

Elasticsearch bool query must match single field single term, and another field with OR terms

I want to find a document with a name that contains 'Bob' and has a location that is in either 'paducah' or 'smyrna'.

Here's what I have now:

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

I know the problem is in the location array, because if i change it to a single element with no array the query works just fine.

This is the closest answer i could find .

It didn't work, i receive the following error:

[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

You could try this query:

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

What about the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM