ElasticSearch - 如何从插入查询中获取自动生成的 ID
本文关键字:获取 自动生成 ID 查询 插入 ElasticSearch | 更新日期: 2023-09-27 17:56:52
在我的ElasticSearch数据库上,我需要从插入查询中获取自动生成的id(我使用的是.NET C#)。怎么办?我尝试调试 readRecords 响应,但没有找到这样的 id。
基本上我需要相当于MySQL LAST_INSERT_ID()
命令。
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
可以通过查看 Hits
集合(而不是 Documents
集合)中的对象,从ISearchResponse
中找到 id 值(基于上面的代码示例)。 每Hit
都拥有Id
属性。
在原始索引调用中(假设您单独执行此操作 - 而不是通过 _bulk
端点),您可以从IIndexResponse
的 Id
属性中获取新索引文档的 id。