Elasticsearch java API client trackTotalHits

I am trying to search my data and expect to get all data,but I can't get more than 10000 result,so I try to use trackTotalHits ,can someone tell me how to use it?

try {
			ElasticsearchClient client = elasticsearchConfig.getClient();
			SearchResponse<Object> searchResponse = null;
			searchResponse = client.search(s -> s.index("my-index")
					.trackTotalHits(?), Object.class);
			for (Hit<Object> hit : searchResponse.hits().hits()) {
				Object data = hit.source();
				Map<?, ?> map = (Map<?, ?>) data;
				JSONObject jsonObject = new JSONObject(map);
				System.out.println(jsonObject );
			}
			client._transport().close();
		} catch (Exception e) {
			e.printStackTrace();
		}

what should I do with .trackTotalHits(?)

1 Like

TrackTotalHits will just give you the exact total number of hits. It's not going to help you to extract all the hits.

But to answer your question, use:

  .trackTotalHits(tth -> tth.enabled(true))

To extract all data, you can use:

  • the size and from parameters to display by default up to 10000 records to your users. If you want to change this limit, you can change index.max_result_window setting but be aware of the consequences (ie memory).
  • the search after feature to do deep pagination.
  • the Scroll API if you want to extract a resultset to be consumed by another tool later. (Not recommended anymore)
1 Like

Thanks for your answer!
But I still have some questions
1.I have tried to set index.max_result_window before,but it seems like if i have some new write in data,it won't have this setting?
2.For "search after" and "Scroll API",is there any document about how to use in Java API client?
because all I can get is how to use in elasticsearch but not in Java

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