'Elasticsearch java client stopped supporting custom query type values, such as enum

Looks like there is a regression\change in Elasticsearch java client, it stopped supporting custom query type values, such as enum:

String query = QueryBuilders.boolQuery().
                must(QueryBuilders.termsQuery("status", 
                List.of(MyEnumStatus.ACTIVE, MyEnumStatus.UNDER_TEST))).toString();

The above example produced a json string query with java client 6.8.14 but gives below exception with 7.15.2, any workaround?

java.lang.IllegalArgumentException: can not write type [class ...$MyEnumStatus]
    at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:845)
    at org.elasticsearch.common.io.stream.StreamOutput.lambda$static$12(StreamOutput.java:699)
    at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:843)
    at org.elasticsearch.index.query.TermsQueryBuilder$Values.serialize(TermsQueryBuilder.java:435)
    at org.elasticsearch.index.query.TermsQueryBuilder$BinaryValues.<init>(TermsQueryBuilder.java:495)
    at org.elasticsearch.index.query.TermsQueryBuilder$BinaryValues.<init>(TermsQueryBuilder.java:485)
    at org.elasticsearch.index.query.TermsQueryBuilder.<init>(TermsQueryBuilder.java:161)
    at org.elasticsearch.index.query.QueryBuilders.termsQuery(QueryBuilders.java:590)


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.

String query = QueryBuilders.boolQuery().
                must(QueryBuilders.termsQuery("status", 
                List.of(MyEnumStatus.ACTIVE.toString(), MyEnumStatus.UNDER_TEST.toString()))).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