Automatically create and display the _ID NEST identifier

I want the identifier to be automatically generated when I index the document in elastic search. This works fine when I don't specify the Id property in my poco.

What I would like to do is map the _id base field to my poco class when getting and using the automatically generated identifier when indexing. It looks like I can either specify an identifier or not at all. Do they have any api socket options that I am missing?

EDIT

Example https://gist.github.com/antonydenyer/9074159

+6
source share
3 answers

As @ Duc.Duong said in the comments, you can access the Id using DocumentWithMeta . In the current version of NEST, DocumentsWithMetaData is replaced by Hits , and you must also distinguish 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(); 
+2
source

Auto ID Creation

An index operation can be performed without specifying an identifier. In this case, the identifier will be generated automatically. In addition, op_type will be automatically configured to create. Here is an example (note that POST is used instead of PUT):

 $ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{ "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" }' 

Result:

 { "_index" : "twitter", "_type" : "tweet", "_id" : "6a8ca01c-7896-48e9-81cc-9f70661fcb32", "_version" : 1, "created" : true } 
+2
source

Assume that NEST automatically determines the "Id" field in the class and maps it to the "_id" in ES. Your requirement looks strange, but why not create 2 classes for it? One for indexing (without an Id field), one for receiving (inherited from the indexing class and declaring a new "Id" field)?

eg:.

 public class DataForIndex { public string Name { get; set; } // some other fields... } public class DataForGet : DataForIndex { public int Id { get; set; } } 
-1
source

Source: https://habr.com/ru/post/956565/


All Articles