# Organisation of the document to index in the java API with a nested field

**URL:** <https://discuss.elastic.co/t/organisation-of-the-document-to-index-in-the-java-api-with-a-nested-field/324400>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [February 1, 2023, 8:50am UTC](https://discuss.elastic.co/t/organisation-of-the-document-to-index-in-the-java-api-with-a-nested-field/324400 "2023-02-01T08:50:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![QuentinV](https://avatars.discourse-cdn.com/v4/letter/q/2bfe46/32.png) [@QuentinV](https://discuss.elastic.co/u/QuentinV)\
**Post date:** [February 1, 2023, 8:50am UTC](https://discuss.elastic.co/t/organisation-of-the-document-to-index-in-the-java-api-with-a-nested-field/324400/1 "2023-02-01T08:50:40Z")

</div>

Hello.  
In my project, I success to connect, create and index documents to an elasticsearch with the new java API but I want to index a file with an nested field and I don't find any information about it.  
Can you explain how to organise my class product ?  
I try with an array or a list, but it's not working

Thanks and have a good day.

My code:

_my class product:_

```auto
public class Product2 {
    private String sku2;
    private String name2;
    private double price2;

    public Product2() {}
    public Product2(String sku, String name, double price) {
        this.sku2 = sku;
        this.name2 = name;
        this.price2 = price;
    }

    the getter and the setter.......
}

```

_my builder:_

```auto
public void indexing() throws Exception{
        Product product = new Product("bk-7", "City bike", 123.0);
        ElasticsearchClient esClient = ConnectingTest.createSecureClientFingerPrint();
        IndexResponse response = esClient.index(i -> i
                .index("test101")
                .document(product)
        );
        System.out.println(response);
    }

```

The result in elastic

`GET test101/_search`

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "test101",
        "_id": "6lm-CIYBhu3QYJ_OsVQk",
        "_score": 1,
        "_source": {
          "sku": "bk-7",
          "name": "City bike",
          "price": 123
        }
      }
    ]
  }
}

```

_the result I want_

```auto
      {
        "_index": "test101",
        "_id": "6lm-CIYBhu3QYJ_OsVQk",
        "_score": 1,
        "_source": {
          "sku": "bk-7",
          "name": "City bike",
          "price": 123,
          "aNestedField": [
                {
           "skuInNestedField1": "bk-7",
          "nameInNestedField1": "City bike",
          "priceInNestedField1": 123,
                },
                {
           "skuInNestedField2": "bk-7",
          "nameInNestedField2": "City bike",
          "priceInNestedField2": 123,
                }
          ]
        }
      }

```

---

<div class="post-metadata">

**Author:** ![QuentinV](https://avatars.discourse-cdn.com/v4/letter/q/2bfe46/32.png) [@QuentinV](https://discuss.elastic.co/u/QuentinV)\
**Post date:** [February 1, 2023, 9:22am UTC](https://discuss.elastic.co/t/organisation-of-the-document-to-index-in-the-java-api-with-a-nested-field/324400/2 "2023-02-01T09:22:00Z")

</div>

Hello again.

I find the solution, sorry for the trouble.  
it’s simpler than I think

```auto
public class Product {
    private String sku;
    private String name;
    private double price;
    private Product2 Product2;

    public Product(String sku, String name, double price) {
        this.sku = sku;
        this.name = name;
        this.price = price;
        this.Product2 = new Product2("bk-9", "space bike", 150000.0);
    }

getter and setter ........
}

```

---

<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:** [March 1, 2023, 9:22am UTC](https://discuss.elastic.co/t/organisation-of-the-document-to-index-in-the-java-api-with-a-nested-field/324400/3 "2023-03-01T09:22:52Z")

</div>

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