【问题标题】:IndexQueryBuilder method inside entity class results in "failed to index the document [id: 1]"实体类中的 IndexQueryBuilder 方法导致“无法索引文档 [id: 1]”
【发布时间】:2016-08-06 12:37:01
【问题描述】:

我收到了这个错误:

org.springframework.data.elasticsearch.ElasticsearchException:失败 索引文档 [id: 1]

在 org.springframework.data.elasticsearch.core.ElasticsearchTemplate.prepareIndex(ElasticsearchTemplate.java:1028) 在 org.springframework.data.elasticsearch.core.ElasticsearchTemplate.index(ElasticsearchTemplate.java:525) ...

每次我在我的实体Book 类中放入一个getIndexQuery 方法。所以它看起来像这样:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "bookshop", type = "book", shards = 1, replicas = 0, refreshInterval = "-1")
public class Book  {

    @Id
    private String bookId;

    @Field(type = FieldType.String, store = true)
    private String title;

    public IndexQuery getIndexQuery(){
        return new IndexQueryBuilder().withId(this.getBookId()).withObject(this).build();
    }

}

即使我在代码中没有使用此方法,也会发生错误。 我怎样才能把这个方法放在我的实体类中而不弄乱实体模式(因为这是我假设的问题)?

【问题讨论】:

    标签: java spring spring-data


    【解决方案1】:

    使用@JsonIgnore.

    @JsonIgnore
    public IndexQuery getIndexQuery(){
        return new IndexQueryBuilder().withId(this.getBookId()).withObject(this).build();
    }
    

    com.fasterxml.jackson.annotation.JsonIgnore 来自 jackson-core-2.8.1.jar 或添加此依赖项。

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.1</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2019-03-01
      相关资源
      最近更新 更多