提炼:
- GET /website/blog/123?pretty 格式化取回文档
- curl -i -XGET http://localhost:9200/website/blog/124?pretty 返回 metadata (带响应头)
- GET /website/blog/123?_source=title,text 选择性返回原信息(带 metadata)
- GET /website/blog/123/_source 只返回源信息(不带 metadata)
原文:
为了从 Elasticsearch 中检索出文档, 我们仍然使用相同的 _index/_type/_id, 但是 HTTP 谓词更改为 GET:
To get the document out of Elasticsearch, we use the same _index, _type, _id, but the HTTP verb changes to GET:
GET /website/blog/123?pretty
响应体包括目前已经熟悉了的元数据元素, 再加上 _source 字段,这个字段包含我们索引数据时发送给 Elasticsearch 的原始 JSON 文档:
The response includes the by-now-familiar metadata elements, plus the _source field, which contains the original JSON document that we sent to Elasticsearch when we indexed it:
{ “_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” } }
NOTE:
在请求的查询串参数中加上 pretty 参数, 正如前面的例子中看到的, 这将会调用 Elasticsearch 的 pretty-print 功能, 该功能使得 JSON 响应体更可读。但是 _source 字段不能被格式化打印出来。相反, 我们得到的 _source 字段中的 JSON 串, 刚好是和我们传给它的一样。
Adding pretty to the query-string parameters for any request, as in the preceding example, cause Elasticsearch to pretty-print the Jsonn response to make it more readable. The _source field, however, isn’t pretty-printed. Instead we get back exactly the same JSON string that we passed in.
GET 请求的响应体包括 {“found”: true} ,这证实了文档已经被找到。 如果我们请求一个不存在的文档,我们仍旧会得到一个 JSON 响应体,但是 found 将会是 false 。 此外, HTTP 响应码将会是 404 Not Found ,而不是 200 OK 。
The response to the GET request includes {“found”: true} . This confirms that the document was found. If we were to request a document that dosen’t exist, we would still get a JSON response, but found would be set to false.
我们可以通过传递 -i 参数给 curl 命令,该参数 能够显示响应的头部:
Also, the HTTP response code would be 404 Not Found instead of 200 OK. We can see this by passing the -i argument to curl, which causes it to display the response headers:
curl -i -XGET http://localhost:9200/website/blog/124?pretty
显示响应头部的响应体现在类似这样:
The response now looks like this:
HTTP/1.1 404 Not Found Content-Type: application/json; charset=UTF-8 Content-Length: 83 { “_index” : “website”, “_type” : “blog”, “_id” : “124”, “found” : false }
Retrieving Part of a Document(返回文档的一部分)
默认情况下, GET 请求 会返回整个文档,这个文档正如存储在 _source 字段中的一样。但是也许你只对其中的 title 字段感兴趣。单个字段能用 _source 参数请求得到,多个字段也能使用逗号分隔的列表来指定。
By default, a GET request will return the whole document, as stored in the _source field. But perhaps all you are interested in are the title and text fields. Individual fields can be requested by using the _source parameter. Mutiple fields can be specifield in a comma-separated list:
GET /website/blog/123?_source=title,text
该 _source 字段现在包含的只是我们请求的那些字段,并且已经将 date 字段过滤掉了。
The _source field now contains just the fields that we requested and has filtered out the date field:
{ “_index” : “website”, “_type”: “blog”, “_id”: “123”, “_version”: 1, “found”: true, “_source” : { “title” : “My first blog entry”, “text” : “Just trying this out…” } }
Or if you want just the _source field without any metadata, you can use the _source endpoint:
GET /website/blog/123/_source
which returns just the following:
{ “title”: “My first blog entry”, “text”: “Just trying this out…”, “date”: “2014/01/01” }