我的 Spring 应用程序使用 Spring-data-elasticsearch ( https://github.com/spring-projects/spring-data-elasticsearch )。
我想将以下文档保存到 elasticsearch 数据库:
@Document(indexName = "documents", type = "customEntity", replicas = 0, shards = 5)
public class CustomEntity implements Serializable{
@Id
private String Id;
@Field(type = FieldType.String)
private String Field1;
@Field(type = FieldType.Integer)
private int Field2;
@Field(type = FieldType.Object) //not sure which annotation I should use
private JsonObject exportJSON; //gson object
...getters and setters...
}
使用这种方式:
public class CustomEntityDao {
@Resource
ElasticsearchTemplate elasticsearchTemplate;
public void insertCustomEntity(CustomEntity entity){
IndexQuery indexQuery = new IndexQuery();
indexQuery.setId(entity.getCustomEntityId());
indexQuery.setObject(entity);
elasticsearchTemplate.index(indexQuery); //exception thrown
}
}
但我收到此错误:
com.fasterxml.jackson.databind.JsonMappingException: JsonObject (通过引用链: data.nosql.entities.CustomEntity ["exportJSON"]->com.google.gson.JsonObject["asString"])
我理解这个问题,但我不知道如何解决它。请问有什么想法吗?