# Retrieving specific amount of nested documents

**URL:** <https://discuss.elastic.co/t/retrieving-specific-amount-of-nested-documents/158306>\
**Category:** Elasticsearch\
**Created:** [November 27, 2018, 9:47am UTC](https://discuss.elastic.co/t/retrieving-specific-amount-of-nested-documents/158306 "2018-11-27T09:47:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slade](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@Slade](https://discuss.elastic.co/u/Slade)\
**Post date:** [November 27, 2018, 9:47am UTC](https://discuss.elastic.co/t/retrieving-specific-amount-of-nested-documents/158306/1 "2018-11-27T09:47:30Z")

</div>

Say I have an index with mapping:  
PUT /item

```
 {
    "mappings": {
        "_doc" : {
            "properties" : {
				"name": { "type" : "keyword" },
                "supplierName": { "type" : "keyword" },				
                "comments" : { 
                    "type" : "nested",
                    "properties" : {
                        "username" : { "type" : "keyword" },
                        "comment" : { "type" : "text" }
                    }
                }
            }
        }
    }
 }

```

and I want to retrieve a specific amount of comments that are from a specific supplier and where the comment is made by a specific user.  
A user can comment on an item as many times as they want and a supplier can have many items.  
Example:  
PUT /item/\_doc/1?refresh

```
{
     "name":"ItemOne",
     "supplierName":"CoolSupplier",
     "comments": [
         
         {"username": "mark", "comment": "Cool item1"},
         {"username": "mark", "comment": "Cool item2"},
         {"username": "mark", "comment": "Cool item3"},
         {"username": "mark", "comment": "Cool item4"},
         {"username": "mark", "comment": "Cool item5"},
         {"username": "mark", "comment": "Cool item6"},
         {"username": "jake", "comment": "Bad item"},
         {"username": "paul", "comment": "Great item"}
     ]
}

```

So say I want to retrieve a certain amount of comments with the name of the item for a specific supplier and user regardless if all the comments are on a single item or spread across multiple.  
If I use nested inner\_hits like this:  
GET item/\_search

```
{
  "size": 4,
  "_source": "name",
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "supplierName": "CoolSupplier"
          }
        },
        {
          "nested": {
            "path": "comments",
            "query": {
              "match": {
                "comments.username": "mark"
              }
            },
            "inner_hits": {
              "size": 4
            }
          }
        }
      ]
    }
  }
}

```

With this query up to four comments can be returned per parent document.  
The thing is I only want 4 nested documents in total. Those four nested documents could come from the first found parent document, or one document from four different parent documents.  
Is there a way to specify a total amount/maximum number of inner\_hits to return regardless of parent doc?

Another alternative I found is the top\_hits metric aggregation:  
GET item/\_search

```
{
  "size": 0,
  "aggs": {
    "outerFilter": {
      "filter": {
        "match": {
          "supplierName": "CoolSupplier"
        }
      },
      "aggs": {
        "commentAggs": {
          "nested": {
            "path": "comments"
          },
          "aggs": {
            "commentsFilter": {
              "filter": {
                "match": {
                  "comments.username": "mark"
                }
              },
              "aggs": {
                "foundComments": {
                  "top_hits": {
                    "size": 4
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

This correctly returns a maximum of 4 comments regardless of parent docs. The only problem is I wish to also retrieve information from the parent document. Is there a way to do this?  
If I wanted to retrieve say the name of the item the comment was made on, how would I do that? Would i need to perform the aggs query, then perform another query with all of the item ids in order to retrieve the name?  
Or that if the parent document had a createDate timestamp and I wanted to order by that, is that possible using top\_hits aggs? I haven't been able to figure it out.

Should the mapping maybe be a join datatype instead of nested?

---

<div class="post-metadata">

**Author:** ![Slade](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@Slade](https://discuss.elastic.co/u/Slade)\
**Post date:** [November 29, 2018, 2:27pm UTC](https://discuss.elastic.co/t/retrieving-specific-amount-of-nested-documents/158306/2 "2018-11-29T14:27:00Z")

</div>

No one?

---

<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:** [December 27, 2018, 2:27pm UTC](https://discuss.elastic.co/t/retrieving-specific-amount-of-nested-documents/158306/3 "2018-12-27T14:27:09Z")

</div>

Closed on December 27, 2018, 2:27pm UTC

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