gpt4 book ai didi

elasticsearch - 如何使用自定义转换Spring-data-elasticsearch

转载 作者:行者123 更新时间:2023-12-02 23:43:46 24 4
gpt4 key购买 nike

我正在使用Elasticist 4.0.1。我有一个 Realm

@Field(type = FieldType.Nested, index = false, includeInParent = true)
private Map<String, Map<String, List<City>>> province = new HashMap<>();
根据文档,我进行了自定义转换。
public class ElasticConfig extends AbstractElasticsearchConfiguration {


@Bean
@Override
public RestHighLevelClient elasticsearchClient() {

ClientConfiguration clientConfiguration
= ClientConfiguration.builder()
.connectedTo("0.0.0.0:9200")
.build();

return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(elasticsearchClient());
}

@Bean
@Override
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new ElasticsearchCustomConversions(Arrays.asList(new ProvinceToMap(), new MapToProvince()));
}

@WritingConverter
static class ProvinceToMap implements Converter<Map<String, Map<String, ArrayList<City>>>, Map<String, Object>> {

@Override
public Map<String, Object> convert(Map<String, Map<String, ArrayList<City>>> userTaskEvents) {

HashMap<String, Object> target = new HashMap<>();

for(Map.Entry<String, Map<String, ArrayList<City>>> entry: userTaskEvents.entrySet()) {
target.put(entry.getKey(), entry.getValue());

}

return target;
}

}

@ReadingConverter
static class MapToProvince implements Converter<Map<String, Object>, Map<String, Map<String, ArrayList<City>>>> {

@Override
public Map<String, Map<String, ArrayList<City>>> convert(Map<String, Object> stringObjectMap) {
ObjectMapper oMapper = new ObjectMapper();

Map<String, Map<String, ArrayList<City>>> target = new HashMap<>();

for(Map.Entry<String, Object> entry: stringObjectMap.entrySet()) {
Map<String, ArrayList<Map<String, Object>>> event = oMapper.convertValue(entry.getValue(), Map.class);

target.put(entry.getKey(), conv);
}

return target;
}
当我执行这段代码时。
 for(Map.Entry<String, Map<String, ArrayList<City>>> entry: userTaskEvents.entrySet()) {
target.put(entry.getKey(), entry.getValue());

for (Map.Entry<String, ArrayList<City>> aaa: entry.getValue().entrySet()) {
for (TestClass tt: aaa.getValue())
System.out.println(tt.getNameCity());
}

}
我得到一个错误
java.lang.ClassCastException: java.util.HashMap cannot be cast to org.elasticsearch.example.model.City
at kz.elasticsearch.example.config.ElasticConfig$UserTaskEventToMap.convert(ElasticConfig.java:66) ~[classes/:na]
at kz.elasticsearch.example.config.ElasticConfig$UserTaskEventToMap.convert(ElasticConfig.java:54) ~[classes/:na]
...
也许我做错了。我注意到,当我从Elastic接收数据时,数据将转换为Map。
如何正确使用零件4.0.1。我以前用过 jackson 。

最佳答案

我将此 map 带到一个单独的类中。

public class SomeHardMap {

private Map<String, Map<String, ArrayList<TestClass>>> hardMap = new HashMap<>();
/// Getter and Setter
........
Elasticsearch字段类别

@Field(type = FieldType.Object)private SomeHardMap someHardMap = new SomeHardMap();


完成@ReadingConverter后,将Province类解析为Map。我正在写做其他映射。我没有做到漂亮
@WritingConverter
静态类UserTaskEventToMap实现Converter {
    @Override
public Object convert(SomeHardMap userTaskEvents) {


Object obj = userTaskEvents;
return obj;
}

}
@ReadingConverter
静态类MapToUserTaskEvent实现Converter {
    @SneakyThrows
@Override
public SomeHardMap convert(Object stringObjectMap) {

Map<String, Map<String, Map<String, ArrayList<Map<String, String>>>>> mamamia = (Map)stringObjectMap;

Map<String, Map<String, ArrayList<TestClass>>> hardMapV = new HashMap<>();

for (Map.Entry<String, Map<String, Map<String, ArrayList<Map<String, String>>>>> entryObj: mamamia.entrySet()) {

for (Map.Entry<String, Map<String, ArrayList<Map<String, String>>>> entryMapHight: entryObj.getValue().entrySet()) {

Map<String, ArrayList<TestClass>> hard1 = new HashMap<>();

for (Map.Entry<String, ArrayList<Map<String, String>>> entryNextLevel: entryMapHight.getValue().entrySet()) {

ArrayList<TestClass> list = new ArrayList<>();

entryNextLevel.getValue().forEach(f -> list.add(mapClass(f)));


hard1.put(entryNextLevel.getKey(), list);
}

hardMapV.put(entryMapHight.getKey(), hard1);
}
}

SomeHardMap hardMap = new SomeHardMap();
hardMap.setHardMap(hardMapV);
return hardMap;
}

private TestClass mapClass(Map<String, String> f) {
TestClass testClass = new TestClass();
for(Map.Entry<String, String> entry: f.entrySet()) {

if (entry.getKey().equals("workPlace"))
testClass.setWorkPlace(entry.getValue());
else if (entry.getKey().equals("city"))
testClass.setCity(entry.getValue());
else if(entry.getKey().equals("nameJob"))
testClass.setNameJob(entry.getValue());
}

return testClass;
}
}
如果有人决定,则可以共享。如有必要,我会在github上发布一个测试项目

关于elasticsearch - 如何使用自定义转换Spring-data-elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63047623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com