# Query Results are different Using Java API

**URL:** <https://discuss.elastic.co/t/query-results-are-different-using-java-api/6549>\
**Category:** Elasticsearch\
**Created:** [January 31, 2012, 2:06am UTC](https://discuss.elastic.co/t/query-results-are-different-using-java-api/6549 "2012-01-31T02:06:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prashanth](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@Prashanth](https://discuss.elastic.co/u/Prashanth)\
**Post date:** [January 31, 2012, 2:06am UTC](https://discuss.elastic.co/t/query-results-are-different-using-java-api/6549/1 "2012-01-31T02:06:34Z")

</div>

Hello all,

```
  I have started using elastic search recently. I am facing a

```

problem using Java API for free text search.

```
 If I try using QueryBuilder to build the query, result is what is

```

expected.

```
            SearchResponse response = esClient
			.prepareSearch(getDefaultIndexName(client))
	 		.setSearchType(SearchType.QUERY_THEN_FETCH)
			.setQuery(queryString("freetext")).execute().actionGet(); -> This

```

is working fine, resulting the data properly.

As per our design, jSON will be formed in the UI Layer and in the  
BL layer we need to use the already formed JSON to search elastic  
search.

With HTTP approach, result is same as above.

Query Json used is : {"query":{"query\_string":{"query":"freetext"}}}

But when I use the same JSON in java API, result is different.

Following is the piece of code which is written in java to use json  
query.

SearchResponse response = esClient  
.prepareSearch(getDefaultIndexName(client))  
.setSource(queryJson).execute().actionGet(); // Query Json is  
having above mentioned query.

In this case, the result is different.( Returning all data without  
applying search params)

Please let me know what is being done in a wrong way.

Thanks,  
Prashanth

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 31, 2012, 5:23pm UTC](https://discuss.elastic.co/t/query-results-are-different-using-java-api/6549/2 "2012-01-31T17:23:19Z")

</div>

Yea, its a problem where the source itself gets overwritten, I will fix that, for now, you can use setExtraSource.

On Tuesday, January 31, 2012 at 4:06 AM, Prashanth wrote:

> Hello all,
> 
> I have started using Elasticsearch recently. I am facing a  
> problem using Java API for free text search.
> 
> If I try using QueryBuilder to build the query, result is what is  
> expected.
> 
> SearchResponse response = esClient  
> .prepareSearch(getDefaultIndexName(client))  
> .setSearchType(SearchType.QUERY\_THEN\_FETCH)  
> .setQuery(queryString("freetext")).execute().actionGet(); -\> This  
> is working fine, resulting the data properly.
> 
> As per our design, jSON will be formed in the UI Layer and in the  
> BL layer we need to use the already formed JSON to search elastic  
> search.
> 
> With HTTP approach, result is same as above.
> 
> Query Json used is : {"query":{"query\_string":{"query":"freetext"}}}
> 
> But when I use the same JSON in java API, result is different.
> 
> Following is the piece of code which is written in java to use json  
> query.
> 
> SearchResponse response = esClient  
> .prepareSearch(getDefaultIndexName(client))  
> .setSource(queryJson).execute().actionGet(); // Query Json is  
> having above mentioned query.
> 
> In this case, the result is different.( Returning all data without  
> applying search params)
> 
> Please let me know what is being done in a wrong way.
> 
> Thanks,  
> Prashanth

---

<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 6, 2017, 3:40am UTC](https://discuss.elastic.co/t/query-results-are-different-using-java-api/6549/3 "2017-07-06T03:40:58Z")

</div>


