1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
// This file is autogenerated, DO NOT EDIT
// aggregations/metrics/weighted-avg-aggregation.asciidoc:152
[source, python]
----
resp = client.index(
index="exams",
refresh=True,
document={
"grade": 100,
"weight": [
2,
3
]
},
)
print(resp)
resp1 = client.index(
index="exams",
refresh=True,
document={
"grade": 80,
"weight": 3
},
)
print(resp1)
resp2 = client.search(
index="exams",
filter_path="aggregations",
size=0,
runtime_mappings={
"weight.combined": {
"type": "double",
"script": "\n double s = 0;\n for (double w : doc['weight']) {\n s += w;\n }\n emit(s);\n "
}
},
aggs={
"weighted_grade": {
"weighted_avg": {
"value": {
"script": "doc.grade.value + 1"
},
"weight": {
"field": "weight.combined"
}
}
}
},
)
print(resp2)
----
|