# Get by Id Missing in Current C#/NEST Documentation

**URL:** <https://discuss.elastic.co/t/get-by-id-missing-in-current-c-nest-documentation/153496>\
**Category:** Elasticsearch\
**Created:** [October 22, 2018, 10:50pm UTC](https://discuss.elastic.co/t/get-by-id-missing-in-current-c-nest-documentation/153496 "2018-10-22T22:50:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iinuwa](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@iinuwa](https://discuss.elastic.co/u/iinuwa)\
**Post date:** [October 22, 2018, 10:50pm UTC](https://discuss.elastic.co/t/get-by-id-missing-in-current-c-nest-documentation/153496/1 "2018-10-22T22:50:41Z")

</div>

Hello,

I am wondering what is the best way to query by document Id. In previous versions of the NEST documentation, there was an example of using the Get() method of the ElasticClient class, e.g. [1.x Documentation](https://www.elastic.co/guide/en/elasticsearch/client/net-api/1.x/get.html). The current documentation does not reference that field, and it seems that the method works differently now, requiring Document paths, e.g.:

```
var path = new DocumentPath<Type>(id).Type("_doc").Index("index-name");
var response = _elasticClient.Get(path);

```

or using the static method:

```
var path = DocumentPath<T>.Id(id).Type("_doc").Index("index-name");
var response = _elasticClient.Get(path);

```

Is this the recommended way to get an document by its Id? Or should we use the Ids query?

```
new IdsQuery
{
    Values = new List<Id> { id },
    Types = Type<ContactInfo>()
}

```

Or should we use a Match query on the \_id field?

```
new MatchQuery
{
     Query = id
     Field = Infer.Field<Type>(t => t._id)
}

```

Thanks for your help,

Isaiah

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [October 23, 2018, 2:13am UTC](https://discuss.elastic.co/t/get-by-id-missing-in-current-c-nest-documentation/153496/2 "2018-10-23T02:13:22Z")

</div>

Using the GET API is the recommended method.

A `DocumentPath<T>` is simply a type that can resolve a path from a POCO, so for example, if you have a POCO called `ContactInfo` that looks like

```auto
public class ContactInfo
{
	public string Id { get; set; }
	
	// other properties
}

```

You can retrieve a document using the Get API with

```auto
var getResponse = client.Get<ContactInfo>("<document id>");

```

This will make a request to the _default_ index configured on `ConnectionSettings`, and infer the type name from the POCO type. For example, with the following

```auto
var defaultIndex = "my-index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
    .DefaultIndex(defaultIndex);

var client = new ElasticClient(settings);

var getResponse = client.Get<ContactInfo>("<document id>");

```

The request will be sent to

```auto
GET http://localhost:9200/my-index/contactinfo/<document id>?

```

Now, you can also configure a default index and type name for `ContactInfo` on `ConnectionSettings`

```auto
var defaultIndex = "my-index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
    .DefaultIndex(defaultIndex)
    .DefaultMappingFor<ContactInfo>(m => m
        .IndexName("contacts")
        .TypeName("doc")
    );

var client = new ElasticClient(settings);

var getResponse = client.Get<ContactInfo>("<document id>");

```

which will send a request to

```auto
GET http://localhost:9200/contacts/doc/<document id>

```

You can of course just pass the index and type name to use in the request

```auto
var getResponse = client.Get<ContactInfo>("<document id>", g => g
	.Index("another-index")
	.Type("another-type")
);

```

which sends a request to

```auto
GET http://localhost:9200/another-index/another-type/<document id>

```

Or you could construct a `DocumentPath<T>` as you're doing.

The intention with accepting a `DocumentPath<T>` as opposed to just an Id for the first parameter is that your application may have an instance of a POCO already, but may need to get an updated version from Elasticsearch. In this case, you can simply pass the POCO as the first parameter

```auto
var oldContactInfo = new ContactInfo { Id = "<document id>" };
var getResponse = client.Get<ContactInfo>(oldContactInfo);

```

And this will use the Id from the document, in addition to index name and type name that may be configured for the `ContactInfo` type.

Take a look at the [documentation on `DocumentPath<T>`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/document-paths.html) for further details

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 20, 2018, 2:13am UTC](https://discuss.elastic.co/t/get-by-id-missing-in-current-c-nest-documentation/153496/3 "2018-11-20T02:13:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
