I want to use existQuery in my Java code. We're using Elasticsearch 2.0.
Should I wrap the query as a filter using constantScoreQuery? (as shown by the Query DSL documentation )
The code snippet will be something like this:
//instantiate Elasticsearch client
Client client = .....; //code omitted for brevity
QueryBuilder locationExist = QueryBuilders
.constantScoreQuery(QueryBuilders.existsQuery("location"));
SearchRequestBuilder srb = client.prepareSearch(index).setTypes(type)
.setSearchType(SearchType.SCAN)
.setQuery(locationExist)
.setSize(100);
Or, can I use it directly? As shown in code snippet below:
QueryBuilder locationExist = QueryBuilders.existsQuery("location");
SearchRequestBuilder srb = client.prepareSearch(index).setTypes(type)
.setSearchType(SearchType.SCAN)
.setQuery(locationExist)
.setSize(100);
cbuescher
(Christoph)
November 20, 2015, 11:17am
2
Hi,
it shouldn't really matter, unless you want to give the exists-query results a score other than 1.0. In both versions the exists-query is internally wrapped in a ConstantScore query in lucene. You can check this for yourself using the "_validate" endpoint e.g. with Sense by setting the explain parameter to true:
GET /test/type/_validate/query?explain=true
{
"query": {
"exists" : {
"field" : "location"
}
}
}
=>
"explanations": [
{
"index": "test",
"valid": true,
"explanation": "+ConstantScore(_field_names:location) #QueryWrapperFilter(+ConstantScore(_type:type))"
}
]
And for the other version (just using a different boost of 1.5 to show where it gets applied:
GET /test/type/_validate/query?explain=true
{
"query": {
"constant_score": {
"filter": {
"exists": {
"field": "location"
}
},
"boost": 1.5
}
}
}
=>
"explanations": [
{
"index": "test",
"valid": true,
"explanation": "+ConstantScore(ConstantScore(_field_names:location))^1.5 #QueryWrapperFilter(+ConstantScore(_type:type))"
}
]
So in terms of performance it should not make a difference what you use and if you don't need special scoring you can use the short version.
1 Like