Joey Yi Zhao
Joey Yi Zhao

Reputation: 42556

What is the difference between `constant_score + filter` and `term` query?

I have two queries in Elasticsearch:

{
    "term" : {
        "price" : 20
    }
}

and

"constant_score" : { 
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }

They are returning the same query result. I wonder what the main difference between them. I read some articles about scoring document. And I believe both queries are scoring document. The constant_score will use default score 1.0 to match the document's score. So I don't see much difference between these two.

Upvotes: 0

Views: 246

Answers (1)

Val
Val

Reputation: 217434

The results would be exactly the exact.

However, the biggest difference is that the constant_score/filter version will cache the results of the term query since it's run in a filter context. All future executions will leverage that cache. Also, one feature of the constant_score query is that the returned score is always equal to the given boost value (which defaults to 1)

The first query will be run outside of the filter context and hence not benefit from the filter cache.

Upvotes: 1

Related Questions