mCs mCs - 4 months ago 11
Java Question

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

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)?

Answer

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>