0 Вопрос: Как выполнить агрегацию по группам внутри группы в elacticsearch?

вопрос создан в Thu, May 2, 2019 12:00 AM

У меня есть данные JSON в следующем формате:

    {
  "_index": "sq_resource_idx",
  "_type": "sqr",
  "_id": "4dADdGoBoJweEMpjwLKF",
  "_version": 1,
  "_score": 2,
  "_source": {
    "function_complexity": "1.4",
    "security_rating": "2.0",
    "violations": "92",
    "duplicated_lines_density": "2.0",
    "name": "sonar",
    "vulnerabilities": "1",
    "code_smells": "87",
    "file_complexity": "32.3",
    "bugs": "4",
    "blocker_violations": "0",
    "metrics": "vulnerabilities",
    "ncloc": "2362",
    "complexity": "291",
    "coverage": "0.0",
    "critical_violations": "5",
    "date": "2018-08-26T04:13:34+0000",
    "Time_stamp": "2018-08-26T04:13:34+0000",
    "sqale_index": "429",
    "major_violations": "25",
    "security_remediation_effort": "10"
  },
  "fields": {
    "date": [
      "2018-08-26T04:13:34.000Z"
    ],
    "Time_stamp": [
      "2018-08-26T04:13:34.000Z"
    ]
  }
}

«Имя» имеет 5 различных типов значений. Я хотел бы получить поле ncloc для каждого типа "name" в соответствующем последнем документе.

Я написал запрос dsl, который успешно создал 5 групп, содержащих поля ncloc из их последнего документа. Теперь я хочу объединить эти поля ncloc всех проектов в одну метрику.

POST /sq_resource_idx/_search?size=0
{
  "aggs": {
        "my_date_histo": {
            "terms": {
                "field": "name.keyword"
            },
            "aggs": {
                "group_docs": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "Time_stamp": {
                                    "order": "desc"
                                }
                            }

                        ]
                    }
                }
            }
        }
}

} р>

