如何对 Elasticsearch 中的对象列表执行聚合(例如 avg)?
How to perform aggregation (eg. avg) on list of objects in Elasticsearch?
我在 Elasticsearch 中有以下形式的数据:
[
{
"id": 1,
"name": "abc",
"score": 10,
"values": [
{
"v1": 1,
"v2": 2
}
]
},
{
"id": 2,
"name": "def",
"score": 20,
"values": [
{
"v1": 3,
"v2": 4
}
]
}
]
目前,我正在使用以下代码计算 score
的平均值:
s = Search(using=es, index=index).query(Q(query))
s.aggs.bucket('average_score', 'avg', field='score') # average_score = 15
现在我想使用 elasticsearch_dsl
计算 v1
和 v2
的平均值,但我不知道如何进行嵌套聚合。在此示例中,v1 = 2
和 v2 = 3
。请帮忙。提前致谢!
编辑: 不幸的是,这里values
是一个对象而不是嵌套。
不知道如何在您的 DSL 中执行此操作,但这是原始步骤。
映射:
PUT avgs/
{
"mappings": {
"properties": {
"values": {
"type": "nested",
"properties": {
"v1": {
"type": "integer"
},
"v2": {
"type": "integer"
}
}
}
}
}
}
索引:
POST avgs/_doc
{
"id": 1,
"name": "abc",
"score": 10,
"values": [
{
"v1": 1,
"v2": 2
}
]
}
POST avgs/_doc
{
"id": 2,
"name": "def",
"score": 20,
"values": [
{
"v1": 3,
"v2": 4
}
]
}
正在查询:
GET avgs/_search
{
"size": 0,
"aggs": {
"avg_v1": {
"nested": {
"path": "values"
},
"aggs": {
"nested_v1": {
"avg": {
"field": "values.v1"
}
}
}
},
"avg_v2": {
"nested": {
"path": "values"
},
"aggs": {
"nested_v2": {
"avg": {
"field": "values.v2"
}
}
}
}
}
}
产量:
...
"aggregations" : {
"avg_v2" : {
"doc_count" : 2,
"nested_v2" : {
"value" : 3.0
}
},
"avg_v1" : {
"doc_count" : 2,
"nested_v1" : {
"value" : 2.0
}
}
}
...
我在 Elasticsearch 中有以下形式的数据:
[
{
"id": 1,
"name": "abc",
"score": 10,
"values": [
{
"v1": 1,
"v2": 2
}
]
},
{
"id": 2,
"name": "def",
"score": 20,
"values": [
{
"v1": 3,
"v2": 4
}
]
}
]
目前,我正在使用以下代码计算 score
的平均值:
s = Search(using=es, index=index).query(Q(query))
s.aggs.bucket('average_score', 'avg', field='score') # average_score = 15
现在我想使用 elasticsearch_dsl
计算 v1
和 v2
的平均值,但我不知道如何进行嵌套聚合。在此示例中,v1 = 2
和 v2 = 3
。请帮忙。提前致谢!
编辑: 不幸的是,这里values
是一个对象而不是嵌套。
不知道如何在您的 DSL 中执行此操作,但这是原始步骤。
映射:
PUT avgs/
{
"mappings": {
"properties": {
"values": {
"type": "nested",
"properties": {
"v1": {
"type": "integer"
},
"v2": {
"type": "integer"
}
}
}
}
}
}
索引:
POST avgs/_doc
{
"id": 1,
"name": "abc",
"score": 10,
"values": [
{
"v1": 1,
"v2": 2
}
]
}
POST avgs/_doc
{
"id": 2,
"name": "def",
"score": 20,
"values": [
{
"v1": 3,
"v2": 4
}
]
}
正在查询:
GET avgs/_search
{
"size": 0,
"aggs": {
"avg_v1": {
"nested": {
"path": "values"
},
"aggs": {
"nested_v1": {
"avg": {
"field": "values.v1"
}
}
}
},
"avg_v2": {
"nested": {
"path": "values"
},
"aggs": {
"nested_v2": {
"avg": {
"field": "values.v2"
}
}
}
}
}
}
产量:
...
"aggregations" : {
"avg_v2" : {
"doc_count" : 2,
"nested_v2" : {
"value" : 3.0
}
},
"avg_v1" : {
"doc_count" : 2,
"nested_v1" : {
"value" : 2.0
}
}
}
...