# Spring + ElasticSearch - How to bulk create or update document with autogenerated string \_id/id

**URL:** <https://discuss.elastic.co/t/spring-elasticsearch-how-to-bulk-create-or-update-document-with-autogenerated-string-id-id/316352>\
**Category:** Elasticsearch\
**Created:** [October 11, 2022, 3:44pm UTC](https://discuss.elastic.co/t/spring-elasticsearch-how-to-bulk-create-or-update-document-with-autogenerated-string-id-id/316352 "2022-10-11T15:44:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Java2avaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/java2avaj/32/99497_2.png) [@Java2avaj](https://discuss.elastic.co/u/Java2avaj)\
**Post date:** [October 11, 2022, 3:44pm UTC](https://discuss.elastic.co/t/spring-elasticsearch-how-to-bulk-create-or-update-document-with-autogenerated-string-id-id/316352/1 "2022-10-11T15:44:12Z")

</div>

currently, we are forcing the \_id of the Elasticsearch documents by setting the id field. This simplifies the design, as it allows to perform updates and/or creation of new documents without previously knowing whether a document is already present in the index.

For example, this is the class representing the document:

```auto
@Data
@Document(indexName = "person")
public class Person {

  @Id
  private String id;
  
  private String name;
}

```

and this is the method for bulk create or update:

```auto
 public void bulkCreateOrUpdate(final List<Person> personList) {

    List<UpdateQuery> updateList = new ArrayList<>();

    for (Person person : personList) {
      UpdateQuery updateQuery = UpdateQuery.builder(person.getId())
          .withDocument(this.operations.getElasticsearchConverter().mapObject(person))
          .withDocAsUpsert(true)
          .build();
      updateList.add(updateQuery);
    }

    this.operations.bulkUpdate(updateList,
        this.operations.getIndexCoordinatesFor(this.entityClass));
  }

```

When saved, this is what it produces:

```auto
  {
    "_index": "person",
    "_type": "_doc",
    "_id": "10001",
    "_score": 1.0,
    "_source": {
        "_class": "com.app.Person",
        "id": "10001",
        "name": "Miguelito",
    }

```

How/what to modify the code so that the \_id is autogenerated during saving? Is there an annotation that can be used on id field or is there a need to modify the bulkCreateOrUpdate implementation? The expected output is this:

```auto
 {
        "_index": "person",
        "_type": "_doc",
        "_id": "qwrqwr3242frw",
        "_score": 1.0,
        "_source": {
            "_class": "com.app.Person",
            "name": "Miguelito",
        }

```

Please advice.

Thanks!

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [October 11, 2022, 4:49pm UTC](https://discuss.elastic.co/t/spring-elasticsearch-how-to-bulk-create-or-update-document-with-autogenerated-string-id-id/316352/2 "2022-10-11T16:49:00Z")

</div>

Hi @Java2avaj

Try this:

```auto
@id
@JsonIgnore
private String id;

```

---

<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:** [November 8, 2022, 4:49pm UTC](https://discuss.elastic.co/t/spring-elasticsearch-how-to-bulk-create-or-update-document-with-autogenerated-string-id-id/316352/3 "2022-11-08T16:49:10Z")

</div>

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