当前位置:   article > 正文

elasticsearch时间自动映射问题_关于es 时间类型映射

关于es 时间类型映射

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

PUT /test
  • 1

往test索引中插入一个数据

POST /test/_doc
{
  "name":"zhangsan",
  "birthday":"2021-01-01"
}
  • 1
  • 2
  • 3
  • 4
  • 5

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

GET /test/_mapping
  • 1

映射关系如下

{
  "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
            }
          }
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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

PUT /test
  • 1

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

POST /test/_doc
{
  "name":"zhangsan",
  "birthday":"2021-01-01 00:00:00"
}
  • 1
  • 2
  • 3
  • 4
  • 5

再看看映射关系

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

可以看到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"]
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/837329
推荐阅读
相关标签
  

闽ICP备14008679号