'What causes can not write type error in a TermsQueryBuilder

What causes can not write type error in a TermsQueryBuilder

TermsQueryBuilder excludeStatusQuery = QueryBuilders.termsQuery("bar", BarStatus.READY, BarStatus.DELETED); // BarStatus is an enumeration which maps to a string

java.lang.IllegalArgumentException: can not write type [class com.model.BarStatus]
    at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:845)
    at org.elasticsearch.common.io.stream.StreamOutput.lambda$static$12(StreamOutput.java:700)
    at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:843)
    at org.elasticsearch.index.query.TermsQueryBuilder$Values.serialize(TermsQueryBuilder.java:434)
    at org.elasticsearch.index.query.TermsQueryBuilder$BinaryValues.<init>(TermsQueryBuilder.java:493)
    at org.elasticsearch.index.query.TermsQueryBuilder$BinaryValues.<init>(TermsQueryBuilder.java:483)
    at org.elasticsearch.index.query.TermsQueryBuilder.<init>(TermsQueryBuilder.java:161)
    at org.elasticsearch.index.query.TermsQueryBuilder.<init>(TermsQueryBuilder.java:141)
    at org.elasticsearch.index.query.QueryBuilders.termsQuery(QueryBuilders.java:568)


Solution 1:[1]

As per my understanding, it is JSON serialization issue. You can add toString() method to both the ENUM and it will resolve issue.

TermsQueryBuilder excludeStatusQuery = QueryBuilders.termsQuery("bar", BarStatus.READY.toString(), BarStatus.DELETED.toString());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sagar Patel