# Elasticsearch java API client trackTotalHits

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-api-client-tracktotalhits/341935>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 30, 2023, 3:41am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-tracktotalhits/341935 "2023-08-30T03:41:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hld942614](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hld942614/32/124643_2.png) [@hld942614](https://discuss.elastic.co/u/hld942614)\
**Post date:** [August 30, 2023, 3:41am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-tracktotalhits/341935/1 "2023-08-30T03:41:11Z")

</div>

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?

```auto
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(?)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 30, 2023, 4:30am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-tracktotalhits/341935/2 "2023-08-30T04:30:56Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html) 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](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after) feature to do deep pagination.
- the [Scroll API](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#scroll-search-results) if you want to extract a resultset to be consumed by another tool later. (Not recommended anymore)

---

<div class="post-metadata">

**Author:** ![hld942614](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hld942614/32/124643_2.png) [@hld942614](https://discuss.elastic.co/u/hld942614)\
**Post date:** [August 30, 2023, 5:46am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-tracktotalhits/341935/3 "2023-08-30T05:46:06Z")

</div>

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

---

<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:** [September 27, 2023, 5:46am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-tracktotalhits/341935/4 "2023-09-27T05:46:55Z")

</div>

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