| endpoint | get |
|---|---|
| lang | ruby |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
Use client.get to retrieve a document by its ID.
response = client.get(index: 'products', id: 'prod-1')
doc = response['_source']
puts "#{doc['name']} — $#{doc['price']}"The response includes metadata (_index, _id, _version,
_seq_no, _primary_term) alongside the _source document body.
Use _source_includes or _source_excludes to return only the fields
you need:
response = client.get(
index: 'products',
id: 'prod-1',
_source_includes: ['name', 'price']
)To fetch metadata without the source body, disable it entirely:
response = client.get(index: 'products', id: 'prod-1', _source: false)A NotFound error is raised when the document does not exist:
begin
client.get(index: 'products', id: 'prod-999')
rescue Elastic::Transport::Transport::Errors::NotFound
puts 'Document not found'
end