DBA Elasticsearch 增删更新

操作语法

​ 由于Elasticsearch中弱化了表的概念,更多的是index与document,故不再研究type。

​ 以下是操作语法:

Restful风格 语法 描述
PUT Elasticsearch-host:port/indexName/typeName/documentID 创建or修改(如果创建文档则需要指定文档ID,如果修改则需要带上所有field)
POST Elasticsearch-host:port/indexName/typeName/documentID 创建用法(不可创建索引,同时创建文档不需要指定ID)
POST Elasticsearch-host:port/indexName/typeName/documentID/_update 修改用法(针对文档的PUT做出优化,不需要再带上所有field)
DELETE Elasticsearch-host:port/indexName/typeName/documentID/ 删除用法(删除索引或者文档)
GET Elasticsearch-host:port/indexName/typeName/documentID/ 查询用法(查询索引或者文档)
POST Elasticsearch-host:port/indexName/typeName/documentID/_serach 查询用法(查询全部文档)

​ 值得一提的是,在Elasticsearch7中如果不指定type,则默认为_doc1

索引操作

创建索引

​ 创建一个索引,相当于创建一个库:

PUT /test_index?pretty=true
{
  "settings": {
    "number_of_shards" : 5,
    "number_of_replicas" : 1
  }
}

# pretty为JSON格式返回,在curl命令中常用,如果使用kibana则无所谓
# settings可以省略,但是多集群模式下推荐加上

image-20210405010150988

​ 由于type(表)概念弱化,现在结构更紧密的是索引(库)和文档(记录),可以在创建索引时指定文档field类型。

​ 但filed类型的指定不是必须的,索引会自动甄别文档的field,并对其智能的分配类型。

​ 以下是常用的类型:

名称 类型 描述
text 字符串 可被拆分检索的字符串类型
keyword 字符串 不可被拆分检索的字符串类型(特定关键字)
long 数值
integer 数值
short 数值
byte 数值
float 数值
doule 数值
half float 数值
scaled float 数值
date 日期
boolean 布尔
binary 二进制

​ 示例演示:

PUT /test_index2?pretty
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type": "integer"
      },
      "create_time":{
        "type": "date"
      }
    }
  }
}

image-20210405113329831

查看索引

​ 查看索引使用GET即可:

GET /test_index?pretty=true

image-20210405112437949

删除索引

​ 使用DELETE对索引进行删除:

DELETE /test_index2?pretty

image-20210405113532644

文档操作

创建文档

​ 与MongoDB类似,Elasticsearch中可以在创建索引时一并创建文档。

​ 如下所示,以PUT方式创建文档,需要手动插入文档_id字段:

PUT /test_index/userinfo/1?pretty
{
  "name" : "Jack",
  "age"  : 18,
  "gender" : 1,
  "hobby" : ["篮球", "足球", "音乐"],
  "class" : {
    "name" : "三年级一班",
    "principal" : "王老师",
    "number_of_people" : 30
  },
  "grades":{
    "MySQL" : 89,
    "MongoDB" : 62,
    "Redis" : 38,
    "Elasticsearch": 92
  }
}

image-20210405114507522

使用POST方式进行创建,它会自动生成一个随机的_id,而不是手动指定:

POST /test_index/userinfo/?pretty
{
  "name" : "Tom",
  "age"  : 22,
  "gender" : 1,
  "hobby" : ["排球", "游戏", "唱歌"],
  "class" : {
    "name" : "三年级一班",
    "principal" : "王老师",
    "number_of_people" : 30
  },
  "grades":{
    "MySQL" : 72,
    "MongoDB" : 34,
    "Redis" : 28,
    "Elasticsearch": 73
  }
}

image-20210405114640701

修改文档

​ 修改文档时如果使用PUT方式进行修改,则必须保证源文档所有字段都被传入。

​ 否则会字段缺失:

PUT /test_index/userinfo/1?pretty
{
  "name" : "Jack",
  "age"  : 18,
  "gender" : 1,
  "hobby" : ["篮球", "足球", "音乐"],
  "class" : {
    "name" : "三年级一班",
    "principal" : "王老师",
    "number_of_people" : 30
  },
  "grades":{
    "MySQL" : 100,
    "Elasticsearch": 92
  }
}

image-20210405114906125

image-20210405114958008

​ 因此,更加推荐使用POST方式加_update进行修改,即使只传入单个field,其他的field依旧不会发生任何变更:

POST /test_index/userinfo/1/_update
{
  "doc":{
    "age" : 19
  }
}

# 必须使用doc进行包裹

image-20210405120427097

删除文档

​ 使用DELETE删除文档:

DELETE /test_index/userinfo/1/

image-20210405120507615

posted @ 2021-04-05 12:07  云崖君  阅读(45)  评论(0编辑  收藏  举报