在Elasticsearch嵌套aggs脚本中正确访问父字段

时间:2018-08-06 15:05:52

标签: elasticsearch

Elasticsearch版本:5.6.3

我有一个这样的映射:

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

}

数据:

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}

然后我要构建如下的aggs查询:

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

但是“ doc ['changeRatio']。value”无法获得正确的值,它始终返回0

另一个这样的查询:

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”是正确的,但是“ doc ['industry.rate']。value”得到0

  

请参阅以下问题:Elasticsearch 5.4: Use normal and nested fields in same Painless script query?   1. {params ['_ source'] ['changeRatio']}或{params ['_ source'] ['industry.rate']}在此版本中不起作用   2.“复制到”存储为多值字段,同样不起作用

如何使正确的脚本获得“ changeRatio * industry.rate”?

0 个答案:

没有答案