5

I am currently converting a project I did from Spring Data with Hibernate to Spring Data with ElasticSearch.

Before, I was able to auto generate the id of my entity using the annotation @GeneratedValue(strategy = GenerationType.IDENTITY). This way, when the entity is saved, an id number is generated for it.

However, I can't seem to find an annotation that ElasticSearch uses to achieve the same result.

The entity using Hibernate annotation:

@Entity
@Table(name = "person")
public class Person {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

Is there an ElasticSearch equivalent to the @GeneratedValue annotation? If not, what is the optimal way to populate the id field when using ElasticSearch?

pike
  • 671
  • 3
  • 9
  • 23

3 Answers3

0

I just ran into the same issue and looks like there is no annotation as such but we can write a component by defining a custom BeforeConvertCallback that sets an id if necessary.

@Component
public class BookBeforeConvertCallback implements BeforeConvertCallback<Book> {

    @Override
    public Book onBeforeConvert(Book book, IndexCoordinates indexCoordinates) {

        if (book.getId() == null) {
            book.setId(UUIDs.randomBase64UUID());
        }

        return book;
    }
}

Ref: https://github.com/spring-projects/spring-data-elasticsearch/issues/616#issuecomment-752866566

Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
-2

The documentation on https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-mapping-entity does seem to indicate that @GeneratedValue should work correctly. Are you using the proper integration libraries?

Piotr Wilkin
  • 3,446
  • 10
  • 18
  • 2
    Thanks for answering, but I am searching for an ElasticSearch annotation that can act as an equivalent to @GeneratedValue. Do you happen to have any experience with ElasticSearch in this area? – pike Nov 14 '17 at 15:35
  • Sorry, I understood that you were using Hibernate with ElasticSearch. If you're using the pure ElasticSearch API, then just use POST as documented here: https://www.elastic.co/guide/en/elasticsearch/guide/current/index-doc.html – Piotr Wilkin Nov 14 '17 at 15:40
  • @PiotrWilkin Hibernate only works with SQL databases. It does not work with ElasticSearch. The question is about Spring Data Elasticsearch, not Hibernate, and not "pure Elasticsearch". – Mark B Mar 01 '20 at 15:55
-2

I had the same problem and solved it using this code:

@Entity
@Table(name = "person")
public class Person {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @GeneratedValue(generator="generator")
    @org.springframework.data.annotation.Id
    private long id;
Engineero
  • 12,340
  • 5
  • 53
  • 75
aditya gupta
  • 403
  • 2
  • 10
  • 1
    This answer may work for Spring Data JPA, but it does not work for Spring Data Elasticsearch, which is what the question is about. – Mark B Mar 01 '20 at 15:57