ITPub博客

首页 > IT基础架构 > > ElasticSearch基本实操(2)

ElasticSearch基本实操(2)

作者:格伯纳 时间:2020-05-29 13:55:49 0 删除 编辑

#修改文档原理 - PUT方式

发生并发冲突的可能性比较大

  • POST方式

发生并发冲突可能性降低

查询出document然后使用用户提交过来的数据更新到document中, 已有的document被标记为deleted创建一个新的document。

post方式比put方式网络数据传输的次数要少,从而提高了性能post方式从查询文档到修改文档再到 创建新的文档都是在es内部实现的

修改文档内容

GET /lib/user/_search

全部替换

PUT /lib/user/4
{
  "first_name":"Jane",
  "last_name":"Lucy",
  "age":28,
  "about":"I like to collect rock albums",
  "interests":["music"]
}

部分替换(partial update)

POST /lib/user/4/_update
{
  "doc":{
    "age":29
  }
}

删除文档

DELETE /lib/user/4

基于groovy脚本执行partial update

修改年龄

POST /lib/user/4/_update
{
  "script": "ctx._source.age+=1"
}

修改名字

POST /lib/user/4/_update
{
  "script": "ctx._source.last_name+='Chan'"
}

添加爱好

POST /lib/user/4/_update
{
  "script": {"source":"ctx._source.interests.add(params.tag)","params": {"tag":"picture"}}
}

删除爱好

POST /lib/user/4/_update
{
  "script": {"source":"ctx._source.interests.remove(ctx._source.interests.indexOf(params.tag))","params": {"tag":"picture"}}
}

删除文档 age=30

POST /lib/user/4/_update
{
  "script": {
    "source":"ctx.op=ctx._source.age==params.count?'delete':'none'",
    "params": {
      "count":30
    }
  }
}

upsert操作,如果文档不存在则初始化,不存在则执行

POST /lib/user/4/_update
{
  "script": "ctx._source.age+=1",

  "upsert": {
    "first_name":"Jane",
    "last_name":"Lucy",
    "age":20,
    "about":"I like to collect rock albums",
    "interests":[
      "music"
      ]

  }
}

多index,多type查询

ES6一个索引下,只能只有一个类型

查询结果基本信息

{
  "took": 0,  #耗时
  "timed_out": false,  #是否超时
  "_shards": {         #分片信息
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  }

查询所有文档

GET _search
GET /_all/_search

查询某个索引下的所有文档

GET /lib/_search
GET /lib/user/_search

查询两个索引

GET /lib,lib3/_search

使用匹配符号

GET /*3,*4/_search

查询两个索引

GET /lib,lib3/user,items/_search
GET /_all/user,items/_search

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31509949/viewspace-3140563/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
管理员

注册时间:2018-03-30

  • 博文量
    2383
  • 访问量
    984382