Description
This is a feature request.
I'm migrating a platform from Apache Solr to ElasticSearch and I'm struggling in creating dynamic fields like Apache Solr used to have. I need facets (aka "aggregations" in ElasticSearch) on these dynamic fields. On Solr that was easily done with an annotation on a Map:
@Dynamic("*_str")
@Field("attributes")
private Map<String, String> attributes;
Instead, with ElasticSearch, I need to create a dynamic template for the index like this:
{
"mappings": {
"dynamic_templates": [
{
"_str": {
"match": "*_str",
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
The main issue is that when I now save a document having
@Field("attributes")
private Map<String, String> attributes;
, Spring is inputting a payload like
{
// other document fields above
"attributes": {
"code1_str":"value1",
"code2_str":"other-value"
},
// other document fields below
}
and I can't do any aggregation query to get a result like
{
"aggregations": {
"code1_str": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "value1",
"doc_count": 1
},
{
"key": "value2",
"doc_count": 1
}
]
},
"code2_str": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "other-value",
"doc_count": 2
}
]
}
}
}
The workaround for this issue is atm a custom Spring Converter<S, T>
for the entire document: immagine having to convert 20 or 30 fields.. this would take a lot of work just because of 1 field serialization/deserialization issue.
A solution could be found Instead in having something like @JsonUnwrapped
annotation from Jackson library to remove the attributes level from the payload, leading to Spring inputting a payload like:
{
// other document fields above
"code1_str":"value1",
"code2_str":"other-value"
// other document fields below
}
Could this be feasible in an upcoming release?