Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch: How to use two different multiple matching fields?

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 following:

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

However, I get this error:

[bool] filter does not support [minimum_should_match];

Is there another way to go about what I'm attempting to do, or am I on the right track? Or is this just not possible in elasticsearch?

like image 786
beth Avatar asked Sep 24 '13 19:09

beth


1 Answers

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
          }
        }
      ]
    }
  }
}
like image 192
kielni Avatar answered Nov 20 '22 17:11

kielni