前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >ES中数据类型转换处理

ES中数据类型转换处理

原创
作者头像
周银辉
发布2025-02-14 00:10:39
发布2025-02-14 00:10:39
640
举报

测试数据类型存储

代码语言:txt
复制
PUT my_type_index
{
  "mappings": {
    "properties": {
      "price":{
        "type":"float"
      }
    }
  }
}

正常存储数字

代码语言:txt
复制
PUT my_type_index/_doc/1
{
  "price":7.8
}

这里能把字符串数据存进去

代码语言:txt
复制
PUT my_type_index/_doc/2
{
  "price":"9.4"
}

字符串无法存储

代码语言:txt
复制
PUT my_type_index/_doc/3
{
  "price":"change the data type"
}

查询到数据即有数字,又有字符串

代码语言:txt
复制
GET my_type_index/_search
{
  "query": {
    "match_all": {}
  }
}

如何才能只存储数字,字段中使用coerce

代码语言:txt
复制
DELETE my_type_index
PUT my_type_index
{
  "mappings": {
    "properties": {
      "price": {
        "type": "float",
        "coerce": false
      }
    }
  }
}

这个时候就无法写入了

代码语言:txt
复制
PUT my_type_index/_doc/1
{
  "price":"9.4"
}

针对已有的数据如何处理呢?可以通过reindex + pipeline

代码语言:txt
复制
DELETE my_type_index2
PUT my_type_index2
{
  "mappings": {
    "properties": {
      "price":{
        "type":"float",
        "coerce": false
      }
    }
  }
}

这是是不会直接成功的,必须先转换

代码语言:txt
复制
POST _reindex
{
  "source": {
    "index": "my_type_index"
  },
  "dest": {
    "index": "my_type_index2"
  }
}

创建pipeline

代码语言:txt
复制
PUT _ingest/pipeline/my_convert_test_pipe
{
  "description": "convert string to float", 
  "processors": [
    {
      "convert": {
        "field": "price",
        "type": "float"
      }
    }
  ]
}

再次重建

代码语言:txt
复制
POST _reindex
{
  "source": {
    "index": "my_type_index"
  },
  "dest": {
    "index": "my_type_index2",
    "pipeline": "my_convert_test_pipe"
  }
}

经过查询,数据全部完成转换

代码语言:txt
复制
GET my_type_index2/_search

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试数据类型存储
  • 正常存储数字
  • 这里能把字符串数据存进去
  • 字符串无法存储
  • 查询到数据即有数字,又有字符串
  • 如何才能只存储数字,字段中使用coerce
  • 这个时候就无法写入了
  • 针对已有的数据如何处理呢?可以通过reindex + pipeline
  • 这是是不会直接成功的,必须先转换
  • 创建pipeline
  • 再次重建
  • 经过查询,数据全部完成转换
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档