riccardo.cardin
riccardo.cardin

Reputation: 8353

Using term or terms with one value in Elasticsearch queries

I am querying an Elasticsearch index using the values of a field. Sometimes, I have to extract all the documents having a field set to exactly one value; Some other times I have to retrieve all the documents having a field, set with one of the values in a list of values.

The latter use case contains the former. Can I use a single query using the terms construct?

POST /_search
{
    "query": {
        "terms" : { "user" : ["kimchy", "elasticsearch"]}
    }
}

Or, in cases I know I need to search only for a unique value, it is better to use the term construct?

POST _search
{
    "query": {
        "term" : { "user" : "kimchy" } 
    }
}

Which approach is better regarding performance? Does Elasticsearch perform any optimization if the value in the terms construct is unique?

Thanks to all.

Upvotes: 3

Views: 652

Answers (1)

Phenomenal One
Phenomenal One

Reputation: 2587

See this link. Terms query is automatically cached while term query is not . So, the next you run the same query, the took time for query for execution will be faster. So if you have a case where you need to run the same query again and again, terms query is a good choice. If not, there is not much of difference between the two.

Upvotes: 1

Related Questions