首页 > 解决方案 > 无法创建弹性搜索映射或使日期字段工作

问题描述

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true."
      }
  "status" : 400

我读过上面的内容是因为在 elasticsearch 7.7 中不推荐使用类型,这对数据类型有效吗?我的意思是我该怎么说我希望将数据视为日期?我当前的映射有这个元素:

"Time": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  }
},

我只是想用 type:"date" 再次创建它,但我注意到即使复制粘贴当前映射(有效)也会产生错误......我拥有的索引和映射由https://github.com自动生成/jayzeng/scrapy-elasticsearch 我的目标只是拥有一个日期字段,我的所有日​​期都在我的索引中,但是当我想在 kibana 中过滤时,我可以看到它不被视为日期字段。而且修改映射似乎不是一种选择。这里有明显的 ELK 菜鸟,请与我裸露(:

在此处输入图像描述

该错误非常具有讽刺意味,因为我粘贴了现有索引/映射中的映射... 在此处输入图像描述

标签: elasticsearchscrapykibanaelastic-stack

解决方案


您遇到这种情况是因为7.0中有关命名文档类型的重大变化。

长话短说,而不是

PUT example_index
{
  "mappings": {
    "_doc_type": {
      "properties": {
        "Time": {
          "type": "date",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

你应该做

PUT example_index
{
  "mappings": {        <-- Note no top-level doc type definition
    "properties": {
      "Time": {
        "type": "date",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

我不熟悉你的 scrapy 包,但乍一看它似乎只是原生 py ES 客户端的包装器,它应该将你的映射定义转发给 ES。


警告:如果您将字段定义为text并且打算将其更改为date(或添加fieldtype date),您将收到以下错误:

mapper [Time] of different type, current_type [text], merged_type [date]

你基本上有两个选择:

  1. 删除索引,设置新的映射并重新索引所有内容(最简单但会引入停机时间)
  2. 用一个新字段扩展映射,让我们调用它Time_as_datetime并使用 a 更新您现有的文档script(引入一个全新的字段/属性——这有点冗长)

推荐阅读