【问题标题】:ElasticSearch keyword mapping leads to illegal_state_exceptionElasticSearch 关键字映射导致非法状态异常
【发布时间】:2022-02-02 15:16:12
【问题描述】:

在 Spring Boot 应用程序中,将对象索引到 es 时出现以下异常。

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,因此需要配置null_value。

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

我试图用flattened 替换keyword,但是,docs 状态:“一个字符串值替换任何显式空值 展平的对象字段。默认为null,这意味着 null silels 被视为丢失了。”因此,如果对象本身为 null,则它不起作用。

版本 ElasticSearch 7.10(无法更改)

我的错误是什么?我该怎么做才能让它发挥作用?

ilvar 回复更新:

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

"billingperiod": {
  "properties": {
    "startdate": {"type": "date"},
    "enddate": {"type": "date"}
  },
  "null_value": "anothertry"
}

原因:org.elasticsearch.ElasticsearchException:Elasticsearch 异常 [type=mapper_parsing_exception, reason=[billingperiod] 的映射定义具有不受支持的参数:[null_value : anothertry]]

使用flattened 类型它也会失败,这对我来说不清楚,因为没有typenull_value 它可以工作。

"billingperiod": {
  "type": "flattened",
  "properties": {
    "startdate": {"type": "date"},
    "enddate": {"type": "date"}
  },
  "null_value": "anothertry"
}

原因:org.elasticsearch.ElasticsearchException:Elasticsearch 异常 [type=mapper_parsing_exception, reason=[billingperiod] 的映射定义具有不受支持的参数:[properties : {enddate={type=date}, startdate={type=date} }]]

以下映射有效,但不允许 billingperiod 本身的 null 条目,但只允许其中的字段(例如 {startdate=2000-01-01,enddate=null} 找到但 null 本身不存在)。

"billingperiod": {
  "type": "flattened",
  "null_value": "anothertry"
}

【问题讨论】:

    标签: java spring-boot elasticsearch


    【解决方案1】:

    您的映射应该正确反映您的数据架构,因此您应该有类似的内容:

    "billingperiod": {
      "properties": {
        "startdate": {"type": "date"},
        "enddate": {"type": "date"}
      }
    }
    

    【讨论】:

    • 我用我试过的一些代码示例更新了这个问题。可以实现索引本身,但我找不到可以在索引中搜索空值的组合。
    • 你试过{"bool": {"must_not": {"exists": {"field": "billingperiod"}}}}吗?
    • 是的,没用
    • 如果没有null_value,此查询返回{"billingperiod": {}}{"billingperiod": null}{},但不返回{"billingperiod": {"start_date": "2011-01-01"}}
    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2013-01-13
    相关资源
    最近更新 更多