Skip to content

@ScriptedFields & Kotlin data classes [DATAES-915] #1488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Aug 27, 2020 · 2 comments · Fixed by #1802
Closed

@ScriptedFields & Kotlin data classes [DATAES-915] #1488

spring-projects-issues opened this issue Aug 27, 2020 · 2 comments · Fixed by #1802
Assignees
Labels
in: mapping Mapping and conversion infrastructure type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Alfonso Gonzalez Luis opened DATAES-915 and commented

I found that when I have a document that looks like, this:

 

@Document(indexName = "test")
data class Test(
 @Id var id: String
 @ScriptedField var distance: Double
)

 

and try to make a search like

 

elasticsearchOperations.search(search, Test.class)

 

the conversion fails on

MappingElasticsearchConverter#readEntity

because it tries to instantiate the target entity without all the required parameters.

Looking further into the code, I saw that it tries to add the script_fields later on, with  the populateScriptFields method, but only if this condition is not met, which led me to modify my class to look like this

@Document(indexName = "test")
data class Test(@Id var id: String) {
 @ScriptedField val distance: Double
}

 

Since this is an issue that can occur both in Kotlin & Java, I think having some documentation of this would be great. It could help other people that might get into the same issue I did.

The other solution could be to combine the fields of the Document with the source and let ElasticsearchPropertyValueProvider handle all of it. But that one requires much more work.

 

 


Affects: 4.0.3 (Neumann SR3)

@spring-projects-issues
Copy link
Author

sothawo commented

It would be better if the instantiaton of the data class would work as you thought it would. I'll need to have a deeper look at this

@spring-projects-issues
Copy link
Author

Alfonso Gonzalez Luis commented

Thanks for looking into this, I also think that solution is a better approach. Btw, I found another quirk that might be solved alongside this initial issue.

If the scripted_field is not a primitive type but rather a complex type as a GeoPoint the populateScriptFields does not work as expected. eg:
@Document(indexName = "test")
data class Test(@Id var id: String) {
@ScriptedField val point: GeoPoint}
and let's suppose that our scripted field return something that looks like

 

{

 "lon": -171.050373,
 "lat": 22.340608
}

 

The reflection will fail, so I solve this by using a field to hold the raw data and a second field that constructs the proper object by delegation.
@Document(indexName = "test")
data class Test(@Id var id: String) {
@ScriptedField private val _point: Map<String,Double>
val point: Money by lazy {
return GeoPoint(_point["lat"], _point["lng"])
}}
 

Not the cleanest solution, but works for me. Let me know if I can help somehow

 

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: mapping Mapping and conversion infrastructure labels Dec 31, 2020
@sothawo sothawo self-assigned this May 6, 2021
sothawo added a commit that referenced this issue May 7, 2021
…ds and Kotlin data classes.

Original Pull Request #1802 
Closes #1488
@sothawo sothawo added this to the 4.3 M1 (2021.1.0) milestone May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure type: enhancement A general enhancement
Projects
None yet
2 participants