使用kibana对索引创建删除和文档的CRUD操作命令

说明:这里的kibana会默认的在请求方式后面拼接上es连接,示例:
在这里插入图片描述
put /

1.建立索引

 #建立测试嵌套索引
PUT /zhouquantest

在这里插入图片描述

2.创建映射

类比与mysql中的字段名定义和属性约束

#建立mapping属性映射,cat属于嵌套类型
PUT /zhouquantest/_mapping
{
 "properties":{
   "name":{
     "type": "text",
     "analyzer":"ik_max_word"
   },
   "age":{
     "type": "keyword"
   },
   "sex":{
     "type":"keyword"
   },
   "email":{
     "type":"keyword",
     "index":false
   },
   "cat":{
     "properties":{
        "nick":{
          "type":"text",
          "analyzer":"ik_smart"
        },
        "cat_age":{
          "type":"integer"
        },
        "cat_sex":{
          "type":"keyword"
        },
        "cat_favor":{
          "type":"text",
          "analyzer":"ik_max_word"
        }
     
     }
   }
 }
}

在这里插入图片描述

3.删除索引

以我多年坑队友的经验,没事别瞎几把乱删

#删除索引
DELETE /zhouquantest

在这里插入图片描述

4.查看索引

#查看索引,这里可根据需求在索引名称后添加参数:/aliases,/mappings,/_settings,否则默认全部查询
GET /zhouquantest

查询结果:

{
  "zhouquantest" : { #索引名称
    "aliases" : { }, #索引别名
    "mappings" : { #索引映射
      "properties" : {
        "age" : {
          "type" : "keyword"
        },
        "cat" : {
          "properties" : {
            "cat_age" : {
              "type" : "integer"
            },
            "cat_favor" : {
              "type" : "text",
              "analyzer" : "ik_max_word"
            },
            "cat_sex" : {
              "type" : "keyword"
            },
            "nick" : {
              "type" : "text",
              "analyzer" : "ik_smart"
            }
          }
        },
        "email" : {
          "type" : "keyword",
          "index" : false
        },
        "name" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        },
        "sex" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : { #索引信息
        "creation_date" : "1653142665547", #索引创建时间的时间戳
        "number_of_shards" : "1", #索引分片数量
        "number_of_replicas" : "1", #索引副本数量
        "uuid" : "36sSkC3WRgS4jDIZa8dcSA", #生成的唯一id
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "zhouquantest" #索引名
      }
    }
  }
}

5.新增文档

#指定id新增文档
PUT /zhouquantest/_doc/1001
{
  "name":"周小全",
  "age":18,
  "sex":"男",
  "email":"zhouquan@163.com",
  "cat":{
    "nick":"铁蛋",
    "cat_age":2,
    "cat_favor":"喜欢睡觉和吃猫条",
    "cat_sex":"公的"
  }
}

返回结果

{
  "_index" : "zhouquantest", #索引名称
  "_type" : "_doc", #类型,7.x之后的版本默认为doc,8.x会弃用此属性
  "_id" : "1001",
  "_version" : 1, #文档的版本,如果有修改,版本会加以
  "result" : "created", #表示文档的创建类型,如果是修改则返回updated
  "_shards" : { # 分片信息
    "total" : 2, 
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0, 
  "_primary_term" : 1
}

6.文档查询

GET请求根据id查询数据

GET /zhouquantest/_doc/1001

在这里插入图片描述

POST请求根据id查询数据

POST /zhouquantest/_search
{
  "query":{
    "match":{
       "_id":1001
    }
  }
}

POST请求全量查询数据

POST /zhouquantest/_search
{
  "query":{
    "match_all": {}
  }
}

POST请求条件查询数据

# 条件查询
POST /zhouquantest/_search
{
  "query":{
    "match": {
      "name":"周" //这里的name的分词器使用的是ik_max_word
    }
  }
}

查询结果:

{
  "took" : 532, #花费毫秒数
  "timed_out" : false, #是否超时
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : { #命中结果对象
    "total" : { #命中数信息
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0, #查询最大相关度
    "hits" : [ #命中文档记录属组
      {
        "_index" : "zhouquantest",
        "_type" : "_doc",
        "_id" : "1001",
        "_score" : 1.0,
        "_source" : {
          "name" : "周小全",
          "age" : 18,
          "sex" : "男",
          "email" : "zhouquan@163.com",
          "cat" : {
            "nick" : "铁蛋",
            "cat_age" : 2,
            "cat_favor" : "喜欢睡觉和吃猫条",
            "cat_sex" : "公的"
          }
        }
      }
    ]
  }
}

7.文档修改

全量修改

#全量修改,先删除后新增
PUT /zhouquantest/_doc/1001 #这里的id如果不存在,则表示为数据新增
{
  "name":"周小全-全量修改",
  "age":18,
  "sex":"男",
  "email":"zhouquan@163.com",
  "cat":{
    "nick":"铁蛋",
    "cat_age":2,
    "cat_favor":"喜欢睡觉和吃猫条",
    "cat_sex":"公的"
  }
}

返回结果:

{
  "_index" : "zhouquantest",
  "_type" : "_doc",
  "_id" : "1001",
  "_version" : 2, #版本号加一!
  "result" : "updated", #表示执行的是更新操作
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

部分修改(增量修改)

#部分修改(增量修改)
POST /zhouquantest/_update/1001
{
  "doc": {
    "name": "周小全-部分修改",
    "cat": {
      "nick": "铁蛋-部分修改"
    }
  }
}

返回结果:

{
  "_index" : "zhouquantest",
  "_type" : "_doc",
  "_id" : "1001",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

8.文档删除

注意:多年坑队友经验,没事别瞎几把乱删

单条删除

DELETE /zhouquantest/_doc/1001

返回结果

{
  "_index" : "zhouquantest",
  "_type" : "_doc",
  "_id" : "1001",
  "_version" : 4, #版本号几经修改变成了4
  "result" : "deleted", #操作类型为删除
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

根据查询删除

#根据查询结果删除
POST /zhouquantest/_delete_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {  
            "name":"周"
          }
        }
      ]
    }
  }
}

返回结果

{
  "took" : 6,
  "timed_out" : false,
  "total" : 1,
  "deleted" : 1,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

总结:
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