温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ElasticSearch基本操作有哪些

发布时间:2021-12-16 10:06:35 来源:亿速云 阅读:164 作者:iii 栏目:大数据

这篇文章主要介绍“ElasticSearch基本操作有哪些”,在日常操作中,相信很多人在ElasticSearch基本操作有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ElasticSearch基本操作有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1、_cat

GET /_cat/nodes: 查看所有节点
GET /_cat/health: 查看健康状况
GET /_cat/master:查看主节点
GET /_cat/indices:查看所有索引 相当于mysql中的showdatabases

2、保存一个文档

PUT customer/external/1;在customer索引(mysql中的数据库)下的external类型(mysql中的表)下保存1号数据(唯一标识)为

PUT customer/external/1
{
  "name":"gison"
}

PUT和POST都可以,

POST新增:如果不指定id,会自动生成id。指定id就会修改这个数据,并新增版本号。

PUT可以新增可以修改。PUT必须指定id;由于PUT需要指定id,我们一般用来做修改操作,不指定id会报错。

操作结果:

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

3、查询

GET customer/external/1

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,//版本号
  "_seq_no" : 1,//并发控制字段,每次更新就会+1,用来做乐观锁
  "_primary_term" : 1,//同上,主分片重新分配,如重启,就会变化
  "found" : true,
  "_source" : {
    "name" : "gison"
  }
}

(新版本的es乐观锁控制用seq_no,老版本用version)

测试一下乐观锁

模拟两个用户A跟B都想改上面这条数据,A用户查出if_seq_no=1,if_primary_term=1,执行更新

PUT customer/external/1?if_seq_no=1&if_primary_term=1
{
  "name":"鸣人"
}

B用户同样也查出if_seq_no=1,if_primary_term=1,随后执行更新

PUT customer/external/1?if_seq_no=1&if_primary_term=1
{
  "name":"卡卡西"
}

A用户执行结果

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

B用户执行结果

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, required seqNo [1], primary term [1]. current document has seqNo [2] and primary term [1]",
        "index_uuid": "XbVN6IayQTWbliz3cOOyGw",
        "shard": "0",
        "index": "customer"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[1]: version conflict, required seqNo [1], primary term [1]. current document has seqNo [2] and primary term [1]",
    "index_uuid": "XbVN6IayQTWbliz3cOOyGw",
    "shard": "0",
    "index": "customer"
  },
  "status": 409
}

4、更新

 post带_update

带_update更新的时候,要加上doc,对比原来数据,与原来一样就不做任何操作

POST customer/external/1/_update
{
  "doc": {
    "name":"gison"
  }
}

返回结果

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 7,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 6,
  "_primary_term" : 1
}

post和put不带_update,都不与前内容做对比

5、删除

DELETE customer/external/1 // 删除一条数据
DELETE customer //删除索引

ES没有提供删除类型的操作

6、bulk批量api

POST customer/external/_bulk
{"delete": {"_index": "website", "_type": "blog", "_id": "123"}}
{"create": {"_index": "website", "_type": "blog", "_id": "123"}}
{"title": "first blog"}
{"index": {"_index": "website", "_type": "blog"}}
{"title": "second blog"}
{"update": {"_index": "website", "_type": "blog", "_id": "123"}}
{"doc":{"title": "update blog"}}

语法格式

{action:{metadata}}\n

{request body} \n

{action:{metadata}}\n

{request body} \n

index和create区别

index时会检查_version。如果插入时没有指定_version,那对于已有的doc,_version会递增,并对文档覆盖。插入时如果指定_version,如果与已有的文档_version不相等,则插入失败,如果相等则覆盖,_version递增。

create时也会检查_version,但是对于已有的文档,不会创建新文档,即插入失败。

到此,关于“ElasticSearch基本操作有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI