Soheil
Soheil

Reputation: 5327

How does ElasticSearch rank filter queries (rather than text queries)?

I know that ElasticSearch uses relevance ranking algorithms such as Lucene's tf/idf, length normalization and couple of more algorithms to rank term queries applied on textual fields (i.e. searching words "medical" AND "journal" in the "title" and "body" fields).

My question is how does ElasticSearch rank and retrieve results of a filter or range query (i.e. age=25, or weight>60)?

I know these types of queries are just filtering documents based on the condition(s). But lets say I have 200 documents which their age field value is 25. Which of those documents will be retrieved as top 10 results?

Does ElasticSearch retrieve them by the order it indexed them?

Upvotes: 1

Views: 968

Answers (1)

Olly Cruickshank
Olly Cruickshank

Reputation: 6180

From the Elasticsearch documentation:

Filters: As a general rule, filters should be used instead of queries:

  • for binary yes/no searches
  • for queries on exact values

Queries: As a general rule, queries should be used instead of filters:

  • for full text search
  • where the result depends on a relevance score

So when running a search such as "age=25, or weight>60" you should be using a filter.

However - Filters do not affect the scoring - i.e. if you only used a filter your search results would all have the same score.

There is a range query - this is a query that would affect score and I would guess that it scores documents based on things like the document timestamp (most recent gets a higher score).

You'd need to explore the documentation further and dig into Lucene documentation to understand exactly how and why the a document got its score - but as above, you may be better using Filters that don't affect scoring.

Upvotes: 4

Related Questions