Elasticsearch相同index不同type下的相同字段怎么设置不同的类型?

新手上路,请多包涵

Elasticsearch创建mapping的时候发现不同type的相同字段名称设置不同的type的是会报错,所以请问一下这种情况应该怎么解决???

请求连接

 PUT  http://localhost:9200/new

参数如下:

{
    "max_result_window": "1000000",
    "_source" : {
        "enabled": true
      },
      "_all" : {
        "enabled": false
      },
    "mappings": {
         "type1": {
          "properties": {
            "@timestamp" : {
              "type" : "date"
            },
            "@version" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "create_time" : {
              "index": "not_analyzed",
              "type" : "long"
            }
          }
        },
        "type2": {
          "properties": {
            "@timestamp" : {
              "type" : "date"
            },
            "@version" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "create_time" : {
              "index": "not_analyzed",
              "type" : "date"
            }
          }
        }
    }
}
 

请求结果:

 {
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "mapper [create_time] cannot be changed from type [date] to [long]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "mapper [create_time] cannot be changed from type [date] to [long]"
    },
    "status": 400
}
阅读 7.1k
2 个回答

这个是Elasticsearch规定死的,同一个索引内的相同字段必须是相同类型.

但是你可以曲线救国,利用copy_to,复制到另一个字段里,使用不同的类型

可以使用嵌套对象,或者在子属性设置成不同的那个类型,但是一旦设定了,也是不能变的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进