When adding setSize, no records return

my code is as below:

 sr = client.prepareSearch(lastIndex)
                        .setQuery(boolQueryBuilder)
                        .setRouting(elasticRouting)
                        .addSort("startTime", SortOrder.DESC)
                        .execute().actionGet();

the above code will get two hits, but when I add "setSize(1)", as below:

  sr = client.prepareSearch(lastIndex)
                        .setQuery(boolQueryBuilder)
                        .setRouting(elasticRouting)
                        .addSort("startTime", SortOrder.DESC)
                        **.setSize(1)**
                        .execute().actionGet();

I get no hits. I want to limit return results to 1, am I doing it wrong?

Please format your code using </> icon. It will make your post more readable.

My guess is that it is related to routing although it should not be an issue here.

Can you reproduce this behavior with a pure full REST script so we can see if it's a bug?

The weird thing is when I using restful request, the result is normal, it returns exactly one hit.
here is my post request:

POST /service/_search?routing=id

{
	"query": {
		"bool": {
			"filter": {
				"term": {
					"id": "4785SERTDDSSER322"
				}
			},
			"must": {
				"exists": {
					"field": "startTime"
				}
			},
			"should": [{
				"bool": {
					"must_not": {
						"exists": {
							"field": "status"
						}
					}
				}
			},
			{
				"term": {
					"status": ""
				}
			}]
		}
	},
	"sort": {
		"startTime": {
			"order": "desc"
		}
	}, 
    "size": 1
}

So can you not call execute().get() but call toString() and paste the result here in both cases?

I think I figure out the issue. it is because I just insert the data and get it immediately in my test, the data is not sync yet, but after I make it wait 2 seconds, and then get the result again, the result is fine.

Then prefer using refresh feature instead of "waiting" for x seconds.
But please do that only in tests :slight_smile:

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