# Insert Nested object for indexing in elasticsearch using Java API

**URL:** <https://discuss.elastic.co/t/insert-nested-object-for-indexing-in-elasticsearch-using-java-api/99648>\
**Category:** Elasticsearch\
**Created:** [September 7, 2017, 1:47am UTC](https://discuss.elastic.co/t/insert-nested-object-for-indexing-in-elasticsearch-using-java-api/99648 "2017-09-07T01:47:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pritom\_Ahmed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pritom_ahmed/32/20783_2.png) [@Pritom\_Ahmed](https://discuss.elastic.co/u/Pritom_Ahmed)\
**Post date:** [September 7, 2017, 1:47am UTC](https://discuss.elastic.co/t/insert-nested-object-for-indexing-in-elasticsearch-using-java-api/99648/1 "2017-09-07T01:47:39Z")

</div>

Hi,

I have the following two classes.

```
public class Authors implements Serializable {
    private String name;
    private String surname;
    private int numberOfArticles;
}

public class Blog implements Serializable {
    private String name;
    private List<Authors> authors;
}

```

I use the following code to populate some test data.

```
public static XContentBuilder getXContentBuilder() throws IOException {
        Blog blog=new Blog();
        blog.setName("www.tfasun.com");
        blog.setAuthors(new ArrayList<Authors>());
        blog.getAuthors().add(new Authors("tarik","fasun",3));
        blog.getAuthors().add(new Authors("tarik1","fasun1",4));
        XContentBuilder contentBuilder = null;
        try {
            contentBuilder = jsonBuilder().startObject();
            contentBuilder.field("name", blog.getName());
            contentBuilder.startArray("authors");
            for (Authors authors : blog.getAuthors()) {
                contentBuilder.startObject();
                contentBuilder.field("name", authors.getName());
                contentBuilder.field("surname", authors.getSurname());
                contentBuilder.field("numberOfArticles", authors.getNumberOfArticles());
                contentBuilder.endObject();
            }
            contentBuilder.endArray().endObject();
        } catch (IOException ex) {
            throw new RuntimeException("Error", ex);
        }
        return contentBuilder;
    }

```

Then I use elasticsearch's Java API to insert them in elasticsearch.

```
XContentBuilder contentBuilder = Utilities.getXContentBuilder();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("tfasun", "document_type");
indexRequestBuilder.setSource(contentBuilder).execute().actionGet();

```

I end up with the following index mapping.

```
{
  "tfasun" : {
    "aliases" : { },
    "mappings" : {
      "document_type" : {
        "properties" : {
          "authors" : {
            "properties" : {
              "name" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "numberOfArticles" : {
                "type" : "long"
              },
              "surname" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          },
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1504748301549",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "_AJmgF6fSDmDupf84FWYkw",
        "version" : {
          "created" : "5050199"
        },
        "provided_name" : "tfasun"
      }
    }
  }
}

```

But I need the attributes of the class "Author" to be nested so that I can make nested queries as mentioned in the following document

[https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#\_using\_literal\_nested\_literal\_fields\_for\_arrays\_of\_objects](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#_using_literal_nested_literal_fields_for_arrays_of_objects)

How can I use the Java API of elasticsearch and still able to create a mapping so that I can make nested queries ?

Thanks  
Pritom

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [September 9, 2017, 2:27pm UTC](https://discuss.elastic.co/t/insert-nested-object-for-indexing-in-elasticsearch-using-java-api/99648/2 "2017-09-09T14:27:57Z")

</div>

There is no auto-detection for nested type. You need to define the mapping with the proper nested type prior to indexing.

---

<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:** [October 7, 2017, 2:28pm UTC](https://discuss.elastic.co/t/insert-nested-object-for-indexing-in-elasticsearch-using-java-api/99648/3 "2017-10-07T14:28:02Z")

</div>

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