How to combine properties of multiple sibling level nested objects in a single terms aggregations, Elasticsearch

We Have a use case with Elasticsearch, for an index structure, we have multiple nested objects at sibling level,
and for our use case: we need to combine more than one properties of nested objects in a terms aggregation, lets say, Property 1 of a nested object + Property 2 of another nested object as a single term key,

For example, the document structure is,

{
 "OuterProperty1":"value",
 "NestedObject1" : {
              "Property1" : value 1,
              "Property2" : value 2
            },
 "NestedObject2" : {
              "Property1" : value 1,
              "Property2" : value 2
            }
}

and for the terms aggregation with nested objects, i want to use following script,

  "script":{
          "source":"""
           String aggKey= (doc['NestedObject1.Property1'].value.toString())+'_'+(doc['NestedObject2.Property2'].value.toString());
             return aggKey;
          """
        ,
          "lang":"painless"
        },
        "size":10000

and to access properties of different nested objects for the combination key, i need to provide related "paths" of the nested objects, so my question, is there a way to provide multiple paths or with some wildcard way of providing path of multiple sibling level multiple nested objects in an aggregation, and to achieve the combined query result?

What we want to achieve is, something like:

    "buckets" : [
          {
            "key" : "NestedObject1.Property1_NestedObject2.Property2",
            "doc_count" : 14
          },
          {
            "key" : "NestedObject1.Property1_NestedObject2.Property2",
            "doc_count" : 4
          }  ,etc...       
        ]

We've tried with the following queries:
Query 1:

{
    "query":{
    "bool":{
     "some filter..."
  }},
     "aggs":{
    "TopLevelAggs":{
      "nested":{
          "path":"NestedObject1"
        },
      "aggs":{
    "TestTopHitsAggs":{
              "nested": {
                "path": "NestedObject2"
              },
              "aggs":{
            "TopHitsAggs":{
      "terms": {
         "script":{
          "source":"""
           String TestLinkIdD= (doc['NestedObject1.Property1'].value.toString())+'_'+(doc['NestedObject2.Property2'].value.toString());
             return TestLinkIdD;
          """
        ,
          "lang":"painless"
        },
        "size":10000
      }
    }
          }
}
}
}
}
}

-- The above query doesn't seem to provide any results.
--the output we're getting is:

"aggregations" : {
    "ReportedProblemsTopLevelAggs" : {
      "doc_count" : 0,
      "TestTopHitsAggs" : {
        "doc_count" : 0,
        "TopHitsAggs" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ ]
        }
      }
    }
  }

Way 2:
We've also tried to access NestedObject2 with "reverse_nested aggregation", but didnt work with the usecase

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.