Search code examples
spring-dataspring-data-elasticsearch

How to flush data to Spring data elastic search


I run this testcase

Envelope envelope = new Envelope();
envelope.setId("1");
Envelope saved = envelopeRepository.save(envelope);
assertThat(saved.getId()).isEqualTo("1");

saved and envelope are the same object/reference! So even when the index is incorrect, the test still passes! How to fix this issue?

enter image description here

org.elasticsearch.client.RestClient - request [HEAD http://localhost:9200/null?ignore_throttled=false&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=false]

Solution

  • Why should the saved entity be a new object? Elasticsearch does not return a document when saving, so there is no need to create a new object here.

    What Spring Data Elasticsearch does is update this object with the information returned from the index operation:

    • the id if it was not already set on the object but was created by Elasticsearch
    • version if the entity has a version property
    • seq_no and primary_term if the entity has such a property

    If the indexing fails, an exception is thrown.

    What do you mean with

    when the index is incorrect

    And what has the HEAD request do do with this?