Gianluca Ghettini
Gianluca Ghettini

Reputation: 11638

ElasticSearch - how to get the auto generated id from an insert query

On my ElasticSearch database I need to get the autogenerated id from my insert query (I'm using .NET C#). How to do it? I tried debugging the readRecords response but I didn't find such id.

Basically I need the equivalent of the MySQL LAST_INSERT_ID() command.

var readRecords = elasticClient.Search<HistoryRecord>(s => s
                .Index(elasticIndexName)
                .Filter(f =>
                    f.Term(t => t.MacAddr, historyRecord.MacAddr) &&
                    f.Term(t => t.GroupName, historyRecord.GroupName) &&
                    f.Term(t => t.GroupNo, historyRecord.GroupNo) &&
                    f.Term(t => t.InstrType, historyRecord.InstrType) &&
                    f.Term(t => t.InstrumentAddress, historyRecord.InstrumentAddress) &&
                    f.Term(t => t.InstrumentName, historyRecord.InstrumentName) &&
                    f.Term(t => t.UhhVersion, historyRecord.UhhVersion))).Documents 

Upvotes: 0

Views: 1712

Answers (1)

Rick Haffey
Rick Haffey

Reputation: 502

You can find the id values from the ISearchResponse (based on your code example above) by looking at the objects in the Hits collection, rather than the Documents collection. Each Hit has an Id property.

In the original indexing call (assuming you're doing that individually -- not via the _bulk endpoint), you can get the id of the newly indexed document off the IIndexResponse's Id property.

Upvotes: 2

Related Questions