I want the id to be automatically generated when I index a document into elastic search. This works fine when I don't specify an Id property in my poco.
What I would like to do is map the underlying _id field onto my poco class when getting and use auto generated id when indexing. It looks like I can either specify the id or not at all. Are their any nest api options that I am missing?
EDIT
Example gist https://gist.github.com/antonydenyer/9074159
As @Duc.Duong said in comments you can access Id using DocumentWithMeta
.
In current version of NEST DocumentsWithMetaData
is replaced with Hits
and also you should cast IHit<DataForGet>.Id
from string
to int
.
this is my code:
public class DataForIndex
{
public string Name { get; set; }
// some other fields...
}
public class DataForGet : DataForIndex
{
public int Id { get; set; }
}
var result = client.Search<DataForGet>(x => x.Index("index").MatchAll());
var list = results.Hits.Select(h =>
{
h.Source.Id = Convert.ToInt32(h.Id);
return h.Source;
}).ToList();