07 mapping索引操作

版本

ElasticSearch version "number": "7.14.1"

获取健康值

GET _cat/health?v

获取所有索引的信息

GET _cat/indices?v

创建索引

创建方式

  1. 创建一个索引!

PUT /索引名/~类型名~/文档id

类型名以后到8版本就没有了

PUT /test1/type1/1
{
  "name":"haima",
  "age":2
}
  1. 创建一个索引!

PUT /索引名/~类型名~/文档id

建 索引规则 和 字段的类型,不填入数据内容
es6版本需要加type,
es7版本就不需要加type了,es7一个文档里只有一个类型(_doc)(相当于mysql里只有一张表)。

PUT /test1/type1/1
{
  "name":"haima",
  "age":2
}
  1. 查看数据

完成了自动增加了索引,数据也成功的添加了,这就是我说大家在初期可以把它当做数据库学习的原因!

  1. 那么name这个字段用不用指定类型呢。毕竟我们关系型数据库是需要指定类型的啊
  • 字符串类型
    text,keyword
  • 数值类型
    long,integer,short,byte,double,float,half_float,scaled_float
  • 日期类型
    date
  • 布尔值类型
    boolean
  • 二进制类型
    binary
  • 等等

更多详情看下面的文章

官网地址:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

es 数据类型:
https://blog.csdn.net/liuxiao723846/article/details/109099508

1. 自动推导

# 创建一条id为1记录 没有会自动创建索引,字段类型会自动推导
PUT users/_doc/1
{
  "id" : 1,
  "name" : "lisi",
  "height" : 184.5,
  "age" : 32,
  "state" : true,
  "created_at" : "2022-05-02 18:50:49",
  "updated_at" : "2022-05-02 18:50:49"
}

2. 自定义


# mappings信息=======================================
# 创建test3索引
# mapping number_of_shards分片数 
# number_of_replicas版本数
# mappings GET users/_mapping获取mappings字段内容后修改

# 创建users索引
PUT users
{
    "settings":{
        "number_of_shards":1,
        "number_of_replicas":0
    },
    "mappings":{
        "properties":{
            "age":{
                "type":"long"
            },
            "height":{
                "type":"float"
            },
            "id":{
                "type":"long"
            },
            "name":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "state":{
                "type":"boolean"
            },
            "created_at":{
                "type":"date",
                "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd ||HH:mm:ss ||epoch_millis"
            },
            "updated_at":{
                "type":"date",
                "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd ||HH:mm:ss ||epoch_millis"
            }
        }
    }
}

判断索引是否存在

uesrs索引是否存在

HEAD /users

新增字段

# 给users新增一个skuNumber字段,执行下面的命令即可修改mapping。
PUT users/_mapping
{
  "properties":{
      "skuNumber":{
          "type":"keyword"
      }
  }
}

## ES更新mapping-新增字段
## 多个索引,英文逗号隔开
## 更多操作方式参考文档
## https://blog.csdn.net/keketrtr/article/details/124751920?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-124751920-blog-110622091.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-124751920-blog-110622091.pc_relevant_multi_platform_whitelistv3&utm_relevant_index=1

curl -X PUT "127.0.0.1:9200/_test1,user/_mapping?pretty" -H 'Content-Type: application/json' -d'
{"properties":{"geoip3":{"dynamic":"true","properties":{"industry":{"norms":false,"type":"text","fields":{"raw":{"ignore_above":32765,"type":"keyword"}}}}}}}'


curl -X PUT "127.0.0.1:9200/test2/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
    "properties":{
        "industry":{
            "type": "keyword"
        }
    }
}'


新增记录

## 新增记录
PUT users/_doc/1
{
  "skuNumber":"1234"
}

# 新增pushTime字段后,历史数据是没有默认值的。
# 场景一:因为es索引结构特性,当我们对现有索引新增字段时,历史数据并不会有默认值
# 场景二:新增记录时,如果没有写入这个字段值时,也不会有默认值
# 所以有时我们需要给历史数据设置认值,
# 设置默认值时,如果历史数据的此字段已经有值,不会被修改,只会对无值的数据进行修改。
# 设置默认值后,再写入数据新数据,如果此字段没有给值,依然会是null值
# 以命令为指定type类型为_doc的记录 修改默认值为1332466578

POST users/_doc/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": "if (ctx._source.pushTime== null) {ctx._source.pushTime= 1332466579}"
  }
}

查询记录

# 获取users id=1的信息
GET users/_doc/1

# 获取users所有信息
GET users/_doc/_search

GET users/_search

# 获取users中有多少条数据
GET users/_count


//查所有信息 查所有索引
GET _search
{
  "query": {
    "match_all": {}
  }
}

# 获取test2所有信息
GET /test2/_doc/_search

# 查所有信息 查索引kibana_sample_data_flights
GET /kibana_sample_data_flights/_search
{
  "query": {
    "match_all": {}
  }
}

