首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ElasticSearch关键字映射导致illegal_state_exception

ElasticSearch关键字映射导致illegal_state_exception
EN

Stack Overflow用户
提问于 2022-02-02 15:16:12
回答 1查看 466关注 0票数 0

在spring引导应用程序中,当将对象索引到es中时,我会得到以下异常。

代码语言:javascript
运行
复制
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

该类有两个静态字段和两个LocalDate类型的实例变量以及一个视图方法,其中一个是重写的toString。

映射如下。没有映射,索引工作得很好。但是,需要配置null_value,因为对象可以为null。

代码语言:javascript
运行
复制
    {
      "dynamic_templates": [
        {
          "mapStringToKeywordByDefault": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ],
      "properties": {
        "billingperiod": {
          "type": "keyword",
          "null_value": "anemptystring"
        }
      }
    }

然而,我试图用keyword替换flattened状态:“一个字符串值,它替代了扁平对象字段中的任何显式空值。默认为null,这意味着空值被视为丢失。”因此,如果对象本身是空的,那么它就不能工作。

版本ElasticSearch 7.10 (不能更改)

我犯了什么错?我能做些什么才能让它发挥作用?

对ilvar:的答复进行更新

使用示例的配置,可以创建索引,但找不到空值。它需要null_value的映射。但是,如果不指定记帐期的类型,则会失败:

代码语言:javascript
运行
复制
"billingperiod": {
  "properties": {
    "startdate": {"type": "date"},
    "enddate": {"type": "date"}
  },
  "null_value": "anothertry"
}

原因: org.elasticsearch.ElasticsearchException: Elasticsearch异常[type=mapper_parsing_exception,reason=Mapping对票据周期的定义不支持参数: null_value : anothertry]

对于flattened类型,它也会失败,这在我看来并不清楚,因为没有typenull_value,它就能工作。

代码语言:javascript
运行
复制
"billingperiod": {
  "type": "flattened",
  "properties": {
    "startdate": {"type": "date"},
    "enddate": {"type": "date"}
  },
  "null_value": "anothertry"
}

由: org.elasticsearch.ElasticsearchException: Elasticsearch异常引起的[type=mapper_parsing_exception,reason=Mapping对票据周期的定义不支持参数:属性:{enddate={type=date},startdate={type=date}]]

下面的映射可以工作,但不允许票据周期本身的空项,而只允许在其中的字段(例如,{startdate=2000-01-01,enddate=null}被找到,但null本身不允许)。

代码语言:javascript
运行
复制
"billingperiod": {
  "type": "flattened",
  "null_value": "anothertry"
}
EN

回答 1

Stack Overflow用户

发布于 2022-02-02 21:39:14

您的映射应该正确地反映您的数据模式,因此您应该有如下内容:

代码语言:javascript
运行
复制
"billingperiod": {
  "properties": {
    "startdate": {"type": "date"},
    "enddate": {"type": "date"}
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70958144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档