无法从弹性客户端搜索响应中获取嵌套键值对字典
Unable to fetch nested key-val pairs dictionary from elastic client search response
我正在尝试,但无法从下面提到的字典加载到数据库中。
正在尝试访问下面的每个键值对:
{
"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# 代码并查看了以下网址:
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 在此 中回答。
我之前没有意识到。
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);
}
});
}
一个想法:
首先,从根读取,从根创建映射 classes。 Tok、TimedOut、Shards 和 Hits。
其次,您的 classes 是为 "hits.hits" 而设计的。这是一个数组,所以它需要像 List 这样的 IEnumerable。请根据需要添加 [PropertyName("...")] 自行完成:
public class OuterHits
{
public string total {get;Set;}
public string max_score {get;Set;}
public List<RawDocument> hits {get;Set;} // or hits[]
}
您可能还需要根 class
public class rootClass
{
public string string took {get;Set;}
public string timeout {get;Set;}
public Shards shards {get;Set;}
public OuterHits hits {get;Set;}
}
同时实施碎片 class
正如@Russ Cam 所说,以下解决方案更新作为答案发布,供其他人审阅。
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);
}
});
}
我正在尝试,但无法从下面提到的字典加载到数据库中。
正在尝试访问下面的每个键值对:
{
"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# 代码并查看了以下网址:
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 在此
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);
}
});
}
一个想法:
首先,从根读取,从根创建映射 classes。 Tok、TimedOut、Shards 和 Hits。
其次,您的 classes 是为 "hits.hits" 而设计的。这是一个数组,所以它需要像 List 这样的 IEnumerable。请根据需要添加 [PropertyName("...")] 自行完成:
public class OuterHits
{
public string total {get;Set;}
public string max_score {get;Set;}
public List<RawDocument> hits {get;Set;} // or hits[]
}
您可能还需要根 class
public class rootClass
{
public string string took {get;Set;}
public string timeout {get;Set;}
public Shards shards {get;Set;}
public OuterHits hits {get;Set;}
}
同时实施碎片 class
正如@Russ Cam 所说,以下解决方案更新作为答案发布,供其他人审阅。
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);
}
});
}