Jippe
Jippe

Reputation: 714

How to wrap an ElasticSearch filter in a query

In order to use the percolator in ElasticSearch, I need to index search queries. However, we mainly use filters for searching. In order to index these filters, they have to be wrapped inside a query.

I know of two different approaches to accomplish this. Wrap the filter in a filtered query:

{
  "query": {
    "filtered": {
      "filter": { ... }
    }
  }
}

or using a constant_score query:

{
  "query": {
    "constant_score": {
      "filter": { ... }
    }
  }
}

Which method is preferred? Why?

Upvotes: 4

Views: 477

Answers (2)

travelingbones
travelingbones

Reputation: 8408

According to this link, "A filtered query with a match_all query is automatically converted internally to a constant_score one". So sounds like the best option is to use constant_score and omit an internal step.

Upvotes: 0

Alex Brasetvik
Alex Brasetvik

Reputation: 11744

While both will yield the same results, and should have very close performance, I'd prefer using the filtered-query. It communicates the intention quite clearly. A few months down the road you may be starting at the same query and wonder why you cared about scores. :)

Upvotes: 2

Related Questions