前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch 映射3

Elasticsearch 映射3

作者头像
franket
发布2021-11-26 09:48:17
4500
发布2021-11-26 09:48:17
举报
文章被收录于专栏:技术杂记

类型报错

我们尝试添加一条数据类型的记录到 name

代码语言:javascript
复制
[root@h102 ~]# curl -XPUT 'localhost:9200/abc/test/2?pretty' -d '{"name":12,"age":23}'
{
  "_index" : "abc",
  "_type" : "test",
  "_id" : "2",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}
[root@h102 ~]# curl 'localhost:9200/abc/test/2?pretty'
{
  "_index" : "abc",
  "_type" : "test",
  "_id" : "2",
  "_version" : 1,
  "found" : true,
  "_source":{"name":12,"age":23}
}
[root@h102 ~]# 
[root@h102 ~]# curl 'localhost:9200/abc/_mapping/t*/field/n*?pretty'
{
  "abc" : {
    "mappings" : {
      "test" : {
        "name" : {
          "full_name" : "name",
          "mapping" : {
            "name" : {
              "type" : "string"
            }
          }
        }
      }
    }
  }
}
[root@h102 ~]#

成功了,说明数据类型被转化为了字符串类型

我们再尝试添加一条字符串类型的数据到 age

代码语言:javascript
复制
[root@h102 ~]# curl -XPUT 'localhost:9200/abc/test/3?pretty' -d '{"name":"testtype","age":"lili"}'
{
  "error" : {
    "root_cause" : [ {
      "type" : "mapper_parsing_exception",
      "reason" : "failed to parse [age]"
    } ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse [age]",
    "caused_by" : {
      "type" : "number_format_exception",
      "reason" : "For input string: \"lili\""
    }
  },
  "status" : 400
}
[root@h102 ~]# curl 'localhost:9200/abc/test/3?pretty'
{
  "_index" : "abc",
  "_type" : "test",
  "_id" : "3",
  "found" : false
}
[root@h102 ~]#

报类型不匹配的错误

我们尝试进行修改

代码语言:javascript
复制
[root@h102 ~]# curl -XPUT 'localhost:9200/abc/_mapping/test?update_all_types&pretty' -d  '{"properties" : {"age" : {"type" : "string"}}}'
{
  "error" : {
    "root_cause" : [ {
      "type" : "merge_mapping_exception",
      "reason" : "Merge failed with failures {[mapper [age] of different type, current_type [long], merged_type [string]]}"
    } ],
    "type" : "merge_mapping_exception",
    "reason" : "Merge failed with failures {[mapper [age] of different type, current_type [long], merged_type [string]]}"
  },
  "status" : 400
}
[root@h102 ~]# 
[root@h102 ~]# curl 'localhost:9200/abc/_mapping/test/field/age?pretty'
{
  "abc" : {
    "mappings" : {
      "test" : {
        "age" : {
          "full_name" : "age",
          "mapping" : {
            "age" : {
              "type" : "long"
            }
          }
        }
      }
    }
  }
}
[root@h102 ~]# 

结论是:修改不了

Tip: 但可以使用这个方法添加额外属性(就是原来没有指定的特性),对于已经设定好的,就无能为力了,唯一的解决办法就是清空重来,如果此时数据量已经很大了,想想都疼

所以使用之初就应该进行一翻慎重考虑,必要的 scheme设计 可以有效解决这类问题


创建mapping

使用 PUT mapping API 可以在一个索引中创建符合指定mapping的类型(type,其实翻译过来反而怪怪的),或者在一个现有的类型中添加指定mapping的字段

代码语言:javascript
复制
[root@h102 ~]# curl -XPUT 'localhost:9200/def?pretty'  -d '{"mappings": {"test": {"properties": {"userid": {"type": "integer"}}}}}'
{
  "acknowledged" : true
}
[root@h102 ~]# curl 'localhost:9200/def/_mapping?pretty'
{
  "def" : {
    "mappings" : {
      "test" : {
        "properties" : {
          "userid" : {
            "type" : "integer"
          }
        }
      }
    }
  }
}
[root@h102 ~]# curl -XPUT 'localhost:9200/def/_mapping/test?pretty'  -d '{"properties": {"city": {"type": "string"}}}'
{
  "acknowledged" : true
}
[root@h102 ~]# curl 'localhost:9200/def/_mapping?pretty'
{
  "def" : {
    "mappings" : {
      "test" : {
        "properties" : {
          "city" : {
            "type" : "string"
          },
          "userid" : {
            "type" : "integer"
          }
        }
      }
    }
  }
}
[root@h102 ~]# 

本文系转载,前往查看

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

本文系转载前往查看

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 类型报错
  • 创建mapping
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档