This question already has answers here:
is there a way to deserialize Elasticsearch Nest search query?
(5个答案)
2年前关闭。
在此处的文档https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/writing-queries.html
它给出了一个示例,说明使用Fluent API进行此C#查询
将转换为以下json查询:
一切都很好,但是我想自己在代码中看到它。所以现在我的问题是-是否可以在我的C#代码中获取JSON字符串?
我需要它是因为,当我进行一些更复杂的查询(例如大聚合)时,我想看看NEST如何将C#查询转换为JSON。
然后,我可以看到它在JSON中的外观,并查看它是否将其转换为我期望的JSON。
请记住在ConnectionSettings中设置 .DisableDirectStreaming()
(5个答案)
2年前关闭。
在此处的文档https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/writing-queries.html
它给出了一个示例,说明使用Fluent API进行此C#查询
var searchResponse = client.Search<Project>(s => s
.Query(q => q
.MatchAll()
));
将转换为以下json查询:
{
"query": {
"match_all": {}
}
}
一切都很好,但是我想自己在代码中看到它。所以现在我的问题是-是否可以在我的C#代码中获取JSON字符串?
我需要它是因为,当我进行一些更复杂的查询(例如大聚合)时,我想看看NEST如何将C#查询转换为JSON。
然后,我可以看到它在JSON中的外观,并查看它是否将其转换为我期望的JSON。
最佳答案
我才发现自己。这是代码:
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.DisableDirectStreaming()
.DefaultIndex("sales");
var client = new ElasticClient(settings);
var searchResponse = await client.SearchAsync<Sales>(x => x
.Size(0)
.MatchAll()
);
if (searchResponse.ApiCall.ResponseBodyInBytes != null)
{
var requestJson = System.Text.Encoding.UTF8.GetString(searchResponse.ApiCall.RequestBodyInBytes);
Console.WriteLine(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(requestJson), Formatting.Indented));
}
请记住在ConnectionSettings中设置 .DisableDirectStreaming()