# Right way access parent field in nested aggs script?

**URL:** <https://discuss.elastic.co/t/right-way-access-parent-field-in-nested-aggs-script/143086>\
**Category:** Elasticsearch\
**Created:** [August 6, 2018, 4:09am UTC](https://discuss.elastic.co/t/right-way-access-parent-field-in-nested-aggs-script/143086 "2018-08-06T04:09:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yjp211](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yjp211/32/33964_2.png) [@yjp211](https://discuss.elastic.co/u/yjp211)\
**Post date:** [August 6, 2018, 4:09am UTC](https://discuss.elastic.co/t/right-way-access-parent-field-in-nested-aggs-script/143086/1 "2018-08-06T04:09:09Z")

</div>

Elasticsearch Version: 5.6.3

I have a mapping like this:

```auto
PUT /my_stock
{
    "mappings": {
      "stock": {
        "properties": {
          "industry": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "keyword"
              },
              "rate": {
                "type": "double"
              }
            }
          },
          "changeRatio": {
            "type": "double"
          }
        }
      }
    }
  
}

```

Datas:

```auto
POST /_bulk
{"index":{"_index":"my_stock","_type":"stock","_id":null}}
{"industry":[{"name":"Technology","rate":0.6},{"name":"Health", "rate":0.2}],"changeRatio":0.1}
{"index":{"_index":"my_stock","_type":"stock","_id":null}}
{"industry":[{"name":"Health", "rate":0.3},{"name":"Education", "rate":0.2}],"changeRatio":0.2}
{"index":{"_index":"my_stock","_type":"stock","_id":null}}
{"industry":[{"name":"Health","rate":0.5},{"name":"Education","rate":0.2}],"changeRatio":-0.3}
{"index":{"_index":"my_stock","_type":"stock","_id":null}}
{"industry":[{"name":"Technology","rate":0.3},{"name":"Education","rate":0.3}],"changeRatio":0.4}
{"index":{"_index":"my_stock","_type":"stock","_id":null}}
{"industry":[{"name":"Education","rate":0.3},{"name":"Technology","rate":0.1}],"changeRatio":-0.5}

```

I then want to build a aggs query like this:

```auto
GET my_stock/stock/_search
{
  "size": 0,
  "aggs": {
    "industry": {
      "nested": {
        "path": "industry"
      },
      "aggs": {
        "groups": {
          "terms": {
            "field": "industry.name",
            "order": {
              "rate": "desc"
            }
          },
          "aggs": {
            "rate": {
              "sum": {
                "script": {
                  "source": "doc['changeRatio'].value * doc['industry.rate'].value"
                }
              }
            }
          }
        }
      }
    }
  }
}

```

but "doc['changeRatio'].value" can't get right value, it's always return 0

another query like this:

```auto
GET my_stock/stock/_search
{
  "size": 0,
  "aggs": {
    "industry": {
      "nested": {
        "path": "industry"
      },
      "aggs": {
        "groups": {
          "terms": {
            "field": "industry.name",
            "order":{
              "reverse>rate":"desc"
            }
          },
          "aggs": {
            "reverse": {
              "reverse_nested": {},
              "aggs": {
                "rate": {
                  "sum": {
                    "script": {
                      "source": "doc['changeRatio'].value * doc['industry.rate'].value"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

"doc['changeRatio'].value" is right, but "doc['industry.rate'].value" get 0

> Refer to this question: [Elasticsearch 5.4: Use normal and nested fields in same Painless script query? - Stack Overflow](https://stackoverflow.com/questions/43924266/elasticsearch-5-4-use-normal-and-nested-fields-in-same-painless-script-query)
> 
> 1. { params['\_source']['changeRatio'] } or { params['\_source']['industry.rate'] } not work in this version
> 2. "copy to" stored as a multivalue field, also not working

How can i make a correct script get "changeRatio \* industry.rate"?

---

<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:** [September 3, 2018, 4:09am UTC](https://discuss.elastic.co/t/right-way-access-parent-field-in-nested-aggs-script/143086/2 "2018-09-03T04:09:09Z")

</div>

Closed on September 3, 2018, 4:09am UTC

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