elaticsearch学习(一)

概念

  • _index
    文档在哪存放
  • _type(在7.x版本弱化此概念)
    文档表示的对象类别
  • _id
    文档的唯一标识

新增文档

PUT

格式

PUT /{index}/{type}/{id}
{
  "field": "value",
  ...
}

示例

PUT /website/blog/123
{
  "title": "My first blog entry",
  "text":  "Just trying this out...",
  "date":  "2014/01/01"
}

结果

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

特点

具有幂等性,每次操作都是全覆盖,无法做更新操作

POST

格式

自动生成id
POST /{index}/{type}
{
"field": "value",
...
}
//指定id
POST /{index}/{type}/{id}
{
"field": "value",
...
}

示例

POST /website/blog/
{
"title": "My second blog entry",
"text":  "Still trying this out...",
"date":  "2014/01/01"
}

POST /website/blog/123
{
"title": "My second blog entry",
"text":  "Still trying this out...",
"date":  "2014/01/01"
}

POST /website/blog/123
{
"title": "我只修改这一行"
}

创建时增加条件

如果创建指定inde、id的文档存在的话,则返回错误信息。
不存在则创建。

示例

PUT /website/blog/123/_create
{ ... }

PUT /website/blog/123?op_type=create
{ ... }

取回文档

GET

示例

GET /website/blog/123/_source
GET /website/blog/123?pretty    --- pretty格式化输出,是输出结果更加美观
GET /website/blog/123  --- 返回指定id全部文档内容

结果

{
  "title" : "My first blog entry",
  "text" : "Just trying this out...",
  "date" : "2014/01/01"
}

格式化后的结果
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 16,
  "_seq_no" : 15,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "My first blog entry",
    "text" : "Just trying this out...",
    "date" : "2014/01/01"
  }
}

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 16,
  "_seq_no" : 15,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "My first blog entry",
    "text" : "Just trying this out...",
    "date" : "2014/01/01"
  }
}

更新文档

PUT

特点:全量更新

POST

示例

POST /website/blog/123/_update
{
  "doc":{
     "date":  "2018/01/01"
  }
}

结果

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 18,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 17,
  "_primary_term" : 1
}

特点:与PUT相比,不具有幂等性,可以局部更新

删除文档

DELETE

示例

DELETE /website/blog/123

结果

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 19,
  "result" : "deleted", ---成功 deleted  ---失败 not_found
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 18,
  "_primary_term" : 1
}

Q.E.D.


一个热爱生活的95后精神小伙