# Elasticsearch 2.1.1 -DFS\_QUERY\_THEN\_FETCH - how to get most recent doc

**URL:** <https://discuss.elastic.co/t/elasticsearch-2-1-1-dfs-query-then-fetch-how-to-get-most-recent-doc/163947>\
**Category:** Elasticsearch\
**Created:** [January 11, 2019, 6:49pm UTC](https://discuss.elastic.co/t/elasticsearch-2-1-1-dfs-query-then-fetch-how-to-get-most-recent-doc/163947 "2019-01-11T18:49:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neni](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@Neni](https://discuss.elastic.co/u/Neni)\
**Post date:** [January 11, 2019, 6:49pm UTC](https://discuss.elastic.co/t/elasticsearch-2-1-1-dfs-query-then-fetch-how-to-get-most-recent-doc/163947/1 "2019-01-11T18:49:37Z")

</div>

We are using ES version 2.1.1 and below code not returning the latest document created. Please suggest and let me know if the DFS\_QUERY\_THEN\_FETCH is good idea to use in prod ?

we lookup the given input in ES by using below code and then updates some of the other attributes associated to that found document in ES

Example : this code returns Dave Deleted but expecting Dave Created document since Created document is the latest entry in ES.

Name | Status  
Dave Deleted  
Dave Created

final SearchRequestBuilder srb = es.getClient().prepareSearch(es.getStatsIndex()).setTypes("voiceProductSnapshot")  
.setSearchType(org.elasticsearch.action.search.SearchType.DFS\_QUERY\_THEN\_FETCH);

final BoolQueryBuilder qb = QueryBuilders.boolQuery();  
qb.must(QueryBuilders.termQuery("sortName", zoneName));

final SearchResponse response = srb.setQuery(qb).execute().actionGet();

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [January 13, 2019, 11:38am UTC](https://discuss.elastic.co/t/elasticsearch-2-1-1-dfs-query-then-fetch-how-to-get-most-recent-doc/163947/2 "2019-01-13T11:38:42Z")

</div>

What does the documents look like? Do you they have a timestamp that helps you determine which one is the latest?

---

<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:** [February 10, 2019, 11:38am UTC](https://discuss.elastic.co/t/elasticsearch-2-1-1-dfs-query-then-fetch-how-to-get-most-recent-doc/163947/3 "2019-02-10T11:38:44Z")

</div>

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