# Limit size of nested collection on Query Result

**URL:** <https://discuss.elastic.co/t/limit-size-of-nested-collection-on-query-result/118403>\
**Category:** Elasticsearch\
**Created:** [February 5, 2018, 8:26am UTC](https://discuss.elastic.co/t/limit-size-of-nested-collection-on-query-result/118403 "2018-02-05T08:26:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupanjanbaidya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rupanjanbaidya/32/27362_2.png) [@rupanjanbaidya](https://discuss.elastic.co/u/rupanjanbaidya)\
**Post date:** [February 5, 2018, 8:26am UTC](https://discuss.elastic.co/t/limit-size-of-nested-collection-on-query-result/118403/1 "2018-02-05T08:26:18Z")

</div>

I am fairly a new user of Elasticsearch. Struggling a bit to build a query which limits the no of collections on a given nested field.  
Elasticsearch version 6.1

One product can have multiple stores as below,

```
 "hits" : {
    "total" : 1,
    "max_score" : 7.826804,
    "hits" : [
      {
        "_index" : "product_v1",
        "_type" : "product",
        "_id" : "500474",
        "_score" : 7.826804,
        "_source" : {
          "@version" : "1",
          "priorty" : 3378,
          "sku_parent_code" : null,
          "sku_description" : "What It Is\nA gentle and safe toothbrush made with ultra-soft Tynex bristles to suit the delicate gums of babies. It is suitable for babies aged 6 months and above and comes in bright and fun colors that children will love.\n\nWhat It Does\nThe perfect soluti",
          "store" : [
            {
              "batch_no" : 29742,
              "camp_type" : null,
              "list_price" : 30.0,
              "ofc_code" : "110",
              "sku_shelf_life" : 6,
              "disc_percentage" : 0,
              "sku_available_unit" : 9,
              "camp_code" : null,
              "default_price" : 30.0
            },
            {
              "batch_no" : 29742,
              "camp_type" : null,
              "list_price" : 30.0,
              "ofc_code" : "176",
              "sku_shelf_life" : 6,
              "disc_percentage" : 0,
              "sku_available_unit" : 9,
              "camp_code" : null,
              "default_price" : 30.0
            }
          ],
          "category" : [
            {
              "category_code" : 3233,
              "category_name" : "Baby Essentials"
            },
            {
              "category_code" : 3293,
              "category_name" : "Toothbrushes"
            }
          ],,
          "sku_name" : "Johnsons Baby Soft Toothbrush",
          "sku_code" : "500474",
          "sku_title" : "Johnsons Baby Toothbrush",
          "brand" : {
            "brand_code" : 5135,
            "brand_name" : "Johnson & Johnson"
          }
        }
      }
    ]
  }

```

sample query I have tried with,

```
{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "sku_code": "500474"
               }
            },
	        "nested": {
	            "path": "store",
	            "size" : 1,
	            "query": {
	               "bool": {
	                  "must": [
	                     {
	                        "term": {
	                           "store.ofc_code": "110"
	                        }
	                     }
	                  ]
	               }
	            }
	        }
         ]
      }
   }
}

```

Response should contain only the matched store element when queried with **sku\_code** & **store\_code**

```
 "hits" : [
      {
        "_index" : "product_v1",
        "_type" : "product",
        "_id" : "500474",
        "_score" : 7.826804,
        "_source" : {
          "@version" : "1",
          "priorty" : 3378,
          "sku_parent_code" : null,
          "sku_description" : "What It Is\nA gentle and safe toothbrush made with ultra-soft Tynex bristles to suit the delicate gums of babies. It is suitable for babies aged 6 months and above and comes in bright and fun colors that children will love.\n\nWhat It Does\nThe perfect soluti",
          "store" : [
            {
              "batch_no" : 29742,
              "camp_type" : null,
              "list_price" : 30.0,
              "ofc_code" : "176",
              "sku_shelf_life" : 6,
              "disc_percentage" : 0,
              "sku_available_unit" : 9,
              "camp_code" : null,
              "default_price" : 30.0
            }
          ],
          "category" : [
            {
              "category_code" : 3233,
              "category_name" : "Baby Essentials"
            },
            {
              "category_code" : 3293,
              "category_name" : "Toothbrushes"
            }
          ],,
          "sku_name" : "Johnsons Baby Soft Toothbrush",
          "sku_code" : "500474",
          "sku_title" : "Johnsons Baby Toothbrush",
          "brand" : {
            "brand_code" : 5135,
            "brand_name" : "Johnson & Johnson"
          }
        }
      }
    ]
  }
}

```

Any help will be greatly appreciated.

---

<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:** [March 5, 2018, 8:26am UTC](https://discuss.elastic.co/t/limit-size-of-nested-collection-on-query-result/118403/2 "2018-03-05T08:26:26Z")

</div>

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