# ElasticSearch keyword mapping leads to illegal\_state\_exception

**URL:** https://discuss.elastic.co/t/elasticsearch-keyword-mapping-leads-to-illegal-state-exception/296107
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [February 2, 2022, 6:24pm UTC](https://discuss.elastic.co/t/elasticsearch-keyword-mapping-leads-to-illegal-state-exception/296107 "2022-02-02T18:24:08Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Jabest](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jabest/32/101258_2.png) [@Jabest](https://discuss.elastic.co/u/Jabest)
#### Post date: [February 2, 2022, 6:24pm UTC](https://discuss.elastic.co/t/elasticsearch-keyword-mapping-leads-to-illegal-state-exception/296107/1 "2022-02-02T18:24:08Z")

</div>

In a spring boot application when indexing an object into es I get the following exception.

```auto
Suppressed: org.elasticsearch.client.ResponseException: method [PUT], host [http://localhost:9299], 
URI [/my_index_name/_doc/1000001_000000004-v2?timeout=5s], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse field [billingperiod] 
of type [keyword] in document with id '1000001_000000004-v2'. Preview of field's value: 
'{enddate=2016-12-31, startdate=2016-10-01}'"}],"type":"mapper_parsing_exception","reason":"failed to 
parse field [billingperiod] of type [keyword] in document with id '1000001_000000004-v2'. Preview of 
field's value: '{enddate=2016-12-31, startdate=2016-10-01}'","caused_by":{"type":"illegal_state_exception",
"reason":"Can't get text on a START_OBJECT at 1:399"}},"status":400}
    at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:318)
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:288)
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:262)
    at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1628)
    ... 133 common frames omitted
Caused by: org.elasticsearch.ElasticsearchException: 
Elasticsearch exception [type=illegal_state_exception, reason=Can't get text on a START_OBJECT at 1:399]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:496)
at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:407)
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:437)
at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:603)
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:179)
... 136 common frames omitted

```

The class has a couple of static fields and two instance variables of type `LocalDate` and a view methods one of which is the overridden toString.

The mapping is as follows. Without the mapping the indexing works fine. However, a null\_value needs to be configured since the object can be null.

```auto
    {
      "dynamic_templates": [
        {
          "mapStringToKeywordByDefault": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ],
      "properties": {
        "billingperiod": {
          "type": "keyword",
          "null_value": "anemptystring"
        }
      }
    }

```

I tried to replace `keyword` with `flattened` , however, the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/7.10/flattened.html) state: "A string value which is substituted for any explicit null values _within_ the flattened object field. Defaults to null, which means null sields are treated as if it were missing." So if the object itself is null it is not working.

Without any mapping there is no problem. However, shouldn't the default be keyword anyway?

Version Elasticsearch 7.10 (cannot be changed)

What is my mistake? What can I do to make that work?

---

<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: [March 2, 2022, 6:25pm UTC](https://discuss.elastic.co/t/elasticsearch-keyword-mapping-leads-to-illegal-state-exception/296107/2 "2022-03-02T18:25:00Z")

</div>

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