Ответ следующий:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 846,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_date_histo" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "alm",
          "doc_count" : 141,
          "group_docs" : {
            "hits" : {
              "total" : 141,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "sq_resource_idx",
                  "_type" : "sqr",
                  "_id" : "8NGEdGoBoJweEMpjklgc",
                  "_score" : null,
                  "_source" : {
                    "major_violations" : "27",
                    "critical_violations" : "3",
                    "ncloc" : "2137",
                    "security_rating" : "2.0",
                    "new_vulnerabilities" : "0",
                    "duplicated_lines_density" : "0.0",
                    "vulnerabilities" : "1",
                    "security_remediation_effort" : "10",
                    "blocker_violations" : "0",
                    "violations" : "83",
                    "new_security_remediation_effort" : "0",
                    "file_complexity" : "29.9",
                    "metrics" : "vulnerabilities",
                    "coverage" : "0.0",
                    "date" : "2019-01-23T11:13:29+0000",
                    "Time_stamp" : "2019-01-23T11:13:29+0000",
                    "function_complexity" : "1.4",
                    "name" : "alm",
                    "code_smells" : "78",
                    "bugs" : "4",
                    "complexity" : "269",
                    "sqale_index" : "443"
                  },
                  "sort" : [
                    1548242009000
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "gitlab",
          "doc_count" : 141,
          "group_docs" : {
            "hits" : {
              "total" : 141,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "sq_resource_idx",
                  "_type" : "sqr",
                  "_id" : "ktD5c2oBoJweEMpjRKWC",
                  "_score" : null,
                  "_source" : {
                    "Time_stamp" : "2018-08-26T05:42:43+0000",
                    "metrics" : "vulnerabilities",
                    "date" : "2018-08-26T05:42:43+0000",
                    "blocker_violations" : "0",
                    "name" : "gitlab",
                    "bugs" : "4",
                    "code_smells" : "77",
                    "complexity" : "253",
                    "coverage" : "0.0",
                    "critical_violations" : "3",
                    "duplicated_lines_density" : "0.0",
                    "file_complexity" : "28.1",
                    "function_complexity" : "1.5",
                    "major_violations" : "23",
                    "ncloc" : "2059",
                    "security_rating" : "2.0",
                    "security_remediation_effort" : "10",
                    "sqale_index" : "372",
                    "violations" : "82",
                    "vulnerabilities" : "1"
                  },
                  "sort" : [
                    1535262163000
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "jira",
          "doc_count" : 141,
          "group_docs" : {
            "hits" : {
              "total" : 141,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "sq_resource_idx",
                  "_type" : "sqr",
                  "_id" : "aNA7dGoBoJweEMpjSPqy",
                  "_score" : null,
                  "_source" : {
                    "function_complexity" : "1.4",
                    "security_rating" : "2.0",
                    "violations" : "101",
                    "duplicated_lines_density" : "0.0",
                    "name" : "jira",
                    "vulnerabilities" : "1",
                    "code_smells" : "96",
                    "file_complexity" : "29.4",
                    "bugs" : "4",
                    "blocker_violations" : "0",
                    "metrics" : "vulnerabilities",
                    "ncloc" : "2086",
                    "complexity" : "265",
                    "coverage" : "0.0",
                    "critical_violations" : "3",
                    "date" : "2018-08-26T04:10:59+0000",
                    "Time_stamp" : "2018-08-26T04:10:59+0000",
                    "sqale_index" : "491",
                    "major_violations" : "30",
                    "security_remediation_effort" : "10"
                  },
                  "sort" : [
                    1535256659000
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "newsource",
          "doc_count" : 141,
          "group_docs" : {
            "hits" : {
              "total" : 141,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "sq_resource_idx",
                  "_type" : "sqr",
                  "_id" : "4tADdGoBoJweEMpjwrIQ",
                  "_score" : null,
                  "_source" : {
                    "function_complexity" : "1.5",
                    "security_rating" : "2.0",
                    "violations" : "82",
                    "duplicated_lines_density" : "0.0",
                    "name" : "newsource",
                    "vulnerabilities" : "1",
                    "code_smells" : "77",
                    "file_complexity" : "28.1",
                    "bugs" : "4",
                    "blocker_violations" : "0",
                    "metrics" : "vulnerabilities",
                    "ncloc" : "2059",
                    "complexity" : "253",
                    "coverage" : "0.0",
                    "critical_violations" : "3",
                    "date" : "2018-08-26T04:07:11+0000",
                    "Time_stamp" : "2018-08-26T04:07:11+0000",
                    "sqale_index" : "372",
                    "major_violations" : "23",
                    "security_remediation_effort" : "10"
                  },
                  "sort" : [
                    1535256431000
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "servicenow",
          "doc_count" : 141,
          "group_docs" : {
            "hits" : {
              "total" : 141,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "sq_resource_idx",
                  "_type" : "sqr",
                  "_id" : "7dGEdGoBoJweEMpjj1j8",
                  "_score" : null,
                  "_source" : {
                    "function_complexity" : "1.5",
                    "security_rating" : "2.0",
                    "violations" : "92",
                    "duplicated_lines_density" : "4.2",
                    "name" : "servicenow",
                    "vulnerabilities" : "1",
                    "code_smells" : "87",
                    "file_complexity" : "30.2",
                    "bugs" : "4",
                    "blocker_violations" : "0",
                    "metrics" : "vulnerabilities",
                    "ncloc" : "2382",
                    "complexity" : "302",
                    "coverage" : "0.0",
                    "critical_violations" : "3",
                    "date" : "2018-08-26T04:15:32+0000",
                    "Time_stamp" : "2018-08-26T04:15:32+0000",
                    "sqale_index" : "444",
                    "major_violations" : "31",
                    "security_remediation_effort" : "10"
                  },
                  "sort" : [
                    1535256932000
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "sonar",
          "doc_count" : 141,
          "group_docs" : {
            "hits" : {
              "total" : 141,
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "sq_resource_idx",
                  "_type" : "sqr",
                  "_id" : "4dADdGoBoJweEMpjwLKF",
                  "_score" : null,
                  "_source" : {
                    "function_complexity" : "1.4",
                    "security_rating" : "2.0",
                    "violations" : "92",
                    "duplicated_lines_density" : "2.0",
                    "name" : "sonar",
                    "vulnerabilities" : "1",
                    "code_smells" : "87",
                    "file_complexity" : "32.3",
                    "bugs" : "4",
                    "blocker_violations" : "0",
                    "metrics" : "vulnerabilities",
                    "ncloc" : "2362",
                    "complexity" : "291",
                    "coverage" : "0.0",
                    "critical_violations" : "5",
                    "date" : "2018-08-26T04:13:34+0000",
                    "Time_stamp" : "2018-08-26T04:13:34+0000",
                    "sqale_index" : "429",
                    "major_violations" : "25",
                    "security_remediation_effort" : "10"
                  },
                  "sort" : [
                    1535256814000
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Я попытался добавить агрегацию конвейера:

   "total_sum": {
          "sum_bucket": {
            "buckets_path": "my_date_histo.buckets.hits.hits._source.critical_violations"
          }
        }

Это привело к следующей ошибке:

  "reason": "No aggregation found for path [my_date_histo.buckets.hits.hits._source.critical_violations]"
    
0
0 ответов                              0                         
источник размещен Вот