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

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:

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:

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

{
  "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

      {
        "_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,
                }
          ]
        }
      }

Hello again.

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

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 ........
}

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