取回一个文档

get /website/blog/123?pretty
结果为:

  1. {
  2. "_index" : "website",
  3. "_type" : "blog",
  4. "_id" : "123",
  5. "_version" : 1,
  6. "found" : true,
  7. "_source" : {
  8. "title": "My first blog entry",
  9. "text": "Just trying this out...",
  10. "date": "2014/01/01"
  11. }
  12. }

返回文档的一部分

get /website/blog/123?_source=title,text
该 _source 字段现在包含的只是我们请求的那些字段,并且已经将 date 字段过滤掉了。

  1. {
  2. "_index" : "website",
  3. "_type" : "blog",
  4. "_id" : "123",
  5. "_version" : 1,
  6. "found" : true,
  7. "_source" : {
  8. "title": "My first blog entry" ,
  9. "text": "Just trying this out..."
  10. }
  11. }

或者,如果你只想得到 _source 字段,不需要任何元数据,你能使用 _source 端点:
GET /website/blog/123/_source

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

查询多个文档

你需要从 Elasticsearch 检索很多文档,那么使用 multi-get 或者 mget API 来将这些检索请求放在一个请求中,将比逐个文档请求更快地检索到全部文档。
1、GET /_mget
2、GET /website/blog/_mget
3、GET /website/blog/_mget
{
“ids” : [ “2”, “1” ]
}
结果如下:

  1. {
  2. "docs" : [
  3. {
  4. "_index" : "website",
  5. "_type" : "blog",
  6. "_id" : "2",
  7. "_version" : 10,
  8. "found" : true,
  9. "_source" : {
  10. "title": "My first external blog entry",
  11. "text": "This is a piece of cake..."
  12. }
  13. },
  14. {
  15. "_index" : "website",
  16. "_type" : "blog",
  17. "_id" : "1",
  18. "found" : false
  19. }
  20. ]
  21. }

查看文档是否存在

curl -i -XHEAD http://localhost:9200/website/blog/123
若返回成功则:

  1. HTTP/1.1 200 OK
  2. Content-Type: text/plain; charset=UTF-8
  3. Content-Length: 0

若文档不存在, Elasticsearch 将返回一个 404 Not Found 的状态码。

更新整个文档

可以指定版本

  1. PUT /website/blog/1?version=1
  2. {
  3. "title": "My first blog entry",
  4. "text": "Starting to get the hang of this..."
  5. }
  6. 当版本1不存在时,
  7. 返回 409 Conflict HTTP 响应码,和一个如下所示的响应体:
  8. {
  9. "error": {
  10. "root_cause": [
  11. {
  12. "type": "version_conflict_engine_exception",
  13. "reason": "[blog][1]: version conflict, current [2], provided [1]",
  14. "index": "website",
  15. "shard": "3"
  16. }
  17. ],
  18. "type": "version_conflict_engine_exception",
  19. "reason": "[blog][1]: version conflict, current [2], provided [1]",
  20. "index": "website",
  21. "shard": "3"
  22. },
  23. "status": 409
  24. }
PUT /website/blog/123
{
  "title": "My first blog entry",
  "text":  "I am starting to get the hang of this...",
  "date":  "2014/01/02"
}

更新部分文档

增加字, ?retry_on_conflict=5 设置重试次数

POST /website/blog/1/_update?retry_on_conflict=5 
{
   "doc" : {
      "tags" : [ "testing" ],
      "views": 0
   }
}

创建文档三种方式

1、post方式,自动生成id
POST /website/blog/
2、put方式
PUT /website/blog/123?op_type=create
PUT /website/blog/123/_create

删除文档

DELETE /website/blog/123
如果找到该文档,Elasticsearch 将要返回一个 200 ok 的 HTTP 响应码,和一个类似以下结构的响应体。

{
  "found" :    true,
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 3
}

如果文档没有找到,我们将得到 404 Not Found 的响应码和类似这样的响应体

{
  "found" :    false,
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 4
}