elasticsearch-definitive-guide-en

[[get-doc]] === Retrieving a Document

To get the document ((("documents", "retrieving")))out of Elasticsearch, we use the same _index, _type, and _id, but the HTTP verb ((("HTTP methods", "GET")))changes to GET:

[source,sh]

GET /website/blog/123?pretty

// SENSE: 030_Data/15_Get_document.json

The response includes the by-now-familiar metadata elements, plus ((("_source field", sortas="source field")))the _source field, which contains the original JSON document that we sent to Elasticsearch when we indexed it:

[source,js]

{ "_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]

Adding pretty to the query-string parameters for any request,((("query strings", "adding pretty"))) as in the preceding example, causes Elasticsearch to pretty-print the((("pretty-printing JSON response"))) JSON 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.

The response to the +GET+ request includes {"found": true}. This confirms that the document was found. ((("documents", "requesting non-existent document")))If we were to request a document that doesn't exist, we would still get a JSON response, but found would be set to false.

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((("curl command", "-i argument"))) causes it to display the response headers:

[source,sh]

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

// SENSE: 030_Data/15_Get_document.json

The response now looks like this:

[source,js]

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

By default, a GET request((("documents", "retrieving part of"))) will return the whole document, as stored in the _source field. But perhaps all you are interested in is the title field. Individual fields can be ((("fields", "returning individual document fields")))((("_source field", sortas="source field")))requested by using the _source parameter. Multiple fields can be specified in a comma-separated list:

[source,sh]

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

// SENSE: 030_Data/15_Get_document.json

The _source field now contains just the fields that we requested and has filtered out the date field:

[source,js]

{ "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 1, "exists" : 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:

[source,sh]

GET /website/blog/123/_source

// SENSE: 030_Data/15_Get_document.json

which returns just the following:

[source,js]

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

}