# 查所有信息 查多个索引,用逗号隔开
GET /users,kibana_sample_data_flights/_search
{
  "query": {
    "match_all": {}
  }
}

获取mapping信息

请求

GET users/_mapping?pretty
GET test2

返回数据

{
  "test2" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "birthday" : {
          "type" : "date"
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1609688493459",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "lm-IkU7QTsifdLdxyXgwgw",
        "version" : {
          "created" : "7030199"
        },
        "provided_name" : "test2"
      }
    }
  }
}

或者

http://192.168.0.177:9200/test3/_mapping?pretty

{
    "test3": {
        "mappings": {
            "properties": {
                "age": {
                    "type": "long"
                },
                "birth": {
                    "type": "date"
                },
                "height": {
                    "type": "long"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

返回

{
  "users" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "created_at" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "height" : {
          "type" : "float"
        },
        "id" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "state" : {
          "type" : "boolean"
        },
        "updated_at" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

删除索引

curl -XDELETE '127.0.0.1:9200/users'

添加别名

curl -XPOST '127.0.0.1:9200/_aliases' -H 'Content-Type: application/json' -d '{"actions" : [{"add" : {"index" : "student" , "alias" : "student02"}}]}'

查看别名

curl -XGET '127.0.0.1:9200/fofapro_subdomain_cert2/_alias/*'

更新多参考文档:
es之索引的别名操作:
http://t.zoukankan.com/chong-zuo3322-p-13674651.html

索引备份数据

先建好相同mapping的新索引,再用reindex命令备份数据到新索引
es 的reindex详解
从 old_index 复制数据到 new_index 索引
size //可选,每次批量提交1000个,可以提高效率,建议每次提交5-15M的数据

curl -XPOST '127.0.0.1:9200/_reindex' -H 'Content-Type: application/json' -d '
{
    "source":{
        "index":"old_index",
        "size":1000
    },
    "dest":{
        "index":"new_index"
    }
}
'

https://blog.csdn.net/goxingman/article/details/103734747

分词器


GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "我是中国人"
}

GET _analyze
{
  "analyzer": "ik_smart",
  "text": ["我是中国人"]
}

数据字段类型

目录
1 核心数据类型
1.1 字符串类型 - string(不再支持)
1.1.1 文本类型 - text
1.1.2 关键字类型 - keyword
1.2 数字类型 - 8种
1.3 日期类型 - date
1.4 布尔类型 - boolean
1.5 二进制型 - binary
1.6 范围类型 - range
2 复杂数据类型
2.1 数组类型 - array
2.2 对象类型 - object
2.3 嵌套类型 - nested
2.3.1 对象数组是如何存储的
2.3.2 用nested类型解决object类型的不足
3 地理数据类型
3.1 地理点类型 - geo point
3.2 地理形状类型 - geo_shape
4 专门数据类型
4.1 IP类型
4.2 计数数据类型 - token_count
参考资料
版权声明
更新数据类型参考:
https://www.cnblogs.com/shoufeng/p/10692113.html
https://blog.csdn.net/liuxiao723846/article/details/109099508


PUT /test2
{
  "mappings": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd ||HH:mm:ss ||epoch_millis"
        }
      }
  }
}

POST _bulk
{"index":{"_index":"test2","_type":"_doc","_id":1}}
{ "date": "2022-01-02" }
{"index":{"_index":"test2","_type":"_doc","_id":2}}
{ "date": "12:00:00" }
{"index":{"_index":"test2","_type":"_doc","_id":3}}
{ "date": "1420070400001" }
{"index":{"_index":"test2","_type":"_doc","_id":4}}
{ "date": "2018-10-01 12:00:00" }



# 复杂数据类型
# es支持复杂的数据类型,包括:object、array、nested。用的不是很多,举个实例:
PUT /test4
{
    "settings":{
        "number_of_shards":1,
        "number_of_replicas":0
    },
    "mappings":{
        "properties":{
            "manager":{
                "properties":{
                    "age":{
                        "type":"long"
                    },
                    "name":{
                        "properties":{
                            "first":{
                                "type":"text",
                                "fields":{
                                    "keyword":{
                                        "type":"keyword",
                                        "ignore_above":256
                                    }
                                }
                            },
                            "last":{
                                "type":"text",
                                "fields":{
                                    "keyword":{
                                        "type":"keyword",
                                        "ignore_above":256
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "region":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            }
        }
    }
}

PUT /test4/_doc/1
{ 
  "region": "US",
  "manager": { 
    "age":     30,
    "name": { 
      "first": "John",
      "last":  "Smith"
    }
  }
}
GET /test4/_mapping?pretty

GET /test4/_doc/1

DELETE /test4

#存储方式:
{
  "region":             "US",
  "manager.age":        30,
  "manager.name.first": "John",
  "manager.name.last":  "Smith"
}
posted @ 2022-04-28 07:29  HaimaBlog  阅读(102)  评论(0编辑  收藏  举报