Get duplicate documents based on duplicate nested type

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

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