ElasticSearch查询中的嵌套对象检索

时间:2019-01-07 20:54:57

标签: elasticsearch

我是ElasticSearch的新手,我对匹配特定条件时的嵌套对象检索有一些疑问。

我的树状结构如下:

{

    "id": 4,
    "sora": [
        {
            "pContext": {
                "context": {
                    "sT": "D3",
                    "uT": "ST"
                },
                "entities": [
                    {
                        "name": "premium",
                        "bName": "premium",
                        "fT": "site",
                        "eT": "F_P",
                        "children": [
                            {
                                "name": "capa",
                                "bName": "capa",
                                "fT": "site",
                                "eT": "FFT",
                                "children": []
                            },
                            {
                                "name": "code",
                                "bName": "Codes",
                                "fT": "site",
                                "eT": "FFT",
                                "children": []
                            },
                            {
                                "name": "selection A",
                                "fT": "site",
                                "eT": "SELECTION_A",
                                "children": [
                                    {
                                        "name": "A1",
                                        "fT": "site",
                                        "eT": "ADD",
                                        "children": []
                                    },
                                    {
                                        "name": "A2",
                                        "fT": "site",
                                        "eT": "ADD",
                                        "children": []
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        },
        {
            "pContext": {
                "context": {
                    "sT": "D2",
                    "uT": "ST"
                },
                "entities": [
                    {
                        "name": "112",
                        "bName": "112",
                        "eT": "D_TYPE",
                        "children": []
                    }
                ]
            }
        }
    ]
}

我的结构可以有更多的层次。

我有很多如上所述的文件。为了过滤我的文档,我可以使用简单的查询sintax:

{
    "_source": {
        "excludes": [
            "*.context"
        ]
    },
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "sora.pContext.context.sT": "D3"
                    },
                    "match": {
                        "sora.pContext.entities.name": "premium"
                    },
                    "match": {
                        "sora.pContext.entities.fT": "site"
                    }
                }
            ]
        }
    }
}
  • 我想知道的是,如何获取嵌套对象 匹配我的查询和他们的孩子。我需要匹配的对象 必须包含在内的过滤器。有可能吗?
  • 如何在不指定路径的情况下搜索字段?

谢谢

#EDIT

我的映射:

{
    "mappings": {
        "abc": {
            "properties": {
                "id": {
                    "type": "integer"
                },
                "sora": {
                    "type": "nested",
                    "properties": {
                        "pContext": {
                            "type": "nested",
                            "properties": {
                                "context": {
                                    "type": "nested",
                                    "properties": {
                                        "sT": {
                                            "type": "text"
                                        },
                                        "uT": {
                                            "type": "text"
                                        }
                                    }
                                },
                                "entities": {
                                    "type": "nested",
                                    "properties": {
                                        "name": {
                                            "type": "text"
                                        },
                                        "bName": {
                                            "type": "text"
                                        },
                                        "fT": {
                                            "type": "text"
                                        },
                                        "eT": {
                                            "type": "text"
                                        },
                                        "children": {
                                            "type": "object"                                    
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

是的,您可以使用inner_hitsnested query来获得匹配的对象,而不是您添加到问题中的对象。

您的查询将如下所示:

{
  "_source": {
    "excludes": [
      "*.context"
    ]
  },
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "inner_hits": {},
            "path": "sora.pContext",
            "query": {
              "bool": {
                "must": [
                  {
                    "nested": {
                      "path": "sora.pContext.context",
                      "query": {
                        "bool": {
                          "must": [
                            {
                              "match": {
                                "sora.pContext.context.sT": "D3"
                              }
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    "nested": {
                      "path": "sora.pContext.entities",
                      "query": {
                        "bool": {
                          "must": [
                            {
                              "match": {
                                "sora.pContext.entities.name": "premium"
                              }
                            },
                            {
                              "match": {
                                "sora.pContext.entities.fT": "site"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

我已经添加了指向inner_hits文档的链接,您可以在其中了解结果的样子。

答案 1 :(得分:0)

好吧,如果其他人也面临同样的问题,我的解决方案是将所有子代添加到与父代相同的路径/级别,但保持与父代及其子代的映射。这样,我便可以根据需要搜索和检索父级的部分。

相关问题