WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Get a document
editGet a document
editGets a single document from Elasticsearch
By Id
editvar response = client.Get<ElasticSearchProject>(1);
Index and type are inferred but overloads still exists for full control:
var response = client.Get<ElasticSearchProject>("myindex", "mytype", 1);
Handling the Get response
editThe Get<T>()
call returns an IGetResponse<T>
that holds the requested document as well as other meta data returned from Elasticsearch.
response.Source
holds the ElasticSearchProject with id 1
.
You can also use Get<T>()
to query just some fields of a single document:
Fluent DSL
editvar response = client.Get<ElasticsearchProject>(g => g .Index("myindex") .Type("mytype") .Id(1) .Fields(p=>p.Content, p=>p.Name, p=>p.Id, p=>p.DoubleValue) );
Object Initializer Syntax
editvar request = new GetRequest("myindex", "mytype", "1") { Fields = new PropertyPathMarker[] { "content", "name", "id" } }; var response = client.Get<ElasticsearchProject>(request);
You can then access the fields like so:
var name = response.Fields.FieldValue<string>(p => p.Name); var id = response.Fields.FieldValue<int>(p => p.Id); var doubleValue = response.Fields.FieldValue<double>(p => p.DoubleValue);
Remember p => p.Name
can also be written as "name"
and NEST does not force you to
write expressions everywhere (although it is much encouraged!).