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

015.Elasticsearch Mapping介绍

作者头像
CoderJed
发布2020-07-02 10:35:23
9950
发布2020-07-02 10:35:23
举报
文章被收录于专栏:Jed的技术阶梯Jed的技术阶梯

1. mapping解析

1.1 mapping是什么

  • mapping,就是index的type的元数据,每个type都有一个自己的mapping,决定了这个type的数据类型,建立倒排索引的行为,还有进行搜索的行为,可以类比关系型数据库,ES给某个index的type设置mapping,就相当于给一张表定义各个字段的名称和数据类型
  • 往一个不存在的index里面插入数据,es会自动建立该index,同时建立type以及对应的mapping
  • mapping中就自动定义了每个field的数据类型
  • es可以进行dynamic mapping,自动建立mapping,包括自动设置数据类型;也可以提前手动创建index和type的mapping,对各个field进行设置,包括数据类型,包括索引行为,包括分词器,等等

1.2 创建mapping

为空index设置mapping,index需要提前创建好

代码语言:javascript
复制
# ES6.x需要在_mapping后指定type,type可以自动创建
curl -X PUT "node01:9200/nba/_mapping/_doc" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "name": {
            "type": "text"
        },
        "team_name": {
            "type": "text"
        },
        "position": {
            "type": "keyword"
        },
        "play_year": {
            "type": "keyword"
        },
        "jerse_no": {
            "type": "keyword"
        }
    }
}
'

# ES7.x
curl -X PUT "node01:9200/nba/_mapping" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "name": {
            "type": "text"
        },
        "team_name": {
            "type": "text"
        },
        "position": {
            "type": "keyword"
        },
        "play_year": {
            "type": "keyword"
        },
        "jerse_no": {
            "type": "keyword"
        }
    }
}
'

创建index的时候设置mapping

代码语言:javascript
复制
# ES6.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "test_type": {
      "properties": {
        "id": {
          "type": "text"
        }
      }
    }
  }
}
'
# ES7.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "properties": {
      "id": {
      "type": "text"
      }
    }
  }
}
'

1.3 查看mapping

查看某个索引的mapping

代码语言:javascript
复制
# ES6.0需要指定type
curl -X GET "node01:9200/nba/_mapping/_doc"
# ES7.0不需要指定type
curl -X GET "node01:9200/nba/_mapping"

