gpt4 book ai didi

elasticsearch - ElasticSearch:嵌套项目计入搜索结果

转载 作者:行者123 更新时间:2023-12-03 00:50:46 24 4
gpt4 key购买 nike

我有以下映射:

{
"test_index" : {
"mappings" : {
"test_type" : {
"properties" : {
"field1" : {
"type" : "string"
},
"field2" : {
"type" : "string"
},
"items" : {
"type" : "nested",
"properties" : {
"nested_field1" : {
"type" : "string"
},
"nested_field2" : {
"type" : "string"
}
}
}
}
}
}
}
}

对于搜索结果,我想在结果结构中获取嵌套的项目总数:
{
"hits": {
"total": 2,
"max_score": 1.0,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "AWfAc79wljtimCd5JZlJ",
"_score": 1.0,
"_source": {
"field1": "Some string 1",
"field2": "Some string 2",
"items": [
{
"nested_field1": "Some val1",
"nested_field2": "Some val2"
}
],
"totalItems": 1
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "AZxfc79dtrt878xx",
"_score": 1.0,
"_source": {
"field1": "Some string 3",
"field2": "Some string 4",
"items": [
{
"nested_field1": "Some val3",
"nested_field2": "Some val4"
},
{
"nested_field1": "Some val5",
"nested_field2": "Some val6"
}
],
"totalItems": 2
}
}
]
}
}

我可以通过聚合来实现吗?

最佳答案

由于您有个好主意,也可以将totalItems字段存储在根级别,因此您可以对该字段求和,然后得到嵌套项目的数量:

{
"query": {
"match_all": {}
},
"aggs": {
"total_items": {
"sum": {
"field": "totalItems"
}
}
}
}

关于elasticsearch - ElasticSearch:嵌套项目计入搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53830829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com