title: Spring Data Elastic Search with Nested Fields and mapping
body: <p>I am using spring-data-elasticsearch and elasticsearch together to query documents. I'd like to do nested queries on nested documents.</p>
  
  <p>I have this in java :</p>
  
  <pre><code>@Document(indexName = "as", type = "a", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
  class A {
  
       @Id
       private String Id;
  
       @Field(type = String, index = analyzed, store = true)
       private String field1;
  
       // ... Many more Fields.
  
       @NestedField(type = FieldType.Object, index = analyzed, store = true, dotSuffix = "accounts")
       private List&lt;B&gt; bs;
  
       // ... getters and setters
  }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>class B { // some normal pojo }
  </code></pre>
  
  <p>When I let spring-data do the mapping, I get : </p>
  
  <pre><code>"a": {
      "properties": {
          "bs": {
              "properties": {
                  "someBProperty": {
                      "type": "string"
                  },
                  "BId": {
                      "type": "string"
                  }
              }
          },
          "id": { ... },
          ...
  }
  </code></pre>
  
  <p>When I am trying to query the document, I get the classical inner vs nested documents problems and it doesn't recognize the nested element.</p>
  
  <p>When I try to update mapping to use nested document I get "can't be changed from non-nested to nested".</p>
  
  <p>Should I tell spring-data-es somehow that @NestedField => type: "nested" into mapping ? Is there a way to add custom mapping to spring-data when it create index &amp; mapping ?</p>
  
  <p>Also, I am initializing indexes via :</p>
  
  <pre><code>elasticsearchTemplate.deleteIndex(A.class);
  elasticsearchTemplate.createIndex(A.class);
  elasticsearchTemplate.putMapping(A.class);
  elasticsearchTemplate.refresh(A.class,true);
  </code></pre>
  
  <p>And then querying using spring-data repository : </p>
  
  <pre><code>QueryBuilder builder = QueryBuilders.nestedQuery( "bs", QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("as.field1", "A1")).must(QueryBuilders.matchQuery("as.field2", "B1")));
  
  Iterable&lt;DenormalizedRelationshipDocument&gt; res = aRepository.search(builder);
  </code></pre>
  
  <p>Here the res has 0 element in the Iterable but via REST I get the error on the nested query not supported (as I don't have it in mapping).</p>
  
  <p>finally,</p>
  
  <p>Does Spring-Data-ElasticSearch supports nested mappings via ES QueryBuilders API ? When should I make that mapping happens ?</p>