# TimeValue error on XContentBuilder

**URL:** <https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012>\
**Category:** Elasticsearch\
**Tags:** point-in-time\
**Created:** [March 7, 2022, 8:36pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012 "2022-03-07T20:36:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dancerjohn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dancerjohn/32/57013_2.png) [@dancerjohn](https://discuss.elastic.co/u/dancerjohn)\
**Post date:** [March 7, 2022, 8:36pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/1 "2022-03-07T20:36:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [March 7, 2022, 9:34pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/2 "2022-03-07T21:34:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dancerjohn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dancerjohn/32/57013_2.png) [@dancerjohn](https://discuss.elastic.co/u/dancerjohn)\
**Post date:** [March 8, 2022, 11:42am UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/3 "2022-03-08T11:42:36Z")

</div>

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)](https://javadoc.io/static/org.elasticsearch/elasticsearch/7.10.1/org/elasticsearch/search/builder/PointInTimeBuilder.html)  
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:

```auto
    @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:

```auto
    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.

---

<div class="post-metadata">

**Author:** ![dancerjohn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dancerjohn/32/57013_2.png) [@dancerjohn](https://discuss.elastic.co/u/dancerjohn)\
**Post date:** [March 8, 2022, 11:57am UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/4 "2022-03-08T11:57:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2022, 12:10pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/5 "2022-03-08T12:10:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dancerjohn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dancerjohn/32/57013_2.png) [@dancerjohn](https://discuss.elastic.co/u/dancerjohn)\
**Post date:** [March 8, 2022, 5:08pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/6 "2022-03-08T17:08:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2022, 5:22pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/7 "2022-03-08T17:22:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dancerjohn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dancerjohn/32/57013_2.png) [@dancerjohn](https://discuss.elastic.co/u/dancerjohn)\
**Post date:** [March 11, 2022, 2:14pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/8 "2022-03-11T14:14:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 8, 2022, 2:15pm UTC](https://discuss.elastic.co/t/timevalue-error-on-xcontentbuilder/299012/9 "2022-04-08T14:15:19Z")

</div>

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