如何在 elasticsearch 的单独行中显示 "ALL" 对象中的嵌套文档?
How to display "ALL" the nested documents in an object in separate rows from elasticsearch?
我有一个以下形式的嵌套对象:
{
"name": "Multi G. Enre",
"books": [
{
"name": "Guns and lasers",
"genre": "scifi",
"publisher": "orbit"
},
{
"name": "Dead in the night",
"genre": "thriller",
"publisher": "penguin"
}
]
}
我针对上述文档尝试了以下 JSON 查询:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "books",
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"term": {
"books.publisher": "penguin"
}
},
{
"term": {
"books.genre": "thriller"
}
}
]
}
}
}
}
}
}
}
}
所以,我希望看到第二个嵌套文档,即 "Dead in the night" 作为结果,但是对于任何内容,我只搜索第一个文档,即 "Guns and lasers" 显示在 table 中在 elasticsearch head 插件中。
那么,有什么方法可以根据搜索查询分别显示嵌套文档,而不仅仅是第一个文档?
我是 elasticsearch 的新手,所以非常感谢任何类型的回复。谢谢!
您需要在查询中使用 inner_hits
。
此外,如果您只想检索匹配的嵌套文档而不需要其他任何内容,您可以将 "_source":["books"]
添加到您的查询中,并且只返回匹配的嵌套书籍,没有其他内容。
更新
抱歉,我误解了你的评论。您可以添加 "_source": false
并且不会返回顶级文档。仅嵌套匹配文档。
我有一个以下形式的嵌套对象:
{
"name": "Multi G. Enre",
"books": [
{
"name": "Guns and lasers",
"genre": "scifi",
"publisher": "orbit"
},
{
"name": "Dead in the night",
"genre": "thriller",
"publisher": "penguin"
}
]
}
我针对上述文档尝试了以下 JSON 查询:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "books",
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"term": {
"books.publisher": "penguin"
}
},
{
"term": {
"books.genre": "thriller"
}
}
]
}
}
}
}
}
}
}
}
所以,我希望看到第二个嵌套文档,即 "Dead in the night" 作为结果,但是对于任何内容,我只搜索第一个文档,即 "Guns and lasers" 显示在 table 中在 elasticsearch head 插件中。 那么,有什么方法可以根据搜索查询分别显示嵌套文档,而不仅仅是第一个文档? 我是 elasticsearch 的新手,所以非常感谢任何类型的回复。谢谢!
您需要在查询中使用 inner_hits
。
此外,如果您只想检索匹配的嵌套文档而不需要其他任何内容,您可以将 "_source":["books"]
添加到您的查询中,并且只返回匹配的嵌套书籍,没有其他内容。
更新
抱歉,我误解了你的评论。您可以添加 "_source": false
并且不会返回顶级文档。仅嵌套匹配文档。