{
    "nba": {
        "mappings": {
            "_doc": {
                "properties": {
                    "jerse_no": {
                        "type": "keyword"
                    },
                    "name": {
                        "type": "text"
                    },
                    "play_year": {
                        "type": "keyword"
                    },
                    "position": {
                        "type": "keyword"
                    },
                    "team_name": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

查看多个索引或者type的mapping

  • ES5.x:一个index可以有多个type
    • 查看一个index的一个type的mapping:curl -X GET "ip:9200/index/_mapping/type"
    • 查看一个index的多个type的mapping:curl -X GET "ip:9200/index/_mapping/type1,type2"
    • 查看一个index的所有type的mapping:curl -X GET "ip:9200/index/_mapping"
    • 查看多个index的多个type的mapping:curl -X GET "ip:9200/index1,index2/_mapping/type1,type2" 这时会做笛卡尔积,把所有可以查到的index的type的mapping返回
    • 可以使用通配符: curl -X GET "ip:9200/index*,test*/_mapping" curl -X GET "ip:9200/*1,*2/_mapping/type*" 使用通配符也会做笛卡尔积,所有满足通配符的index和type,只要可以这个index有这个type就会查出mapping并返回
  • ES6.x:一个index只能有一个type
    • 查看一个index的mapping curl -X GET "ip:9200/index/_mapping/type" curl -X GET "ip:9200/index/_mapping" 由于只有一个type,所以这两条查询本质上是一样的
    • 查看多个index的mapping curl -X GET "ip:9200/index1,index2/_mapping/type1,type2" curl -X GET "ip:9200/test*/_mapping" 同样,也是做笛卡尔积,只要可以查出来就返回
  • ES7.x:没有type的概念
    • 查看多个index的mapping: curl -X GET "ip:9200/index1,index2/_mapping" curl -X GET "ip:9200/test*/_mapping"
  • ES6.x测试 index和type对应关系: test_index1/test_type1 test_index2/test_type2
代码语言:javascript
复制

 curl -X GET "node01:9200/test_index1/_mapping"
{
  "test_index1":{
      "mappings":{
          "test_type1":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  }
}

curl -X GET "node01:9200/test_index1/_mapping/test_type1"
{
  "test_index1":{
      "mappings":{
          "test_type1":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  }
}

curl -X GET "node01:9200/test_index1,test_index2/_mapping"
{ "test_index2":{
      "mappings":{
          "test_type2":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  },
  "test_index1":{
      "mappings":{
          "test_type1":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  }
}

curl -X GET "node01:9200/test_index1,test_index2/_mapping/test_type1"
{
  "test_index1":{
      "mappings":{
          "test_type1":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  }
}

curl -X GET "node01:9200/test_index1,test_index2/_mapping/test_type2"
{
  "test_index2":{
      "mappings":{
          "test_type2":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  }
}

curl -X GET "node01:9200/test_index1,test_index2/_mapping/test_type1,test_type2"
{ "test_index2":{
      "mappings":{
          "test_type2":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  },
  "test_index1":{
      "mappings":{
          "test_type1":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  }
}

curl -X GET "node01:9200/test_index*,index*/_mapping"
{
  "index3":{
      "mappings":{}
  },
  "test_index":{
      "mappings":{
          "test_type":{
              "properties":{
                  "message":{
                      "type":"text",
                      "fields":{
                          "keyword":{
                              "type":"keyword",
                              "ignore_above":256
                          }
                      }
                  },
                  "user":{
                      "type":"text",
                      "fields":{
                          "keyword":{
                              "type":"keyword",
                              "ignore_above":256
                          }
                      }
                  }
              }
          }
      }
  },
  "test_index2":{
      "mappings":{
          "test_type2":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  },
  "index2":{
      "mappings":{}
  },
  "index1":{
      "mappings":{
          "_doc":{
              "properties":{
                  "jerse_no":{
                      "type":"keyword"
                  },
                  "name":{
                      "type":"text"
                  },
                  "play_year":{
                      "type":"keyword"
                  },
                  "position":{
                      "type":"keyword"
                  },
                  "team_name":{
                      "type":"text"
                  }
              }
          }
      }
  },
  "test_index1":{
      "mappings":{
          "test_type1":{
              "properties":{
                  "name":{
                      "type":"text"
                  }
              }
          }
      }
  }
}

查看全部索引的mapping

  • ES5.x/ES6.x
    • 查看所有的index的所有的type的mapping curl -X GET "ip:9200/_mapping
    • 查看所有的index的某些type的mapping,没有这个type的index,就不返回它的mapping curl -X GET "ip:9200/_all/_mapping/type1,type2
  • ES7.x curl -X GET "ip:9200/_mapping" curl -X GET "ip:9200/_all/_mapping"
  • ES6.x测试
代码语言:javascript
复制
  curl -X GET node01:9200/_all/_mapping/test_type1,test_type2
{
      "test_index2": {
          "mappings": {
              "test_type2": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index1": {
          "mappings": {
              "test_type1": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      }
  }
  
  curl -X GET node01:9200/_mapping
  {
      "index1": {
          "mappings": {
              "_doc": {
                  "properties": {
                      "jerse_no": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "play_year": {
                          "type": "keyword"
                      },
                      "position": {
                          "type": "keyword"
                      },
                      "team_name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "index2": {
          "mappings": {}
      },
      "test_index2": {
          "mappings": {
              "test_type2": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index": {
          "mappings": {
              "test_type": {
                  "properties": {
                      "message": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "user": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      }
                  }
              }
          }
      },
      "test2": {
          "mappings": {}
      },
      "nba": {
          "mappings": {
              "_doc": {
                  "properties": {
                      "jerse_no": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "play_year": {
                          "type": "keyword"
                      },
                      "position": {
                          "type": "keyword"
                      },
                      "team_name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index1": {
          "mappings": {
              "test_type1": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test1": {
          "mappings": {}
      },
      "index3": {
          "mappings": {}
      }
  }

1.4 修改mapping

代码语言:javascript
复制
# 只能新增field,不能修改已有field的数据类型
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        }
    }
}
'

# 如果要写上原有的字段,一定要和以前的数据类型一样
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        },
        "name": {
            "type": "text"
        }
    }
}
'

# EX7.x不需要指定type
curl -X PUT "node01:9200/test_index1/_mapping" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        }
    }
}
'

2.ES常用的数据类型

2.1 核心数据类型

  • 字符串
    • text:用于全文检索,该类型的字段将通过分词器进行分词
    • keyword:不分词,只能搜索该字段的完整的值
  • 数值型
    • byte
    • short
    • integer
    • long
    • float
    • half_float
    • scaled_float
    • double
  • 布尔
    • boolean
  • 二进制
    • binary:该类型的字段把值当作经过BASE64编码的字符串,默认不存储且不可搜索
  • 日期
    • date,定义的类型是date,实际需要传入一个字符串或者long值,只要这个字符串满足日期格式,例如"yyyy-MM-dd"或者"yyyy/MM/dd HH:mm:ss",或者这个long值是一个时间戳,就认为是date类型
  • 范围类型
    • integer_range、double_range等数值范围
    • date_range
    • 例如定义age是一个integer_range { "gte": 20, "lte": 40 }

2.2 复杂数据类型

  • Object:对象类型,可以嵌套
  • Array:ES中没有专有的数组类型,使用[]直接定义即可,数组中的元素必须都是同一类型
    • 字符串数组:["hello", "world"]
    • 整数数组:[0, 1]
    • 对象数组:[{"name": "Tom", "age": 12}, {"name": "Jerry", "age": 20}]
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. mapping解析
    • 1.1 mapping是什么
      • 1.2 创建mapping
        • 为空index设置mapping,index需要提前创建好
        • 创建index的时候设置mapping
      • 1.3 查看mapping
        • 查看某个索引的mapping
        • 查看多个索引或者type的mapping
        • 查看全部索引的mapping
      • 1.4 修改mapping
      • 2.ES常用的数据类型
        • 2.1 核心数据类型
          • 2.2 复杂数据类型
          相关产品与服务
          命令行工具
          腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档