【发布时间】:2021-05-09 00:43:17
【问题描述】:
作为 NEST 库的新手,用对我来说完全没有意义的文档拉扯我的头发。
第一个问题:无法弄清楚如何将查询的结果映射到强类型模型,尽管我已经捕获了 json 结果并粘贴到了 C# 类,无论我尝试什么,除非我在查询中使用动态类型方法然后结果返回null。
从查询返回的 json 结果派生的模型:
public class Rootobject
{
public int took { get; set; }
public bool timed_out { get; set; }
public _Shards _shards { get; set; }
public Hits hits { get; set; }
}
public class _Shards
{
public int total { get; set; }
public int successful { get; set; }
public int skipped { get; set; }
public int failed { get; set; }
}
public class Hits
{
public Total total { get; set; }
public float max_score { get; set; }
public Hit[] hits { get; set; }
}
public class Total
{
public int value { get; set; }
public string relation { get; set; }
}
public class Hit
{
public string _index { get; set; }
public string _type { get; set; }
public string _id { get; set; }
public float _score { get; set; }
public _Source _source { get; set; }
}
public class _Source
{
public DateTime timestamp { get; set; }
public string level { get; set; }
public string messageTemplate { get; set; }
public string message { get; set; }
public Fields fields { get; set; }
}
public class Fields
{
public string LogEventCategory { get; set; }
public string LogEventType { get; set; }
public string LogEventSource { get; set; }
public string LogData { get; set; }
public string MachineName { get; set; }
public int MemoryUsage { get; set; }
public int ProcessId { get; set; }
public string ProcessName { get; set; }
public int ThreadId { get; set; }
}
查询返回的示例 JSON 结果:
{
"took" : 16,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "webapp-razor-2021.05",
"_type" : "_doc",
"_id" : "n2tbTnkBwE4YgJowzRsT",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2021-05-09T00:41:47.2321845+01:00",
"level" : "Information",
"messageTemplate" : "{@LogEventCategory}{@LogEventType}{@LogEventSource}{@LogData}",
"message" : "\"WebApp-RAZOR\"\"Application Startup\"\"System\"\"Application Starting Up\"",
"fields" : {
"LogEventCategory" : "WebApp-RAZOR",
"LogEventType" : "Application Startup",
"LogEventSource" : "System",
"LogData" : "Application Starting Up",
"MachineName" : "DESKTOP-OS52032",
"MemoryUsage" : 4713408,
"ProcessId" : 15152,
"ProcessName" : "WebApp-RAZOR",
"ThreadId" : 1
}
}
},
{
"_index" : "webapp-razor-2021.05",
"_type" : "_doc",
"_id" : "oGtdTnkBwE4YgJowuxu_",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2021-05-09T00:43:54.0326968+01:00",
"level" : "Information",
"messageTemplate" : "{@LogEventCategory}{@LogEventType}{@LogEventSource}{@LogData}",
"message" : "\"WebApp-RAZOR\"\"Application Startup\"\"System\"\"Application Starting Up\"",
"fields" : {
"LogEventCategory" : "WebApp-RAZOR",
"LogEventType" : "Application Startup",
"LogEventSource" : "System",
"LogData" : "Application Starting Up",
"MachineName" : "DESKTOP-OS52032",
"MemoryUsage" : 4656048,
"ProcessId" : 12504,
"ProcessName" : "WebApp-RAZOR",
"ThreadId" : 1
}
}
},
{
"_index" : "webapp-razor-2021.05",
"_type" : "_doc",
"_id" : "oWtgTnkBwE4YgJownRtc",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2021-05-09T00:47:02.8954368+01:00",
"level" : "Information",
"messageTemplate" : "{@LogEventCategory}{@LogEventType}{@LogEventSource}{@LogData}",
"message" : "\"WebApp-RAZOR\"\"Application Startup\"\"System\"\"Application Starting Up\"",
"fields" : {
"LogEventCategory" : "WebApp-RAZOR",
"LogEventType" : "Application Startup",
"LogEventSource" : "System",
"LogData" : "Application Starting Up",
"MachineName" : "DESKTOP-OS52032",
"MemoryUsage" : 4717560,
"ProcessId" : 17952,
"ProcessName" : "WebApp-RAZOR",
"ThreadId" : 1
}
}
}
]
}
}
第二个问题:我正在尝试按嵌套在 JSON 中的属性进行排序,例如名为“LogEventCategory”的属性
NEST 客户端方法:
var searchResponse = await _elasticClient.SearchAsync<dynamic>(s => s
.RequestConfiguration(r => r
.DisableDirectStreaming()
)
//.AllIndices()
.From(0) // From parameter defines the offset from the first result you want to fetch.
.Size(3) // Size parameter allows you to configure the maximum amount of hits to be returned.
.Index("webapp-razor-*")
//.Index("index-1,index-2")
.Query(q => q
.MatchAll()
)
.Sort(so => so
.Field(fs => fs
//.Field("@timestamp") // this one seems to work
.Field("logEventCategory")
.Order(SortOrder.Ascending)
//.Order(ColumnSortOrder)
)
)
);
知道 NEST 高级客户端旨在映射到正确的模型,我意识到使用动态类型是不正确的,但我只是无法理解为什么使用直接从示例 json 响应创建/映射的类是仍然无法正常工作,并且这样做时返回的结果只是为我提供了前几个属性的空值。
下面的示例仅显示前几项(但仍为空)
【问题讨论】:
标签: c# elasticsearch nest asp.net-core-5.0