# Create TermAggregation with the wildcard field in Java code

**URL:** <https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424>\
**Category:** Elasticsearch\
**Created:** [January 14, 2022, 3:19pm UTC](https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424 "2022-01-14T15:19:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiffany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiffany/32/100337_2.png) [@Tiffany](https://discuss.elastic.co/u/Tiffany)\
**Post date:** [January 14, 2022, 3:19pm UTC](https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424/1 "2022-01-14T15:19:55Z")

</div>

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

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

```

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

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

```

The data looks like this:

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

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 14, 2022, 3:37pm UTC](https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424/2 "2022-01-14T15:37:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tiffany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiffany/32/100337_2.png) [@Tiffany](https://discuss.elastic.co/u/Tiffany)\
**Post date:** [January 14, 2022, 3:49pm UTC](https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424/3 "2022-01-14T15:49:39Z")

</div>

Hi Mark, thank you for the quick reply 🙂  
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...

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

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 14, 2022, 5:32pm UTC](https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424/4 "2022-01-14T17:32:16Z")

</div>

If you have content that you'd like to centralise in one field for analysis then using the [copy\_to](https://www.elastic.co/guide/en/elasticsearch/reference/7.16/copy-to.html) 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.

---

<div class="post-metadata">

**Author:** ![Tiffany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tiffany/32/100337_2.png) [@Tiffany](https://discuss.elastic.co/u/Tiffany)\
**Post date:** [January 17, 2022, 7:50am UTC](https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424/5 "2022-01-17T07:50:39Z")

</div>

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?

```auto
{
        "_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" : {
                       },
                 }
            }
}

```

```auto
Result: {"value1", "value2", "value3", "value5", "value6"} or 5 counts

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 14, 2022, 7:51am UTC](https://discuss.elastic.co/t/create-termaggregation-with-the-wildcard-field-in-java-code/294424/6 "2022-02-14T07:51:16Z")

</div>

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