Different search result between REST API and Java Transport Client

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.

In Java code, I code as below:

  response = client.prepareSearch(indexName)
      .addSort("date_of_post", SortOrder.ASC)

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.

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.


    .setFrom(100).setSize(20).addSort("date_of_post", SortOrder.ASC)

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

    .setFrom(100).setSize(20).addSort("date_of_post", SortOrder.ASC)
1 Like

Thanks. I will try again.

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