Right way access parent field in Elasticsearch nested aggs script

 ̄綄美尐妖づ 提交于 2021-01-28 02:48:57

问题


Elasticsearch Version: 5.6.3

I have a mapping like this:

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

}

Datas:

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:

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:

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? 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"?

来源:https://stackoverflow.com/questions/51710652/right-way-access-parent-field-in-elasticsearch-nested-aggs-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!