AndreKR
AndreKR

Reputation: 33707

Match query with multiple values

I have a list of values and I want all documents that have any of these values in their product_code field.

I tried this, but even though it doesn't give an error, it only gives me one of the documents:

"query": {
  "match": {
    "product_code": {
      "query": ["ABC 4", "ABC 5"]
    }
  }
}

So I'm basically looking for the functionality of the terms filter, but with analysis.

Of course I could do:

"bool": {
  "should": [
    {
      "query": {
        "match": {
          "product_code": "ABC 4"
        }
      }
    },
    {
      "query": {
        "match": {
          "product_code": "ABC 5"
        }
      }
    }
  ]
}

but this gets rather verbose for long lists.

product_code is defined like this:

"product_code": {
  "type": "string",
  "analyzer": "product_code",
}

with the product_code analyzer:

"product_code": {
  "type": "custom",
  "filter": [
    "lowercase",
    "trim"
  ],
  "tokenizer": "keyword"
}

Upvotes: 2

Views: 990

Answers (2)

Dan Tuffery
Dan Tuffery

Reputation: 5924

There isn't an equivalent of the terms query for match AFAIK. Another option is the query_string which is less verbose:

{
  "query": {
    "query_string": {
      "default_field": "product_code",
      "query": "\"ABC 4\" OR \"ABC 5\""
    }
  }
}

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

Upvotes: 1

Dror
Dror

Reputation: 13081

I might be missing something, but how about:

{
    "constant_score" : {
        "filter" : {
            "terms" : { "product_code" : ["ABC 4", "ABC 5"]}
        }
    }
}

Could this be what you're looking for?

Upvotes: 0

Related Questions