如何在elasticsearch中聚合脚本字段?

6mzjoqzu  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(0)|浏览(283)

计算类别是查询上的脚本字段。我想在这个脚本字段上再进行一级聚合。有可能吗?如果是,请帮帮我。
我的问题:-

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {}
  },
  "aggs": {
    "stakeholder_category": {
      "terms": {
        "field": "stakeholder_level",
        "size": 214748364
      },
      "aggs": {
        "Coverage-Calculation": {
          "terms": {
            "field": "stakeholder_name",
            "size": 214748364
          },
          "aggs": {
            "Total-Hours": {
              "sum": {
                "field": "field_capacity_building_duration"
              }
            },

                    "include_source": {
                      "top_hits": {
                        "size": 1,
                        "_source": {
                          "includes": [
                            "state_location",
                            "district_location"
                          ]
                        }
                      }
                    },

                    "Calculated-Category": {
                    "bucket_script": {
                      "buckets_path": {
                        "NumberOfEngagements": "_count",
                        "TotalHours":"Total-Hours"
                      },

                      "script": {
                      "lang": "painless",
                      "source": """

                          if (params.TotalHours >= 20 && params.NumberOfEngagements >= 2) {
                          return 1;  
                        } else if(params.TotalHours >3) {
                          return 2 ;  
                        }
                          else if(params.TotalHours >3) {
                          return 2 ;  
                        }
                          else if(params.TotalHours >=1 && params.TotalHours <=3) {
                          return 3 ;  
                        }
                        else if(params.TotalHours ==0) {
                          return 4 ;  
                        }

                """
                }
              }
            }
          }
        }
      }
    }
  }
}

从上面的查询中,我需要在脚本字段calculated category上进行聚合。我们可以在脚本字段上进行聚合吗?如果是,怎么做?
上述查询的结果如下

{
                "key" : "CHUNUSINGH GAUTAM",
                "doc_count" : 1,
                "include_source" : {
                  "hits" : {
                    "total" : 1,
                    "max_score" : 1.0,
                    "hits" : [
                      {
                        "_index" : "elasticsearch_index_apffatest_coverage_new",
                        "_type" : "coverage_new",
                        "_id" : "entity:paragraph/3826125:en",
                        "_score" : 1.0,
                        "_source" : {
                          "district_location" : [
                            "Raipur"
                          ],
                          "state_location" : [
                            "Chhattisgarh"
                          ]
                        }
                      }
                    ]
                  }
                },
                "Total-Hours" : {
                  "value" : 0.0
                },
                "Calculated-Category" : {
                  "value" : 4.0
                }
              },
              {
                "key" : "SAVITA DHSH test",
                "doc_count" : 1,
                "include_source" : {
                  "hits" : {
                    "total" : 1,
                    "max_score" : 1.0,
                    "hits" : [
                      {
                        "_index" : "elasticsearch_index_apffatest_coverage_new",
                        "_type" : "coverage_new",
                        "_id" : "entity:paragraph/3747567:en",
                        "_score" : 1.0,
                        "_source" : {
                          "district_location" : [
                            "Raipur"
                          ],
                          "state_location" : [
                            "Chhattisgarh"
                          ]
                        }
                      }
                    ]
                  }
                },
                "Total-Hours" : {
                  "value" : 3.0
                },
                "Calculated-Category" : {
                  "value" : 3.0
                }
              }

我需要聚合如果计算的类别有多个相同的值,那么它应该和它的计数一起,就像前面的术语字段聚合一样。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题