如何构建 Nest SearchRequest 对象并在查询中查看原始 JSON?
How do you build a Nest SearchRequest object and see the raw JSON in query?
我正在使用带有 ES 6.6 的 Nest 6.2
我有以下运行良好的代码:
var response = elasticClient.Search<PropertyRecord>(s => s
.Query(q => q.Terms(
c => c
.Field(f => f.property_id_orig)
.Terms(listOfPropertyIds) // a list of 20 ids say...
))
.From(0)
.Take(100) // just pull up to a 100...
);
if (!response.IsValid)
throw new Exception(response.ServerError.Error.Reason);
return response.Documents;
但我知道底层查询存在问题,因为索引中的所有文档都已返回。所以我希望能够看到由 lambda 表达式生成的原始 Json,这样我就可以在 Head 插件或 Fiddler 等中看到结果 运行
如果我使用 SearchRequest 对象并将其传递给 Search 方法,那么我是否能够看到查询 Json?
var request = new SearchRequest
{
// and build equivalent query here
};
我在使用 SearchRequest 方法构建相应的查询时遇到问题,并且找不到说明如何执行此操作的合适示例。
有人知道吗?
您可以使用 SerializeToString
extension method
为 任何 NEST 请求获取 JSON
var client = new ElasticClient();
var listOfPropertyIds = new [] { 1, 2, 3 };
// pull the descriptor out of the client API call
var searchDescriptor = new SearchDescriptor<PropertyRecord>()
.Query(q => q.Terms(
c => c
.Field(f => f.property_id_orig)
.Terms(listOfPropertyIds) // a list of 20 ids say...
))
.From(0)
.Take(100);
var json = client.RequestResponseSerializer.SerializeToString(searchDescriptor, SerializationFormatting.Indented);
Console.WriteLine(json);
产生
{
"from": 0,
"query": {
"terms": {
"property_id_orig": [
1,
2,
3
]
}
},
"size": 100
}
我正在使用带有 ES 6.6 的 Nest 6.2
我有以下运行良好的代码:
var response = elasticClient.Search<PropertyRecord>(s => s
.Query(q => q.Terms(
c => c
.Field(f => f.property_id_orig)
.Terms(listOfPropertyIds) // a list of 20 ids say...
))
.From(0)
.Take(100) // just pull up to a 100...
);
if (!response.IsValid)
throw new Exception(response.ServerError.Error.Reason);
return response.Documents;
但我知道底层查询存在问题,因为索引中的所有文档都已返回。所以我希望能够看到由 lambda 表达式生成的原始 Json,这样我就可以在 Head 插件或 Fiddler 等中看到结果 运行
如果我使用 SearchRequest 对象并将其传递给 Search 方法,那么我是否能够看到查询 Json?
var request = new SearchRequest
{
// and build equivalent query here
};
我在使用 SearchRequest 方法构建相应的查询时遇到问题,并且找不到说明如何执行此操作的合适示例。
有人知道吗?
您可以使用 SerializeToString
extension method
var client = new ElasticClient();
var listOfPropertyIds = new [] { 1, 2, 3 };
// pull the descriptor out of the client API call
var searchDescriptor = new SearchDescriptor<PropertyRecord>()
.Query(q => q.Terms(
c => c
.Field(f => f.property_id_orig)
.Terms(listOfPropertyIds) // a list of 20 ids say...
))
.From(0)
.Take(100);
var json = client.RequestResponseSerializer.SerializeToString(searchDescriptor, SerializationFormatting.Indented);
Console.WriteLine(json);
产生
{
"from": 0,
"query": {
"terms": {
"property_id_orig": [
1,
2,
3
]
}
},
"size": 100
}