# Get duplicate documents based on duplicate nested type

**URL:** <https://discuss.elastic.co/t/get-duplicate-documents-based-on-duplicate-nested-type/178613>\
**Category:** Elasticsearch\
**Created:** [April 26, 2019, 9:53am UTC](https://discuss.elastic.co/t/get-duplicate-documents-based-on-duplicate-nested-type/178613 "2019-04-26T09:53:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Q.S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/q.s/32/45062_2.png) [@Q.S](https://discuss.elastic.co/u/Q.S)\
**Post date:** [April 26, 2019, 9:53am UTC](https://discuss.elastic.co/t/get-duplicate-documents-based-on-duplicate-nested-type/178613/1 "2019-04-26T09:53:44Z")

</div>

Good day guys.  
I have a document type "generalTask" with an array of nested documents called "completeUser".  
Here is the mapping :

```
{
    "generalTask": {
        "properties": {
            "id": {
                "type": "long"
            },
            "completeUser": {
                "type": "nested",
                "properties": {
                    "completeTime": {
                        "type": "long"
                    },
                    "userId": {
                        "type": "long"
                    }
                }
            }
        }
    }
}

```

And now, we have two documents.  
e.g.

```
{
    "_source": {
        "id": 1001,
        "completeUser": [
            {
                "userId": 1,
                "completeTime": 100
            },
            {
                "userId": 1,
                "completeTime": 300
            },
            {
                "userId":1,
                "completeTime": 500
            }
        ]
    }
}

```

and

```
{
    "_source": {
        "id": 1002,
        "completeUser": [
            {
                "userId": 1,
                "completeTime": 200
            },
            {
                "userId": 1,
                "completeTime": 400
            },
            {
                "userId":1,
                "completeTime": 600
            }
        ]
    }
}

```

I can get the docCount (which is 6) by nested aggregation like this:

```
    BoolQueryBuilder query = QueryBuilders.boolQuery();
    query.must(nestedQuery("completeUser", termQuery("completeUser.userId", 1)));   
     BoolQueryBuilder builder = getClient().prepareSearch(getIndexName()).setTypes(getIndexType()).setQuery(query)
                        .addAggregation(AggregationBuilders.nested("nested").path("completeUser")
                                .subAggregation(AggregationBuilders.count("count").field("completeUser.userId"))).setSize(0);
   SearchResponse searchResponse = getSearchResponse(builder);
   Nested nested = searchResponse.getAggregations().get("nested");
    long docCount = nested.getDocCount(); // the docCount is 6

```

but there are still only 2 documents in the searchResponse :

```
SearchRequestBuilder builder = getClient().prepareSearch(getIndexName()).setTypes(getIndexType())
                .setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(query).setFrom(0).setSize(5); // the size is 5
builder.addSort(SortBuilders.fieldSort("completeUser.completeTime")
                    .setNestedFilter(FilterBuilders.termFilter("completeUser.userId", 1))
                    .order(SortOrder.DESC));
SearchResponse searchResponse = getSearchResponse(builder);

```

But what I want is duplicate documents based on completeTime.  
How can I get 5 (the value of size) documents in the searchResponse order by the completeTime?  
Oh, yes. ElasticSearch version is 1.4.5

---

<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:** [May 24, 2019, 9:53am UTC](https://discuss.elastic.co/t/get-duplicate-documents-based-on-duplicate-nested-type/178613/2 "2019-05-24T09:53:45Z")

</div>

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