# 7.x How do remove duplicates in search after query?

**URL:** <https://discuss.elastic.co/t/7-x-how-do-remove-duplicates-in-search-after-query/259692>\
**Category:** Elasticsearch\
**Created:** [December 27, 2020, 6:15pm UTC](https://discuss.elastic.co/t/7-x-how-do-remove-duplicates-in-search-after-query/259692 "2020-12-27T18:15:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adin\_Sijamija](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adin_sijamija/32/77134_2.png) [@Adin\_Sijamija](https://discuss.elastic.co/u/Adin_Sijamija)\
**Post date:** [December 27, 2020, 6:15pm UTC](https://discuss.elastic.co/t/7-x-how-do-remove-duplicates-in-search-after-query/259692/1 "2020-12-27T18:15:15Z")

</div>

I have several indexes spread across 3 shards that hold my data. I want to make a SearchAfter where I fetch 50 rows ordered by start time. When I run it I am returned 50 hits but half of them are duplicates as the data is spread to several indices. I thought it was to the query then detach approach but even after I switch to DfsQueryThenFetch I still return duplicates. How do I filter out duplicates (preferably by guid field)? Code below

```auto
    //reduced for brevity
        List<resultObjects> result = new List<resultObjects>();

        DateRangeQuery dq= new DateRangeQuery()
        {
            Name = "StartTime",
            Field = "StartTime",
            GreaterThanOrEqualTo = from,
            LessThanOrEqualTo = to
        };

        TermQuery tq1= new TermQuery()
        {
            Name = "Location",
            Field = "Location"
            Value = "sds"
        };

        BoolQuery boolQuery = new BoolQuery();
        boolQuery.Filter = new List<QueryContainer>() { dq, tq };
        boolQuery.Must = new List<QueryContainer>()
        {
           new MatchQuery() 
                {
                Name = "user",
                Field ="user",
                Query= "test"
                }
        };

        var searchRequest = new SearchRequest("data_index_*");
        searchRequest.SearchType = SearchType.DfsQueryThenFetch;
        searchRequest.From = 0;
        searchRequest.Size = 50;
        searchRequest.Query = boolQuery;
        searchRequest.Sort = new List<ISort>
        {
            new FieldSort { Field=Infer.Field<resultObjects> 
              (p=>p.StartTime),Order=SortOrder.Ascending}
        };

        if (searchAfterTime < to)
            searchRequest.SearchAfter = new List<object> { searchAfterTime };

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 5, 2021, 12:54am UTC](https://discuss.elastic.co/t/7-x-how-do-remove-duplicates-in-search-after-query/259692/2 "2021-01-05T00:54:39Z")

</div>

Which of the duplicates should Elasticsearch use though, the one with the newest `StartTime`? If so then consider also adding a top hits agg on that field.

---

<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 2, 2021, 12:54am UTC](https://discuss.elastic.co/t/7-x-how-do-remove-duplicates-in-search-after-query/259692/3 "2021-02-02T00:54:45Z")

</div>

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