弹性搜索查询列表和子列表



我在Elastic中有一个索引,其中包含键和值的数组。

例如,一个文档看起来像这样:

{
"_index": "my_index",
"_source": {
"name": "test",
"values": [
{
"name": "a",
"score": 10
},
{
"name": "b",
"score": 4
},
{
"name": "c",
"score": 2
},
{
"name": "d",
"score": 1
}
]
},
"fields": {
"name": [
"test"
],
"values.name.keyword": [
"a",
"b",
"c",
"d"
],
"name.keyword": [
"test"
],
"values.score": [
10,
4,
2,
1
],
"values.name": [
"a",
"b",
"c",
"d"
]
}
}

我想创建一个弹性查询(通过API),检索由名称列表过滤的所有名称分数的总和。

例如,对于输入:names = ['a', 'b']结果将是:14

你知道怎么做吗?

您可以通过设置值数组nested来实现这一点。映射例子:

{
"mappings": {
"properties": {
"values": { "type": "nested" }
}
}
}

下面的查询将给出你想要的结果:

{
"size":0,
"aggs": {
"asd": {
"nested": {
"path": "values"
},
"aggs": {
"filter_agg": {
"filter": {
"terms": {
"values.name.keyword": [
"a",
"b"
]
}
},
"aggs": {
"sum": {
"sum": {
"field": "values.score"
}
}
}
}
}
}
}
}

最新更新