Spring Data Elasticsearch with Script Field with Kotlin Data Class results in Jackson serialization issue

Question

When trying to fetch a query result from Elasticsearch and map it to a Kotlin data class which uses the @org.springframework.data.elasticsearch.annotations.ScriptedField annotation the result instance seems to be created with only the script field, but not the other fields to be fetched at the same time, and therefore leads to a .MissingKotlinParameterException. The query is done via the ElasticsearchRepository#search method, which comes with Spring Data Elasticsearch, here is the relevant code piece:

    val searchQuery = NativeSearchQueryBuilder()
            .withQuery(geoDistanceQuery("location").point(53.0, 10.0).distance("100km"))
            .withScriptField(ScriptField("distance",
                    Script(ScriptType.INLINE, "expression", "doc['location'].arcDistance(params.latitude, params.longitude)", emptyMap())))
            .withFilter(boolQuery().must(existsQuery("title")))
            .withSort(SortBuilders.geoDistanceSort("location", 53.0, 10.0).order(SortOrder.ASC))
            .withPageable(PageRequest.of(0, 10))
            .build()

    val nearby = eventRepository.search(searchQuery)

The stack trace with a bit more context:

    Caused by: com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class de.nava.demo.events.model.Event] value failed for JSON property id due to missing (therefore NULL) value for creator parameter id which is a non-nullable type
 at [Source: (String)"{"distance":53.542239465750754}"; line: 1, column: 31] (through reference chain: de.nava.demo.events.model.Event["id"])
    at com.fasterxml.jackson.module.kotlin.KotlinValueInstantiator.createFromObjectWith(KotlinValueInstantiator.kt:53)
    at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:189)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:487)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1265)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:325)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4011)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)

Could someone please shed some light on how you could be able to construct a complete instance with all fields populated at once?

Versions used:

  • Spring Boot Version 2.0.0.M3
  • Spring Data Elasticsearch 3.0.0.RC2 (Elasticsearch 5.5.0)
  • Jackson Module Kotlin 2.9.0

Show source
| jackson   | spring   | kotlin   | spring-data   | spring-data-elasticsearch   2017-09-05 21:09 0 Answers

Answers to Spring Data Elasticsearch with Script Field with Kotlin Data Class results in Jackson serialization issue ( 0 )

Leave a reply to - Spring Data Elasticsearch with Script Field with Kotlin Data Class results in Jackson serialization issue

◀ Go back