Create TermAggregation with the wildcard field in Java code

Hi, I want to create a termAggregation query with wildcard in Java API. It works in Elasticsearch console, but not in Java code. Would you mind shedding some lights? Thank you!

Below query works in console

{
  "aggregations": {
    "distinct_fieldNames": {
      "terms": {
        "field": "profileContent.*.fieldName.keyword"
      }
    }
  }

But it doesn't work when I translate it in Java code

TermsAggregationBuilder aggregationBuilder = AggregationBuilders
                                                            .terms("distinct_fieldNames")
                                                            .field("profileContent.*.fieldName.keyword");

The data looks like this:

{
        "_index" : "profile",
        "_source" : { 
              "profileContent" : {
                    "profileId1" : {
                         "fieldName" : "value"
                       }
                 }
            }
}

Hi Tiffany,

Below query works in console

I don't think it should. The terms aggregation does not do anything special with wildcard characters - they are treated like any other character and assumed to be a part of the field name.

Hi Mark, thank you for the quick reply :slight_smile:
Would you mind sharing how to get the distinct count in the wildcard field? I thought about using cardinality, but it doesn't work unfortunately...

CardinalityAggregationBuilder aggregationBuilder = AggregationBuilders
                    .cardinality("distinct")
                    .field("profileContent.*.fieldName.keyword");

If you have content that you'd like to centralise in one field for analysis then using the copy_to setting in the leaf fields would be a way to create a shared field full of values you can aggregate on.
This would require reindexing.

Thanks for the answer, but I'm not sure if this is a bit too complicated to solve my case. I just want to create a distinct aggregation in a wildcard field (profileContent.*.fieldName in the following case), which contains non-null value. Do you maybe have some other ideas I could try?

{
        "_index" : "profile",
        "uuid": "0001",
        "_source" : { 
              "profileContent" : {
                    "profileId1" : {
                         "fieldName" : "value1"
                       },
                    "profileId2" : {
                         "fieldName" : "value2"
                       },
                    "profileId3" : {
                         "fieldName" : "value3"
                       },
                    "profileId4" : {
                       },
                 }
            }
}, {
        "_index" : "profile",,
        "uuid": "0002",
        "_source" : { 
              "profileContent" : {
                    "profileId1" : {
                         "fieldName" : "value1"
                       },
                    "profileId5" : {
                         "fieldName" : "value5"
                       },
                    "profileId6" : {
                         "fieldName" : "value6"
                       },
                    "profileId7" : {
                       },
                 }
            }
}
Result: {"value1", "value2", "value3", "value5", "value6"} or 5 counts

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