# \`existQuery\` Elasticsearch 2.0 Java API usage

**URL:** <https://discuss.elastic.co/t/existquery-elasticsearch-2-0-java-api-usage/35123>\
**Category:** Elasticsearch\
**Created:** [November 20, 2015, 7:48am UTC](https://discuss.elastic.co/t/existquery-elasticsearch-2-0-java-api-usage/35123 "2015-11-20T07:48:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arinto\_Murdopo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arinto_murdopo/32/1779_2.png) [@Arinto\_Murdopo](https://discuss.elastic.co/u/Arinto_Murdopo)\
**Post date:** [November 20, 2015, 7:48am UTC](https://discuss.elastic.co/t/existquery-elasticsearch-2-0-java-api-usage/35123/1 "2015-11-20T07:48:36Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-exists-query.html))  
The code snippet will be something like this:

```auto
        //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:

```auto
        QueryBuilder locationExist = QueryBuilders.existsQuery("location");
        
        SearchRequestBuilder srb = client.prepareSearch(index).setTypes(type)
                .setSearchType(SearchType.SCAN)
                .setQuery(locationExist)
                .setSize(100);

```

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 20, 2015, 11:17am UTC](https://discuss.elastic.co/t/existquery-elasticsearch-2-0-java-api-usage/35123/2 "2015-11-20T11:17:46Z")

</div>

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](https://github.com/elastic/sense) by setting the `explain` parameter to true:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Arinto\_Murdopo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arinto_murdopo/32/1779_2.png) [@Arinto\_Murdopo](https://discuss.elastic.co/u/Arinto_Murdopo)\
**Post date:** [November 23, 2015, 5:35am UTC](https://discuss.elastic.co/t/existquery-elasticsearch-2-0-java-api-usage/35123/3 "2015-11-23T05:35:55Z")

</div>

Thanks for the answer!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:36pm UTC](https://discuss.elastic.co/t/existquery-elasticsearch-2-0-java-api-usage/35123/4 "2017-07-05T23:36:41Z")

</div>


