Different search result between REST API and Java Transport Client


(Bluish Penguin) #1

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.

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.


(Jörg Prante) #2

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

 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

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

(Bluish Penguin) #3

Thanks. I will try again.


(system) #4

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