TimeValue error on XContentBuilder

Getting IllegalArgumentException ("cannot write xcontent for unknown value of type TimeValue") even though org.Elasticsearch.common.unit.TimeValue is an elastic class and is used in org.Elasticsearch.search.builder.PointInTimeBuilder.keepAlive.
This is with version 7.12.1 of Elasticsearch.

It's really not clear what this is about.
When are you getting this error exactly?
What API requests are you making?
What is the full response you are getting from Elasticsearch?

Using the RestHighLevelClient.search passing in a search request that has a PointInTimeBuilder with a keepAlive of a TimeValue. Getting the IllegalArgumentException in the java code where it is converting the search to XContent.
This is using the PointInTimeBuilder: PointInTimeBuilder (server 7.10.1 API)
Passing in a keepAlive TimeValue: org.Elasticsearch.common.unit.TimeValue

The XContentBuilder is throwing an IllegalArgumentException trying to convert the TimeValue to XContent.

From PointInTimeBuilder:

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject(SearchSourceBuilder.POINT_IN_TIME.getPreferredName());
        builder.field(ID_FIELD.getPreferredName(), encodedId);
        if (keepAlive != null) {
            builder.field(KEEP_ALIVE_FIELD.getPreferredName(), keepAlive);
        }
        builder.endObject();
        return builder;
    }

From XContentBuilder:

    private void unknownValue(Object value, boolean ensureNoSelfReferences) throws IOException {
        if (value == null) {
            this.nullValue();
        } else {
            XContentBuilder.Writer writer = (XContentBuilder.Writer)WRITERS.get(value.getClass());
            if (writer != null) {
                writer.write(this, value);
            } else if (value instanceof Path) {
                this.value((Path)value);
            } else if (value instanceof Map) {
                Map<String, ?> valueMap = (Map)value;
                this.map(valueMap, ensureNoSelfReferences, true);
            } else if (value instanceof Iterable) {
                this.value((Iterable)value, ensureNoSelfReferences);
            } else if (value instanceof Object[]) {
                this.values((Object[])value, ensureNoSelfReferences);
            } else if (value instanceof ToXContent) {
                this.value((ToXContent)value);
            } else {
                if (!(value instanceof Enum)) {
                    throw new IllegalArgumentException("cannot write xcontent for unknown value of type " + value.getClass());
                }

                this.value(Objects.toString(value));
            }

        }
    }

To temp fix this, I had to extend TimeValue implemented ToXContent.

Interestingly, in a different environment an XContentBuilderExtension is found for TimeValue. Any clue why that would exist in one env and not in another even thought they are both using 7.12.1?

In case it gives a clue, I noticed that the package name changed in a recent version...
May be check your imports?

I checked my maven dependency:tree and all ES dependencies are 7.12.1.

And your Elasticsearch version is also 7.12.1?

Could you run a simple test with 7.17.1 as this might have been fixed since then.

I can't test that, it changes too much (Geo, Polygon builders, etc). That said, the version of TimeValue went away starting in 7.16. The package changes. I would hope that a fix was introduced at that time.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.