在Elasticsearch中建立索引时映射异常

 素材火2 发布于 2022-12-28 17:28

当我尝试索引文档时,我得到以下异常.

Caused by: org.elasticsearch.index.mapper.MapperParsingException: failed to parse date field [J], tried both date format [dateOptionalTime], and timestamp number with locale []

这一切都是正确的,但有可能忽略异常并索引其余的字段吗?

1 个回答
  • 我认为这可以在不重新索引的情况下通过将"ignore_malformed"标志添加到字段的映射来完成.这是我尝试过的,它有效:

    使用"日期"字段创建新索引

    POST events 
    {
      "mappings" : {
        "dates" : {
          "properties" : {
            "lenient_date" : {
              "type" : "date"
            }
          }
        }
      }
    }
    

    尝试添加格式错误的日期值

    PUT events/dates/1
    {
      "lenient_date" : "1/32/2014"
    }
    

    结果:解析错误(如预期的那样)

    {
       "error": "MapperParsingException[failed to parse [lenient_date]]; nested: MapperParsingException[failed to parse date field [1/32/2014], tried both date format [dateOptionalTime], and timestamp number with locale []]; nested: IllegalArgumentException[Invalid format: \"1/32/2014\" is malformed at \"/32/2014\"]; ",
       "status": 400
    }
    

    获取事件/日期/ 1结果:找不到日期(按预期)

    {
       "_index": "events",
       "_type": "dates",
       "_id": "1",
       "found": false
    }
    

    更改映射以允许格式错误的值

    PUT events/dates/_mapping
    {
      "dates" : {
        "properties" : {
          "lenient_date" : {
            "type" : "date",
            "ignore_malformed" : true
          }
        }
      }
    }
    

    尝试添加相同的错误日期

    PUT events/dates/1
    {
      "lenient_date" : "1/32/2014"
    }
    GET events/dates/1
    Result: now works
    {
       "_index": "events",
       "_type": "dates",
       "_id": "1",
       "_version": 1,
       "found": true,
       "_source": {
          "lenient_date": "1/32/2014"
       }
    }
    

    注意:

    我正在运行ES版本1.3.2.我还没有检查更新的映射如何影响排序和过滤.

    2022-12-28 17:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有