java - 在Spring中将包含json属性的实体保存到ElasticSearch

标签 java spring elasticsearch gson spring-data-elasticsearch

我的 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 (through reference chain: data.nosql.entities.CustomEntity ["exportJSON"]->com.google.gson.JsonObject["asString"])

我明白这个问题,但我不知道如何解决它。请问有什么想法吗?

最佳答案

我的猜测是 Jackson 试图将你的 gson 对象转换为可由 ES 可索引的字符串,但它不知道如何做到这一点。如果您发布完整的堆栈跟踪,它会更有帮助,但如果我必须猜测,您需要在“exportJSON”对象上使用 @JsonSerializer 注释。

关于java - 在Spring中将包含json属性的实体保存到ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249025/

相关文章:

java - 在java中从维基百科转储中搜索文本的最佳方法是什么?

spring - 有没有办法使用 Spring Integration 的 jdbc :oubound-channel-adapter? 编写 BLOB

java - 如何为使用 WSSJInInterceptor 配置的安全性编写 WS-SecurityPolicy 条目?

java - 速度模板自动重新加载不起作用

javascript - 使用带有 post_filter 的 query_string 搜索

Elasticsearch "simple_query_string"与 "query_string"字段分析错误?

java - 喜欢 Elasticsearch 中的搜索

Java Swing 或 AWT 重绘频率

java - 有没有办法在 Java 中禁用短路评估?

java - 尝试对 ListView 使用惰性加载程序时抛出空指针异常