elasticsearch-获取

检索文档

想要从Elasticsearch中获取文档,咱们使用一样的_index、_type、_id,可是HTTP方法改成GET:web

GET /website/blog/123?prettyjson

响应包含了如今熟悉的元数据节点,增长了_source字段,它包含了在建立索引时咱们发送给Elasticsearch的原始文档。app

{curl

  "_index" :   "website",this

  "_type" :    "blog",url

  "_id" :      "123",spa

  "_version" : 1,blog

  "found" :    true,索引

  "_source" :  {文档

      "title": "My first blog entry",

      "text":  "Just trying this out...",

      "date":  "2014/01/01"

  }

}

pretty

在任意的查询字符串中增长pretty参数,相似于上面的例子。会让Elasticsearch美化输出(pretty-print)JSON响应以便更加容易阅读。_source字段不会被美化,它的样子与咱们输入的一致。

GET请求返回的响应内容包括{"found": true}。这意味着文档已经找到。若是咱们请求一个不存在的文档,依旧会获得一个JSON,不过found值变成了false。此外,HTTP响应状态码也会变成404.

咱们能够在curl后加-i参数获得响应头:

curl -i -XGET http://localhost:9200/website/blog/124?pretty

如今响应相似于这样:

HTTP/1.1 404 Not Found

Content-Type: application/json; charset=UTF-8

Content-Length: 83

 

{

  "_index" : "website",

  "_type" :  "blog",

  "_id" :    "124",

  "found" :  false

}

检索文档的一部分

一般,GET请求将返回文档的所有,存储在_source参数中。可是可能你感兴趣的字段只是title。请求个别字段能够使用_source参数。多个字段能够使用逗号分隔:

GET /website/blog/123?_source=title,text

_source字段如今只包含咱们请求的字段,并且过滤了date字段:

{

  "_index" :   "website",

  "_type" :    "blog",

  "_id" :      "123",

  "_version" : 1,

  "exists" :   true,

  "_source" : {

      "title": "My first blog entry" ,

      "text":  "Just trying this out..."

  }

}

或者你只想获得_source字段而不要其余的元数据,你能够这样请求:

GET /website/blog/123/_source

它仅仅返回:

{

   "title": "My first blog entry",

   "text":  "Just trying this out...",

   "date":  "2014/01/01"

}

相关文章
相关标签/搜索