我的索引如下。
{
"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个嵌套属性,如我想要的结果一样?
答案 0 :(得分:1)
您需要像这样使用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
}
}
}