es索引操作命令

索引操作 index

创建索引

put 方法创建索引

使用 put 创建索引时必须指明文档id,否则报错

sh 复制代码
# PUT 创建命令
# test1 索引名称
# type1 类型名称,默认为_doc,已经被废弃
# 1 文档id 
PUT /test1/type1/1
{
  "name":"zhangsan",
  "age":18,
  "birth":"2000-01-20"
}

结果:

json 复制代码
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test1",     	// 索引
  "_type" : "type1",		// 类型
  "_id" : "1",				// id
  "_version" : 1,			// 版本
  "result" : "created",		// 操作类型
  "_shards" : {				// 分片信息
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

post 方法创建索引

post 如果没有指明文档id,会随机生成一个

sh 复制代码
POST /test2/type2
{
  "name":"zhangsan",
  "age":18,
  "birth":"2000-01-20"
}

结果:

json 复制代码
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test2",
  "_type" : "type2",
  "_id" : "3D_WQY4BOf0ywiICmI8O",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

对比 mysql :

PUT test1/type1/1 : 索引test1相当于关系型数据库的库,类型type1就相当于表 ,1 代表数据中的主键 id

这里需要补充的是 ,在 es5 版本前,一个索引下可以创建多个类型,但是在之后,一个索引只能对应一个类型,默认为 _doc,而 id 相当于关系型数据库的主键id若果不指定就会默认生成一个20位的uuid,属性相当关系型数据库的column(列)。

而结果中的 result 则是操作类型,现在是 created ,表示第一次创建。如果再次点击执行该命令那么 result 则会是 updated ,我们细心则会发现 _version 开始是1,现在你每点击一次就会增加一次。表示第几次更改。

查看索引信息

sh 复制代码
# get 索引名称
GET test1
json 复制代码
{
  "test1" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "birth" : {
          "type" : "date"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1710501416895",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "hemDp4F3T5ePsAZmaO5Ijg",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "test1"
      }
    }
  }
}

可以看到 name、age、birth 字段指定了类型 text、long、date ,说明 es 会根据字段的值指定默认的类型

指定字段类型

如果想要自己指定字段的类型,使用映射

sh 复制代码
PUT /test3
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type": "long"
      },
      "birth":{
        "type": "date"
      }
    }
  }
}

结果:

json 复制代码
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test3"
}

映射数据是字段名:json 数据,上面只指定了type(数据类型),其实可以指定很多属性

  • type:数据类型,es 中支持的数据类型非常丰富,主要使用以下几个:
    • string 类型,又分两种:
      • text:可分词
      • keyword:不可分词,数据会作为完整字段进行匹配
    • Numerical:数值类型,分两类:
      • 基本数据类型:long、integer、short、byte、double、float、half_float
      • 浮点数的高精度类型:scaled_float
    • Date:日期类型
    • Array:数组类型
    • Object:对象
  • index:是否索引,默认为 true,所有字段都会被索引,被索引则可以用来搜索,没有则不可以
  • store:是否将数据进行独立存储,默认为 false,原始的文本会存储在 _source 里面,默认情况下其他提取出来的字段都不是独立存储的,是从 _source 里面提取出来的。当然你也可以独立的存储某个字段,只要设置 "store": true 即可,获取独立存储的字段要比从 _source 中解析快得多,但是也会占用更多的空间,所以要根据实际业务需求来设置。
  • analyzer:分词器,这里的 ik_max_word 即使用 ik 分词器

查看索引映射

sh 复制代码
# get /索引名称/_mapping
GET /test3/_mapping

结果:

json 复制代码
{
  "test3" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "birth" : {
          "type" : "date"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}

查看索引健康情况

sh 复制代码
GET _cat/indices?v

可以查看我们所有索引的状态健康情况,分片,数据储存大小等等。

删除索引

sh 复制代码
# delete 索引名称
DELETE test3

结果:

json 复制代码
{
  "acknowledged" : true
}
相关推荐
bubble小拾6 小时前
ElasticSearch高级功能详解与读写性能调优
大数据·elasticsearch·搜索引擎
不能放弃治疗6 小时前
重生之我们在ES顶端相遇第 18 章 - Script 使用(进阶)
elasticsearch
hengzhepa7 小时前
ElasticSearch备考 -- Search across cluster
学习·elasticsearch·搜索引擎·全文检索·es
Elastic 中国社区官方博客9 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
慕雪华年10 小时前
【WSL】wsl中ubuntu无法通过useradd添加用户
linux·ubuntu·elasticsearch
Elastic 中国社区官方博客12 小时前
使用 Vertex AI Gemini 模型和 Elasticsearch Playground 快速创建 RAG 应用程序
大数据·人工智能·elasticsearch·搜索引擎·全文检索
alfiy13 小时前
Elasticsearch学习笔记(四) Elasticsearch集群安全配置一
笔记·学习·elasticsearch
alfiy13 小时前
Elasticsearch学习笔记(五)Elastic stack安全配置二
笔记·学习·elasticsearch
丶21361 天前
【大数据】Elasticsearch 实战应用总结
大数据·elasticsearch·搜索引擎
闲人编程1 天前
elasticsearch实战应用
大数据·python·elasticsearch·实战应用