Elasticsearch: How to use two different multiple matching fields?

后端 未结 2 1104
梦毁少年i
梦毁少年i 2021-02-10 04:15

I want to do something much like the \'and\' filter example except with terms with a \'should\' in each one, instead of the field types in the example. I came up with the follow

2条回答
  •  感情败类
    2021-02-10 04:51

    Each bool query clause can contain multiple clauses. A terms query (http://www.elasticsearch.org/guide/reference/query-dsl/terms-query/) is an easy way to specify that the query should match any of a list of terms. Here's that uses terms queries to say fruit must be one of orange, apple, pear and color must be one of red, yellow, green, in addition to the ids query you had before:

    {
      "query": {
        "bool": {
          "must": [
            {
              "ids": {
                "type": "foo",
                "values": [
                  "fff",
                  "bar",
                  "baz"
                ]
              }
            },
            {
              "terms": {
                "fruit": [ "orange", "apple","pear" ],
                "minimum_should_match": 1
              }
            },
            {
              "terms": {
                "color": [ "red", "yellow", "green" ],
                "minimum_should_match": 1
              }
            }
          ]
        }
      }
    }
    

提交回复
热议问题