Elasticsearch---CRUED操作

一、创建新文档:

要点:

1). _index_type_id三者唯一确定一个文档。

2). 创建时指定版本:参数 version= 2&version_type=external 

1. 自动生成唯一_id:POST /_index/_type/

请求:
POST 'http://localhost:9200/myIndex/myType/' -d
'{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}'


结果:
创建成功:​
{
    "_index": "myIndex",
    "_type": "myType",
    "_id": "jHWUJmwBwlZ12bzxE2VW",
    "_version": 1, //当前版本
    "result": "created", //结果
    "_shards": {  //分片信息
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 19,
    "_primary_term": 2
}
​创建失败:
{
  "error" : "DocumentAlreadyExistsException[[website][4] [blog][123]:
             document already exists]",
  "status" : 409  //创建失败
}

​

​

2. 指定_id:

PUT 'http://localhost:9200/_index/_type/_id?op_type=create'

PUT 'http://localhost:9200/_index/_type/_ud/_create'

二、更新文档:

要点: 

1. 文档在Elasticsearch中是不可变的——我们不能修改他们,更新已存在的文档实际替换掉它。

2. 操作后_version会自动+1.

3.过程:从旧文档中检索JSON--->修改它-->删除旧文档-->索引新文档

4. 更新时指定版本:参数 version= 2&version_type=external 

1. 更新整个文档:PUT /_index/_type/_id ? version=1

 version: 只希望文档的_version1时更新才生效

请求:
PUT /website/blog/123
{
  "title": "My first blog entry",
  "text":  "I am starting to get the hang of this...",
  "date":  "2014/01/02"
}
结果:_version增加
{
    "_index": "websit",
    "_type": "blog",
    "_id": "2",
    "_version": 3, //版本号+1
    "result": "updated", //结果
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 23,
    "_primary_term": 2
}

2. 更新局部:POST /_index/_type/_id/_update ? retry_on_confilct=5 

 doc:文档里fildes列表

retry_on_confilct: 在错误发生前重试更新5次

{"doc":{ "filde1":xxx, "filde2":xxx,}} 用于:1. 局部更 新  2. 增加新字段

请求参数(doc 必须):
{
    "doc":{
      "title": "My first blog entry", //1. 更新字段信息
      "text":  "I am starting to get the hang of this...",
      "date":  "2014/01/01" //2. 插入新字段
    }
}
结果:
{
    "_index": "megacorp",
    "_type": "employee",
    "_id": "2",
    "_version": 4,
    "result": "updated", //结果
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 24,
    "_primary_term": 2
}

使用Grovy脚本局部更新:

script: 关键字

ctx._source:表示文档

param:脚本参数输入

{
   "script" : "ctx.op = ctx._source.views == count ? 'delete' : 'none'",//脚本
    "params" : { //可用于脚本的参数
        "count": 1
    }
}

{
  "script" : {
        "inline": "ctx._source.counter += count",
        "params" : { "count" : 4 }
    },
}

3. upsert操作:(更新可能不存在文档)

upsert: 参数定义文档来使其不存在时被创建

POST /website/pageviews/1/_update
{
   "script" : "ctx._source.views+=1", //文档存在时执行update操作
   "upsert": {            //文档不存在时,执行insert操作
       "views": 1
   }
}

三、删除文档:DELETE  /_index/_type/_id

请求:DELETE /website/blog/123
结果:{
    "_index": "websit",
    "_type": "blog",
    "_id": "123",
    "_version": 2,
    "result": "deleted", 
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 28,
    "_primary_term": 2
}

四、获取文档:GET   /_index/_type/_id?pretty  (都是指_id)

pretty: 输出(pretty-print)JSON响应以便更加容易阅读

_source: 指定输入的字段

_mget:批量获取:doc参数指定不同的_index/_type/_id

请求:GET /website/blog/123?pretty

结果: {
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 1,
  "found" :    true, //结果
  "_source" :  {
      "title": "My first blog entry",
      "text":  "Just trying this out...",
      "date":  "2014/01/01"
  }
}
(2)筛选输出指定字段
GET /website/blog/123?_source=title,text
_source字段现在只包含我们请求的字段,而且过滤了其它字段:
{
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 1,
  "exists" :   true,
  "_source" : {
      "title": "My first blog entry" ,
      "text":  "Just trying this out..."
  }
}
(3)只输入_source字段,不输出元数据
GET /website/blog/123/_source
它仅仅返回:
{
   "title": "My first blog entry",
   "text":  "Just trying this out...",
   "date":  "2014/01/01"
}
(4)批量获取: 
POST /_mget
{
   "docs" : [
      {
         "_index" : "website",
         "_type" :  "blog",
         "_id" :    2
      },
      {
         "_index" : "website",
         "_type" :  "pageviews",
         "_id" :    1,
         "_source": "views"
      }
   ]
}

五、Bulk API 批量CRUDE操作:

发布了18 篇原创文章 · 获赞 1 · 访问量 3912

猜你喜欢

转载自blog.csdn.net/silmeweed/article/details/97232156