 ES - 索引管理
ES - 索引管理
  # Mapping
# 字段类型
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping-types.html
核心数据类型
- 字符串 - text ⽤于全⽂索引,搜索时会自动使用分词器进⾏分词再匹配 keyword 不分词,搜索时需要匹配完整的值 
- 数值型 - 整型: byte,short,integer,long 浮点型: float, half_float, scaled_float,double 
- 日期类型:date 
- 范围型 - integer_range, long_range, float_range,double_range,date_range - gt是大于,lt是小于,e是equals等于。 - age_limit的区间包含了此值的文档都算是匹配。 
- 布尔 - boolean 
- 二进制 - binary 会把值当做经过 base64 编码的字符串,默认不存储,且不可搜索 
复杂数据类型
- 对象 - object一个对象中可以嵌套对象。 
- 数组 - Array:嵌套类型 - nested 用于json对象数组 
# 映射
Mapping(映射)是用来定义一个文档(document),以及它所包含的属性(field)是如何存储和索引的。
看mapping信息:
GET bank/_mapping
# 索引管理操作
- 创建索引并指定映射
PUT /my_index
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer"
      },
      "email": {
        "type": "keyword" # 指定为keyword
      },
      "name": {
        "type": "text" # 全文检索。保存时候分词,检索时候进行分词匹配
      }
    }
  }
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 查看映射
GET /my_index
- 添加新的字段映射
PUT /my_index/_mapping
{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false # 字段不能被检索。检索
    }
  }
2
3
4
5
6
7
8
9
该index选项控制字段值是否被索引。它接受true orfalse并默认为true. 未编入索引的字段不可查询。
- 修改映射 - 不能更新映射,对于已经存在的字段映射不能更新。 - 更新必须创建新的索引,进行数据迁移。 
# 数据迁移
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.16/docs-reindex.html
先创建new_twitter的正确映射。
然后使用如下方式进行数据迁移,把twitter迁移到new_twitters
POST _reindex
{
  "source":{
      "index":"twitter"
   },
  "dest":{
      "index":"new_twitters"
   }
}
2
3
4
5
6
7
8
9
