IndexQueryBuilder method inside entity class results in “failed to index the document [id: 1]”

孤街浪徒 提交于 2020-01-06 11:09:11

问题


I got this error:

org.springframework.data.elasticsearch.ElasticsearchException: failed to index the document [id: 1]

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

Every time that I put inside my entity Book class a getIndexQuery method. So it look like this:

@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();
    }

}

The error occurs even when I do not use this method anyware in my code. How can I put this method inside my entity class without it messing with the enitties schema (becouse that is what I assume what is wrong)?


回答1:


Use @JsonIgnore.

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

com.fasterxml.jackson.annotation.JsonIgnore is from jackson-core-2.8.1.jar or add this dependency.

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


来源:https://stackoverflow.com/questions/38804379/indexquerybuilder-method-inside-entity-class-results-in-failed-to-index-the-doc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!