ElasticSearch - 如何获取聚合的聚合

ElasticSearch - how to get aggregation of aggregation

我是 elasticsearch 的新手 我在一个约会网站工作,该网站的数据如下: 单一 - 包含字段:姓名、注册日期、州和其他数据字段。

Encounter - 包含字段:state、encounterDate、singlesInvolved 和其他数据字段。

这是我的 2 个索引

现在我必须编写一个 return 如下的查询:

对于每个州,有多少单身人士,多少次相遇,单身人士进入我们网站的最长时间,以及单身人士进入我们网站的平均时间

还有一个结果 return 所有州的平均值相同 像这个例子:

[
 { //this one is the average of all states
    "singles": 45,
    "dates": 18,
    "minWaitingTime": 1644677979530,
    "avgWaitingTime": 15603
  },
  { //these are the averages of each state
    "state": "MA",
    "singles": 50,
    "dates": 23,
    "minWaitingTime": 1644677979530,
    "avgWaitingTime": 15603
  },
  {
    "state": "NY",
    "singles": 39,
    "dates": 13,
    "minWaitingTime": 1644850558872,
    "avgWaitingTime": 6033
  }
]

我一直在单独查询每个州,但我不知道如何获得所有州的平均值

到目前为止我所拥有的是:

GET /single,encounter/_search 
{
  "size": 0, 
  "aggs": {
    "bystate": {
      "terms": {
        "field": "state",
        "size": 59
      },
      "aggs": {
        "group-by-index": {
          "terms": {
            "field": "_index"
          }
        },
        "min_date": {
          "min": {
            "field": "signedUpAt"
          }
        },
        "avg_date": {
          "avg": {
            "field": "signedUpAt"
          }
        }
      }
    }
  }
}

我不知道有没有更好的办法,同样我也不知道如何计算平均值(单身,相遇,min_date和average_date平均值)所有使用此结果的州 上一个查询的每个结果如下所示:

{
      "key" : "MA",
      "doc_count" : 164,
      "avg_date" : {
        "value" : 1.6457900076508965E12,
        "value_as_string" : "2022-02-25T11:53:27.650"
      },
      "min_date" : {
        "value" : 1.64467797953E12,
        "value_as_string" : "2022-02-12T14:59:39.530"
      },
      "group-by-index" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "single",
            "doc_count" : 135
          },
          {
            "key" : "encounter",
            "doc_count" : 29
          }
        ]
      }
    },

非常感谢在这方面的帮助

补充:索引映射。 遭遇:

{
  "encounter" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "avgAge" : {
          "type" : "integer",
          "index" : false,
          "doc_values" : false
        },
        "application" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "createdAt" : {
          "type" : "date",
          "format" : "date_hour_minute_second_millis"
        },
        "encounterId" : {
          "type" : "keyword"
        },
        "locationType" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "singleOneId" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "singleTwoId" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "serviceLine" : {
          "type" : "keyword"
        },
        "state" : {
          "type" : "keyword"
        },
        "rating" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "refresh_interval" : "1s",
        "number_of_shards" : "1",
        "provided_name" : "encounter",
        "creation_date" : "1643704661932",
        "number_of_replicas" : "1",
        "uuid" : "MliXQL_bRBKDN7_d8G_BYw",
        "version" : {
          "created" : "7100299"
        }
      }
    }
  }
}

单身:

{
  "single" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "id" : {
          "type" : "keyword"
        },
        "singleId" : {
          "type" : "keyword"
        },
        "state" : {
          "type" : "keyword"
        },
        "preferedGender" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "refresh_interval" : "1s",
        "number_of_shards" : "1",
        "provided_name" : "single",
        "creation_date" : "1643704662136",
        "number_of_replicas" : "1",
        "uuid" : "Js_tqZfRRx-IxbjVRRN4wQ",
        "version" : {
          "created" : "7100299"
        }
      }
    }
  }
}

您可以使用 avg bucket aggregation,您可以在其中提供 bucket_path 并根据值计算整个聚合的平均值。

下面是示例查询:

{
  "size": 0,
  "aggs": {
    "bystate": {
      "terms": {
        "field": "state",
        "size": 59
      },
      "aggs": {
        "group-by-index": {
          "terms": {
            "field": "_index"
          }
        },
        "min_date": {
          "min": {
            "field": "signedUpAt"
          }
        },
        "avg_date": {
          "avg": {
            "field": "signedUpAt"
          }
        }
      }
    },
    "avg_all_state": {
      "avg_bucket": {
        "buckets_path": "bystate>avg_date"
      }
    }
  }
}