我有这样的映射
{
"properties" : {
"text" : {
"type" : "nested",
"properties": {
"header": {
"type" : "text"
},
"nodes": {
"type": "nested",
"properties": {
"subheader" : {
"type" : "text"
},
"nodes" : {
"type" : "nested"
}
}
}
}
}
}
}
我想返回的文档中,最里面的子
text.nodes.nodes
包含一些术语“X”,并且仅当该子项的父text.nodes.subheader
包含一些术语“Y”时这是一个reproducible gist,我希望在其中搜索子级中的“SUPERFRAGILISTIC”以及子标题中的“Restrictions”应仅返回文档2。
现在正在返回两个文档。
从要点来看,我的查询现在看起来像:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "text.nodes",
"query": {
"bool": {
"must": [
{
"match": {
"text.nodes.subheader": {
"query": "Restrictions"
}
}
}
]
}
}
}
},
{
"nested": {
"path": "text.nodes.nodes",
"query": {
"bool": {
"must": [
{
"match": {
"text.nodes.nodes.content": {
"query": "SUPERFRAGILISTIC"
}
}
}
]
}
}
}
}
]
}
}
}
最佳答案
为了找到单个文档的相同json节点中包含两个条件的结果,您需要将第二个条件嵌套在第一个条件中。通过此查询,您将仅获得document2:
{
"query":{
"bool":{
"must":[
{
"nested":{
"path":"text.nodes",
"query":{
"bool":{
"must":[
{
"match":{
"text.nodes.subheader":{
"query":"Restrictions"
}
}
},
{
"nested":{
"path":"text.nodes.nodes",
"query":{
"bool":{
"must":[
{
"match":{
"text.nodes.nodes.content":{
"query":"SUPERFRAGILISTIC"
}
}
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
关于elasticsearch - 嵌套查询查找包含项X的子项和包含项Y的父项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48218441/