0
votes

I'm new in ElasticSearch and I have a few questions regarding nested object retrieval when a specific condition is matched.

I have a tree-like structure as follow:

{

    "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": []
                    }
                ]
            }
        }
    ]
}

My structure can have more levels.

I have many documents as described above. In order to filter my document I can use the simple query 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"
                    }
                }
            ]
        }
    }
}
  • What I would like to know is, how can I get the nested object that matches my query and their children. I need the object that matched the must inclusive filter. Is that possible?
  • How can I search for a field without specifing the path?

Thanks

# EDIT

My mapping:

{
    "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

2 Answers

0
votes

Yes you can get the matching objects by using inner_hits along with nested query and not the one you added to the question.

Your query will look as below:

{
  "_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"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

I have added link to inner_hits documentation where you can understand how the results will look like.

0
votes

Well, if someone else is facing the same issue my solution was added all child in the same path/level as the parent but keep the mapping with parent and their children. With that, I'm able to search and retrieve the parts of the parent as wanted.