1

I have following match query string:

curl -XGET 'my-es.com/my_indice/_search?pretty' -d '{
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [ {
        "match" : {
          "state" : {
            "query" : ["ACTIVE", "INACTIVE"],
            "type" : "boolean"
          }
        }
      }]
    }
  }
}'

I guess it means "state" = "ACTIVE" or "state" = "INACTIVE", but actually it performs "state" = "INACTIVE".

Then I tried term query string:

curl -XGET 'my-es.com/my_indice/_search?pretty' -d '{
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [{
         "terms" : { "state" : ["ACTIVE", "INACTIVE"] }
      }]
    }
  }
}'

It performs "state" = "ACTIVE" or "state" = "INACTIVE", showing term query supports multiple OR conditions via an array.

I'm curious about why match query does not support OR condition via an array? And it does not show any syntax error.

coderz
  • 4,847
  • 11
  • 47
  • 70

1 Answers1

2

The match query only supports a single string value to be specified. It is not explicitly specified in the official match documentation, but if you're willing to read some source code for MatchQueryParser.java, you can see that while parsing the query field, the parser will skip the tokens delimiting the start and end of the array and always override the value with the latest one being parsed, hence why you're seeing what you see, i.e. that state will be matched with INACTIVE only.

What you can do, however, is to put both tokens inside the same string like below, and both tokens will be taken into account:

curl -XGET 'my-es.com/my_indice/_search?pretty' -d '{
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [ {
        "match" : {
          "state" : {
            "query" : "ACTIVE INACTIVE",
            "type" : "boolean"
          }
        }
      }]
    }
  }
}'
Val
  • 207,596
  • 13
  • 358
  • 360
  • Great answer! While if my query condition is `"name" = "nike inc" or "name" = "samsung inc"`, then what the query will be like? I think `"query" : "ACTIVE INACTIVE"` only handles single word case. – coderz Dec 29 '15 at 23:36