# Different search result between REST API and Java Transport Client

**URL:** <https://discuss.elastic.co/t/different-search-result-between-rest-api-and-java-transport-client/67719>\
**Category:** Elasticsearch\
**Created:** [December 1, 2016, 6:57am UTC](https://discuss.elastic.co/t/different-search-result-between-rest-api-and-java-transport-client/67719 "2016-12-01T06:57:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluish\_penguin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bluish_penguin/32/13769_2.png) [@bluish\_penguin](https://discuss.elastic.co/u/bluish_penguin)\
**Post date:** [December 1, 2016, 6:57am UTC](https://discuss.elastic.co/t/different-search-result-between-rest-api-and-java-transport-client/67719/1 "2016-12-01T06:57:13Z")

</div>

Hi,  
I would like to know how to query the same result by using Java transport client.

In Elasticsearch Head:  
I fire the query below through POST API \_search:  
{  
"query": {},  
"from": 100,  
"size": 20,  
"sort": [  
{  
"date\_of\_post": {  
"order": "asc"  
}  
}  
]  
}  
It returns result with total hits = 102953. See attachment.  
 ![](https://us1.discourse-cdn.com/elastic/original/2X/b/b0b5f3727cabc05d1792688988f064fa619d583c.JPG)

In Java code, I code as below:

```
  response = client.prepareSearch(indexName)
      .setQuery(null)
      .setFrom(9000)
      .setSize(20)
      .addSort("date_of_post", SortOrder.ASC)
      .execute()
      .actionGet();

```

It return zero hits (hits = null),  
with totalHits = 200 (always equals to 200).

Why is it different from the REST API call? What have I done wrong?

Thank you.

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [December 1, 2016, 10:45am UTC](https://discuss.elastic.co/t/different-search-result-between-rest-api-and-java-transport-client/67719/2 "2016-12-01T10:45:33Z")

</div>

The difference is obvious: you set an index name `indexName` (which is not shown in your question) and you use a strange "null" query. Use a correct index name setting or drop it. Do not use `setQuery(null)`, instead drop `setQuery(null)` at all or use a sound query method.

Example

```auto
 client.prepareSearch()
    .setFrom(100).setSize(20).addSort("date_of_post", SortOrder.ASC)
    .execute().actionGet()

```

If you want an equivalent of the strange query syntax of`"query":{}`, try a match all query

```auto
 client.prepareSearch()
    .setQuery(QueryBuilders.matchAllQuery())
    .setFrom(100).setSize(20).addSort("date_of_post", SortOrder.ASC)
    .execute().actionGet()

```

---

<div class="post-metadata">

**Author:** ![bluish\_penguin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bluish_penguin/32/13769_2.png) [@bluish\_penguin](https://discuss.elastic.co/u/bluish_penguin)\
**Post date:** [December 2, 2016, 2:21am UTC](https://discuss.elastic.co/t/different-search-result-between-rest-api-and-java-transport-client/67719/3 "2016-12-02T02:21:52Z")

</div>

Thanks. I will try again.

---

<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:** [December 30, 2016, 2:22am UTC](https://discuss.elastic.co/t/different-search-result-between-rest-api-and-java-transport-client/67719/4 "2016-12-30T02:22:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
