首页 > 技术文章 > eleastcsearch01-操作mapping

haima 2022-04-28 07:29 原文


# ElasticSearch version "number": "7.14.1"

# 获取健康值
GET _cat/health?v

# 获取所有的信息
GET _cat/indices?v

# 创建一条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"
}

# 判断 uesrs索引是否存在
HEAD /users 

# 获取mapping信息
GET users/_mapping?pretty

# 删除索引
DELETE users

# mappings信息=======================================
# 创建test3索引
# mapping number_of_shards分片数 
# number_of_replicas版本数

# 创建test3索引
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"
            }
        }
    }
}


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

# 新增记录
put users/_doc/2
{
  "skuNumber":"1234"
}

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

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

GET users/_search


# 新增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}"
  }
}

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


# 时间类型
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" }

# delete mapping
DELETE /test2


//查所有信息 查所有索引
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": {}
  }
}


# 复杂数据类型
# 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"
}

更新数据类型参考:

https://www.cnblogs.com/shoufeng/p/10692113.html

https://blog.csdn.net/liuxiao723846/article/details/109099508

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

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



更新数据类型参考:


https://www.cnblogs.com/shoufeng/p/10692113.html

https://blog.csdn.net/liuxiao723846/article/details/109099508

推荐阅读