C#使用Elasticsearch获得原始查询(NEST)
在OnRequestCompleted
方法中可以获得原始查询和查询结果
关键代码如下
ElasticClient elasticClient = new ElasticClient(new ConnectionSettings(new Uri(address))
//打印请求、回复,可能影响性能
.DisableDirectStreaming()
.OnRequestCompleted(apiCallDetails =>
{
if (apiCallDetails.Success)
{
#if DEBUG
string infos = GetInfosFromApiCallDetails(apiCallDetails);
Console.WriteLine(infos);
#endif
}
else
{
string infos = GetInfosFromApiCallDetails(apiCallDetails);
Console.WriteLine(infos);
}
}));
private string GetInfosFromApiCallDetails(IApiCallDetails r)
{
string infos = "";
infos += $"Uri:\n{r.Uri}\n";
infos += $"Success:\n{r.Success}\n";
infos += $"SuccessOrKnownError:\n{r.SuccessOrKnownError}\n";
infos += $"HttpMethod:\n{r.HttpMethod}\n";
infos += $"HttpStatusCode:\n{r.HttpStatusCode}\n";
infos += $"DebugInformation:\n{r.DebugInformation}\n";
foreach (var deprecationWarning in r.DeprecationWarnings)
infos += $"DeprecationWarnings:\n{deprecationWarning}\n";
if (r.OriginalException != null)
{
infos += $"OriginalException.GetMessage:\n{r.OriginalException.Message}\n";
infos += $"OriginalException.GetStackTrace:\n{r.OriginalException.Message}\n";
}
if (r.RequestBodyInBytes != null)
infos += $"RequestBody:\n{Encoding.UTF8.GetString(r.RequestBodyInBytes)}\n";
if (r.ResponseBodyInBytes != null)
infos += $"ResponseBody:\n{Encoding.UTF8.GetString(r.ResponseBodyInBytes)}\n";
infos += $"ResponseMimeType:\n{r.ResponseMimeType}\n";
return infos;
}
输出结果如下
Uri:
http://xxxxxx:9200/stuAliasName/_search?typed_keys=true
Success:
True
SuccessOrKnownError:
True
HttpMethod:
POST
HttpStatusCode:
200
DebugInformation:
Successful (200) low level call on POST: /stuAliasName/_search?typed_keys=true
# Audit trail of this API call:
- [1] ProductCheckOnStartup: Took: 00:00:00.6610536
- [2] ProductCheckSuccess: Node: http://xxxxxx:9200/ Took: 00:00:00.6202894
- [3] HealthyResponse: Node: http://xxxxxx:9200/ Took: 00:00:01.4509449
# Request:
{"from":0,"query":{"bool":{"must":[{"bool":{"should":[{"wildcard":{"name":{"value":"Student1*"}}}]}}]}},"size":10}
# Response:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"stu","_type":"_doc","_id":"1","_score":1.0,"_source":{"id":1,"name":"Student1","email":"11111@qq.com"}}]}}
RequestBody:
{"from":0,"query":{"bool":{"must":[{"bool":{"should":[{"wildcard":{"name":{"value":"Student1*"}}}]}}]}},"size":10}
ResponseBody:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"stu","_type":"_doc","_id":"1","_score":1.0,"_source":{"id":1,"name":"Student1","email":"11111@qq.com"}}]}}
ResponseMimeType:
application/json; charset=UTF-8
根据Get raw query from NEST client,也可以直接序列化
var json = elasticClient.RequestResponseSerializer.SerializeToString(request);
示例代码
参考资料
学习技术最好的文档就是【官方文档】,没有之一。
还有学习资料【Microsoft Learn】、【CSharp Learn】、【My Note】。
如果,你认为阅读这篇博客让你有些收获,不妨点击一下右下角的【推荐】按钮。
如果,你希望更容易地发现我的新博客,不妨点击一下【关注】。