gpt4 book ai didi

elasticsearch - 我应该如何编写查询以仅在Elasticsearch中获取嵌套属性

转载 作者:行者123 更新时间:2023-12-03 01:26:47 24 4
gpt4 key购买 nike

我的索引如下。

{
"id": {
"type": "keyword"
},
"title": {
"type": "text"
},
"comments": {
"type": "nested"
}
}

我把一些文档如下。
{
"id": 1,
"title": "Types",
"comments": [
{
"id": 113,
"contents": "Number"
},
{
"id": 2005,
"contents": "String"
},
{
"id": 317,
"contents": "Boolean"
}
]
}
{
"id": 2,
"title": "Animals",
"comments": [
{
"id": 45,
"contents": "Dog"
},
{
"id": 175,
"contents": "Cat"
},
{
"id": 26,
"contents": "Pig"
}
]
}
{
"id": 3,
"title": "Colors",
"comments": [
{
"id": 97,
"contents": "Red"
},
{
"id": 28,
"contents": "Green"
},
{
"id": 56,
"contents": "Blue"
}
]
}

当我如下使用嵌套查询时,总数仅为3,但我想得到9(全部有关嵌套属性)。
{
"query": {
"nested": {
"path": "comment",
"query": {
"match_all": {}
}
}
}
}

我得到的结果如下。
{
"hits": {
"total": 3,
"hits": [
{
"_source": {
"id": 1,
"title": "Types",
"comments": [...]
}
},
{
"_source": {
"id": 2,
"title": "Animals",
"comments": [...]
}
},
{
"_source": {
"id": 3,
"title": "Colors",
"comments": [...]
}
}
]
}
}

但我希望结果类似于以下格式。
{
"hits": {
"total": 9,
"hits": [
{
"_source": {
"id": 113,
"contents": "Number"
}
},
...,
{
"_source": {
"id": 56,
"contents": "Blue"
}
}
]
}
}

我应该如何编写查询以获取仅9个嵌套属性,如我上面想要的结果?

最佳答案

您需要像这样使用nested inner_hits :

{
"_source": false, <-- add this to prevent the parent doc from showing up
"query": {
"nested": {
"path": "comment",
"query": {
"match_all": {}
},
"inner_hits": {} <-- and this to only show the matching nested documents
}
}
}

关于elasticsearch - 我应该如何编写查询以仅在Elasticsearch中获取嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57958370/

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