NEST API - Document Suggester Document Id

I know that if you have a field called Id, then Elasticsearch will use that field as the Id of the document.

However, we want Elasticsearch to create the Ids for us.

We are using the Suggester and we need to access the Id of each document that is returned.

With the NEST API, there is a property called Nest.Id, however, it is an object and does not have any public properties for us to use in order to get the actual value it represents.

Is there any way we can get this Id value from the Nest.Id object.

Many thanks

If you don't have an Id field on the POCO that represents the document, then upon indexing, Elasticsearch will create an id for the document and populate the _id field on the document metadata. You need to be sure to always provide this id whenever you're interacting with the document, such as updating it; the value can be supplied using the Id property on the request type, or .Id() method on the fluent API descriptor.

You can access the _id on the hits metadata which is mapped as Nest.Id with

var myCompletionSuggest = response.Suggest["my-completion-suggest"];

var suggest = myCompletionSuggest.First();
var option = suggest.Options.First();

// in this instance, you could pass null instead of connection settings
var idValue = ((IUrlParameter)option.Id).GetString(connectionSettings);

I think Index and Id ought to be mapped as string here though, to help in using them.

Many,Many Thanks Russ.

I will try this.

Regards

Hassan

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