【发布时间】:2019-01-18 16:22:36
【问题描述】:
我正在尝试,但无法访问下面提到的 hits["_index"]、hits["_type"]、hits["_id"]、hits["_score"] 和 hits["_source"]字典加载到数据库中。
尝试访问下面的每个 key-val 对:
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
**"hits" : {
"total" : 2700881,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_index",
"_type" : "doc",
"_id" : "R22224!!5333e7e4-9ee3-45f4-9dc3-2a8b8d8cdcf8",
"_score" : 1.0,
"_source" : {
"duration" : 14986283,
"group_id" : "com",
"var_time" : "2018-04-24T17:05:13.082+02:00",
"var_name" : "2",
}
}
]
}**
}
我尝试过的事情:
尝试了以下 c# 代码并查看了以下网址:
Unable to fetch _source dictionary key-val from elastic client search response https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html
public class HitsDocument
{
[PropertyName("_index")]
public string Hitsindex { get; set; }
[PropertyName("_type")]
public string Hitstype { get; set; }
[PropertyName("_id")]
public string Hitsid { get; set; }
[PropertyName("_score")]
public string Hitsscore { get; set; }
[PropertyName("_source")]
public RawDocument Hitssource { get; set; }
}
public class RawDocument
{
[PropertyName("duration")]
public long Duration { get; set; }
[PropertyName("group_id")]
public string GroupId { get; set; }
[PropertyName("var_time")]
public DateTime Vartime { get; set; }
[PropertyName("var_name")]
public string Varname { get; set; }
}
static void Main(string[] args)
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultMappingFor<HitsDocument>(m => m
.IndexName("test_index")
.TypeName("doc"));
var searchResponse = client.Search<HitsDocument>();
var numberOfSlices = 4;
var scrollAllObservable = client.ScrollAll<HitsDocument>("3m", numberOfSlices)
.Wait(TimeSpan.FromMinutes(5), onNext: s =>
{
var docs = s.SearchResponse.DebugInformation;
var documents = s.SearchResponse.Hits;
foreach (var document in documents)
{
// do something with this set of documents
// business logic to load into the database.
MessageBox.Show("document.Id=" + document.Id);
MessageBox.Show("document.Score=" + document.Score);
MessageBox.Show("document.Source=" + document.Source);
MessageBox.Show("document.Type=" + document.Type);
MessageBox.Show("document.Index=" + document.Index);
}
});
}
我做错了什么,还请指出文档的方向,以便更好地理解嵌套字典的 API 客户端?
提前致谢。
更新: 以下代码解决方案最初由 @Russ Cam 在此 link 中回答。 我之前没有意识到这一点。
public class RawDocument
{
[PropertyName("duration")]
public long Duration { get; set; }
[PropertyName("group_id")]
public string GroupId { get; set; }
[PropertyName("var_time")]
public DateTime Vartime { get; set; }
[PropertyName("var_name")]
public string Varname { get; set; }
}
static void Main(string[] args)
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultMappingFor<RawDocument>(m => m
.IndexName("test_index")
.TypeName("doc"));
var searchResponse = client.Search<RawDocument>();
var numberOfSlices = 4;
var scrollAllObservable = client.ScrollAll<RawDocument>("3m", numberOfSlices)
.Wait(TimeSpan.FromMinutes(5), onNext: s =>
{
var docs = s.SearchResponse.DebugInformation;
var documents = s.SearchResponse.Hits;
foreach (var document in documents)
{
// do something with this set of documents
// business logic to load into the database.
MessageBox.Show("document.Id=" + document.Id);
MessageBox.Show("document.Score=" + document.Score);
MessageBox.Show("document.Source.duration=" + document.Source.duration);
MessageBox.Show("document.Source.var_time=" + document.Source.var_time);
MessageBox.Show("document.Source.var_name=" + document.Source.var_name);
MessageBox.Show("document.Type=" + document.Type);
MessageBox.Show("document.Index=" + document.Index);
}
});
}
【问题讨论】:
-
我没试过,但是,你不应该从root读取吗?
-
可能值得在下面添加您的
Update作为答案,并接受它。这将在未来帮助其他人解决类似的问题:)
标签: c# elasticsearch visual-studio-2015 nest