[英]Spring Data Elasticsearch dynamic mapping. Map property as key-value?
There is an elastic index that the application generates.应用程序会生成一个弹性索引。 The mapping of an object into an index is described, and then there was a need to create fields in the index dynamically (ie, fields that are not in the described mapping).描述了 object 到索引的映射,然后需要在索引中动态创建字段(即,不在所述映射中的字段)。 Mapping example:映射示例:
@Document (indexName = "product", createIndex = true)
public class Product {
@Id
@Field (name = "id", type = FieldType.Long)
private Long id;
@Field (name = "name", type = FieldType.Text)
private String name;
}
It is necessary to add fields to it when writing a document to the index?将文档写入索引时是否需要向其添加字段?
count_mag1 = 5,
count_mag2 = 6,
...
count_magN = 5
That is, fields that are not described in the mapping and their numbers are dynamic.也就是说,映射中未描述的字段及其编号是动态的。
As I understand it, in this case, I only need to leave the described Entity and save entities through the Spring data repositories?据我了解,在这种情况下,我只需要离开描述的实体并通过 Spring 数据存储库保存实体? Ideally, of course, I would like to add a property to the Entity with a collection value of the form:理想情况下,当然,我想向实体添加一个属性,其集合值的形式为:
@Document (indexName = "product", createIndex = true)
public class Product {
@Id
@Field (name = "id", type = FieldType.Long)
private Long id;
@Field (name = "name", type = FieldType.Text)
private String name;
private Map <String, Long> counts;
}
and at the stage of forming a request in elastic, iterate over this map and create elements with such a key value, and ignore this itself.在弹性请求形成阶段,迭代这个 map 并创建具有这样一个键值的元素,并忽略它本身。 Maybe there is such an opportunity and I don't notice it?也许有这样的机会,我没有注意到?
Update:更新:
No, unfortunately it doesn't suit me.不,不幸的是它不适合我。 In the above question, the data is nested in the innerData field and now I am achieving the same result by specifying the following option in the Entity: private Map<String, Long> counts;
在上面的问题中,数据嵌套在 innerData 字段中,现在我通过在实体中指定以下选项来实现相同的结果: private Map<String, Long> counts;
My goal is to end up with something like this:我的目标是最终得到这样的结果:
"_index": "infos",
"_type": "_doc",
"_id": "123456",
"_version": 1,
"_seq_no": 6919,
"_primary_term": 1,
"_routing": "4",
"found": true,
"_source": {
{
"id": 123,
"name": "test",
"count_wh_1": 123,
"count_wh_2": 1234,
"count_wh_3": 1235,
.....
"count_wh_N": 123,
}
}
may be you can try Custom Conversions也许您可以尝试自定义转换
@Bean
@Override
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new ElasticsearchCustomConversions(
Arrays.asList(new AddressToMap(), new MapToAddress()));
}
@WritingConverter
static class AddressToMap implements Converter<Address, Map<String, Object>> {
@Override
public Map<String, Object> convert(Address source) {
LinkedHashMap<String, Object> target = new LinkedHashMap<>();
target.put("ciudad", source.getCity());
// ...
return target;
}
}
@ReadingConverter
static class MapToAddress implements Converter<Map<String, Object>, Address> {
@Override
public Address convert(Map<String, Object> source) {
// ...
return address;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.