Uploaded image for project: 'Spring Data Elasticsearch'
  1. Spring Data Elasticsearch
  2. DATAES-936

Take id property from the source when deserializing an entity

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.0.4 (Neumann SR4)
    • Component/s: None
    • Labels:
      None

      Description

      When trying to search for data through spring-data-elasticsearch (see below for an example) but the data class it's supposed to create instances of happens to have an id field, spring-data-elasticsearch will use the _id field of the document instead of the id field of the source, which in this example will result in a NumberFormatException (see attachment for exception) since the class id field is an integer, while the elasticsearch "_id" field is not (even the "id" field in the mapping of elasticsearch is an integer). I'm not entirely sure if this is what actually happens, but it very much seems like it.

      Setup:

      This is what spring should deserialize the searchhits from elasticsearch to (Yes, it combines jpa with elasticsearch, haven't had an issue with that yet though).

      @Entity
      @Document(createIndex = false, indexName = "exampleIndex")
      @Table(name = "someTable")
      public class Person implements Serializable {
      
      @Id
      @Field
      @Column
      private Integer id;
      
      @Field
      @Column
      private String name;
      

      This is how I request the data from elasticsearch through spring:

      NativeSearchQuery nativeQuery = new NativeSearchQueryBuilder.withFilter(someFilter()).withPageable(somePageable()).build();
      SearchHits<Person> result = restTemplate.search(nativeQuery, Person.class);

      However, I get an exception (longer version as attachment):

      java.lang.NumberFormatException: For input string: "CaFHtHMBfczCkwGFm0zd" 

      Thankfully, spring allows to send the elasticsearch request to the console. So I can easily execute the request in kibana and check its result:

      {
          "took": 0,
          "timed_out": false,
          "_shards": {
              "total": 1,
              "successful": 1,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": {
                  "value": 10000,
                  "relation": "gte"
              },
              "max_score": 1.0,
              "hits": [
                  {
                      "_index": "exampleIndex",
                      "_type": "_doc",
                      "_id": "CaFHtHMBfczCkwGFm0zd",
                      "_score": 1.0,
                      "_source": {
                          "id": 123,
                          "name": "Example"
                      }
                  }
              ]
          }
      }
      

      As you can see, _id contains the value spring-data-elasticsearch is failing at, id on the other hand has an integer value as the data class suggested.

      This makes spring-data-elasticsearch basically useless for my case (it worked great in spring-boot 2.2.0 but with the 2.3.4 release (probably prior to that as well), it stopped working.

        Attachments

          Activity

            People

            Assignee:
            sothawo sothawo
            Reporter:
            Kev Kevin Ullrich
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: