2

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"
}

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This looks good... are there any special characters I have to escape inside the quotes besides other quotes? Or just put no quotes but escape everything special including the space?
You'll need to escape the special characters that Lucene lists here:lucene.apache.org/core/2_9_4/queryparsersyntax.html#Escaping Special Characters
0

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?

2 Comments

Does it analyze the terms?
The terms query doesn't analyze terms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.