栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch时间自动映射问题

elasticsearch时间自动映射问题

elasticsearch在没有手动指定映射时会自动根据值映射类型,在时间的映射有需要注意。
首先创建一个索引(test),这里不指定映射类型

PUT /test

往test索引中插入一个数据

POST /test/_doc
{
  "name":"zhangsan",
  "birthday":"2021-01-01"
}

然后查询索引中数据映射关系

GET /test/_mapping

映射关系如下

{
  "test" : {
    "mappings" : {
      "dynamic_date_formats" : [
        "yyyy-MM-dd",
        "yyyy-MM-dd HH:mm:ss"
      ],
      "date_detection" : true,
      "properties" : {
        "birthday" : {
          "type" : "date",
          "format" : "yyyy-MM-dd"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

从映射关系可以看到birthday自动映射成了日期类型。将test索引删除重新创建

PUT /test

仍然插入刚才的数据,现在将birthday的时分秒加上

POST /test/_doc
{
  "name":"zhangsan",
  "birthday":"2021-01-01 00:00:00"
}

再看看映射关系

GET /oa_data_01/_mapping
{
  "test" : {
    "mappings" : {
      "properties" : {
        "birthday" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

可以看到birthday加了时分秒后es没有将其映射为日期类型,而是字符串类型,这里需要在创建索引时做如下设置才能让es识别时分秒的日期。date_detection设置为ture是让es自动检测日期类型,dynamic_date_formats是需要匹配的日期格式,这里设置了两种格式都要让es自动映射为日期类型。

PUT /test
{
  "mappings": {
    "date_detection": true,
    "dynamic_date_formats": ["yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"]
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号