`existQuery` Elasticsearch 2.0 Java API usage


(Arinto Murdopo) #1

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);

(Christoph) #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.


(Arinto Murdopo) #3

Thanks for the answer!


(system) #4