将 DSL 查询转换为 NEST(elasticsearch)
Converting DSL Query to NEST (elasticsearch)
我在 Kibana 中的 DSL 查询是:
GET elastic-search-app-log*/_search
{
"_source": ["@timestamp", "level", "messageTemplate", "message"],
"query": {
"match": {
"_id" : "x5pcgHkBlRaz9flxW1uw"
}
}
}
这将 return CLI 中的以下内容:
{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "elastic-search-app-logs-development-2021-05",
"_type" : "_doc",
"_id" : "x5pcgHkBlRaz9flxW1uw",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2021-05-18T11:43:25.6155311-05:00",
"level" : "Error",
"message" : "Something bad happened",
"messageTemplate" : "Something bad happened"
}
}
]
}
}
这是我尝试做的,但在构建时出现错误:
[HttpGet("{_id}")]
public async Task<EsSource> Get(String _id)
{
var response = await _elasticClient.Search<EsSource>(s => s
.Source(src => src.Includes(i => i
.Fields(f => f.TimeStamp,
f => f.Level,
f => f.MessageTemplate,
f => f.Message)))
.Index("elastic-search-app-logs*")
.Query(q => q
.Match(m => m.Field("_id").Query(_id))));
return response?.Documents?.FirstOrDefault();
}
我的 EsSource 模型 class:
public class EsSource
{
public String TimeStamp { get; set; }
public String Level { get; set; }
public String MessageTemplate { get; set; }
public String Message { get; set; }
}
VS19 中的错误说:
ESController.cs(39,28,47,61): error CS1061: 'ISearchResponse<EsSource>' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'ISearchResponse<EsSource>' could be found (are you missing a using directive or an assembly reference?)
我没有正确地将 DSL 查询转换为 NEST 吗?我对此非常陌生,希望能得到所有帮助。
您需要将 Search
方法更改为 SearchAsync
,因为 Search
中的 return 类型是 ISearchResponse<EsSource>
,并且不包含 GetAwaiter
所以你不能 await
它。
[HttpGet("{_id}")]
public async Task<EsSource> Get(String _id)
{
var response = await _elasticClient.SearchAsync<EsSource>(s => s
.Source(src => src.Includes(i => i
.Fields(f => f.TimeStamp,
f => f.Level,
f => f.MessageTemplate,
f => f.Message)))
.Index("elastic-search-app-logs*")
.Query(q => q
.Match(m => m.Field("_id").Query(_id))));
return response?.Documents?.FirstOrDefault();
}
我在 Kibana 中的 DSL 查询是:
GET elastic-search-app-log*/_search
{
"_source": ["@timestamp", "level", "messageTemplate", "message"],
"query": {
"match": {
"_id" : "x5pcgHkBlRaz9flxW1uw"
}
}
}
这将 return CLI 中的以下内容:
{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "elastic-search-app-logs-development-2021-05",
"_type" : "_doc",
"_id" : "x5pcgHkBlRaz9flxW1uw",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2021-05-18T11:43:25.6155311-05:00",
"level" : "Error",
"message" : "Something bad happened",
"messageTemplate" : "Something bad happened"
}
}
]
}
}
这是我尝试做的,但在构建时出现错误:
[HttpGet("{_id}")]
public async Task<EsSource> Get(String _id)
{
var response = await _elasticClient.Search<EsSource>(s => s
.Source(src => src.Includes(i => i
.Fields(f => f.TimeStamp,
f => f.Level,
f => f.MessageTemplate,
f => f.Message)))
.Index("elastic-search-app-logs*")
.Query(q => q
.Match(m => m.Field("_id").Query(_id))));
return response?.Documents?.FirstOrDefault();
}
我的 EsSource 模型 class:
public class EsSource
{
public String TimeStamp { get; set; }
public String Level { get; set; }
public String MessageTemplate { get; set; }
public String Message { get; set; }
}
VS19 中的错误说:
ESController.cs(39,28,47,61): error CS1061: 'ISearchResponse<EsSource>' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'ISearchResponse<EsSource>' could be found (are you missing a using directive or an assembly reference?)
我没有正确地将 DSL 查询转换为 NEST 吗?我对此非常陌生,希望能得到所有帮助。
您需要将 Search
方法更改为 SearchAsync
,因为 Search
中的 return 类型是 ISearchResponse<EsSource>
,并且不包含 GetAwaiter
所以你不能 await
它。
[HttpGet("{_id}")]
public async Task<EsSource> Get(String _id)
{
var response = await _elasticClient.SearchAsync<EsSource>(s => s
.Source(src => src.Includes(i => i
.Fields(f => f.TimeStamp,
f => f.Level,
f => f.MessageTemplate,
f => f.Message)))
.Index("elastic-search-app-logs*")
.Query(q => q
.Match(m => m.Field("_id").Query(_id))));
return response?.Documents?.FirstOrDefault();
}