# Separation of hits returned from elastic by nested field value

**URL:** <https://discuss.elastic.co/t/separation-of-hits-returned-from-elastic-by-nested-field-value/182171>\
**Category:** Elasticsearch\
**Created:** [May 22, 2019, 8:25am UTC](https://discuss.elastic.co/t/separation-of-hits-returned-from-elastic-by-nested-field-value/182171 "2019-05-22T08:25:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyk](https://avatars.discourse-cdn.com/v4/letter/p/c4cdca/32.png) [@pyk](https://discuss.elastic.co/u/pyk)\
**Post date:** [May 22, 2019, 8:25am UTC](https://discuss.elastic.co/t/separation-of-hits-returned-from-elastic-by-nested-field-value/182171/1 "2019-05-22T08:25:49Z")

</div>

I've index with products there. I'm trying to separate hits returned from elastic by nested field value. There's my shortened

```
  {
          "mapping": {
            "product": {
              "properties": {        
                "id": {
                  "type": "integer"
                },
                "model_name": {
                  "type": "text",          
                },
                "variants": {
                  "type": "nested",
                  "properties": {
                    "attributes": {
                      "type": "nested",
                      "properties": {
                        "id": {
                          "type": "integer"
                        },
                        "name": {
                          "type": "text"
                        },
                        "product_attribute_id": {
                          "type": "integer"
                        },               
                        "value": {
                          "type": "text"
                        }
                      }
                    },
                    "id": {
                      "type": "integer"
                    },
                    "product_id": {
                      "type": "integer"
                    }            
                  }
                }
              }
            }
          }
    }

```

And product example (there's is more variants and attributes in product - I just cut them off):

```
{
   "_index":"product_index",
   "_type":"product",
   "id":192,
   "model_name":"Some tshirt",
   "variants":[
      {
         "id":1271,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"XL",
               "product_attribute_id":36740
            }
         ]
      },
      {
         "id":1272,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"L",
               "product_attribute_id":36741
            }
         ]
      }
   ]
} 

```

The field in question is attribute id. Let's say I want to separate products by size attribute - id 29. It would be perfect if the response would look like:

```
"hits" : [
{
   "_index":"product_index",
   "_type":"product",
   "id":192,
   "model_name":"Some tshirt",
   "variants":[
      {
         "id":1271,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"XL",
               "product_attribute_id":36740
            }
         ]
      }
   ]
},     
{
   "_index":"product_index",
   "_type":"product",
   "id":192,
   "model_name":"Some tshirt",
   "variants":[      
      {
         "id":1272,
         "product_id":192,
         "attributes":[
            {
               "id":29,
               "name":"clothesSize",
               "value":"L",
               "product_attribute_id":36741
            }
         ]
      }
   ]
}]

```

I thought about separate all variants in elastic request and then group them on application side by those attribute but i think it's not most elegant and above all, efficient way.  
What are the elastic keywords that I should be interested in?  
Thank you in advance for your help.

---

<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:** [June 19, 2019, 8:36am UTC](https://discuss.elastic.co/t/separation-of-hits-returned-from-elastic-by-nested-field-value/182171/2 "2019-06-19T08:36:46Z")

</div>

Closed on June 19, 2019, 8:36am UTC

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