title: How to create subfield keyword for Aggregation in ElasticSearch
body: <p>I am trying to get Aggregation results from ElasticSearch index</p>
  <p>For exmaple , values in my index</p>
  <pre><code>&quot;_source&quot;: {
  &quot;ctry&quot;: &quot;abc&quot;, 
  &quot;totalentry&quot;: 1,
  &quot;entrydate&quot;: &quot;2022-01-06&quot;
  },
  &quot;_source&quot;: {
  &quot;ctry&quot;: &quot;abc&quot;, 
  &quot;totalentry&quot;: 3,
  &quot;entrydate&quot;: &quot;2022-01-07&quot;
  },
  &quot;_source&quot;: {
  &quot;ctry&quot;: &quot;xyz&quot;, 
  &quot;totalentry&quot;: 1,
  &quot;entrydate&quot;: &quot;2022-01-08&quot;
  }
  </code></pre>
  <p>expected Results should be get totalentry based on country</p>
  <pre><code>ctry : abc
  totalentry : 4
  ctry : xyz
  totalentry : 1
  </code></pre>
  <p>My Aggreagtion query</p>
  <pre><code>  QueryBuilder querybuilder = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery(&quot;entrydate&quot;)
                  .gte(&quot;2022-01-01&quot;).lte (&quot;2022-01-31&quot;));      
        TermsAggregationBuilder groupBy = AggregationBuilders.terms(&quot;ctry&quot;).field(&quot;ctry&quot;);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                          .withQuery(querybuilder).addAggregation(groupBy)
                          .build();
         List&lt;Sample&gt; records = elasticsearchRestTemplate.queryForList(searchQuery, Sample.class);
  </code></pre>
  <p>Above aggregation query returning 3 records instead of 2 aggregated results.
  My index properties</p>
  <pre><code>&quot;ctry&quot;: {
  &quot;type&quot;: &quot;keyword&quot;
  </code></pre>
  <p>How to change it to below , so that i hope i will get correct aggregation results</p>
  <pre><code>ctry&quot;: {
  &quot;type&quot;: &quot;text&quot;,
  &quot;fields&quot;: {
  &quot;keyword&quot;: {
  &quot;ignore_above&quot;: 256,
  &quot;type&quot;: &quot;keyword&quot;
  }
  }
  }
  </code></pre>
  <p>My java code</p>
  <pre><code>@Document(indexName=&quot;sample&quot;, createIndex=true, shards = 4)
  public class Sample { 
  
       @Field(type = FieldType.Keyword)
       private String ctry;
  
  </code></pre>