serialize query from Nest client elastic search 2.3

隐身守侯 提交于 2019-12-03 19:32:41

问题


Since upgrading my Nest client to 2.2.1 I'm unable see the query I'm submitting to my elastic search client (now version 2.3.0). I used to use this line:

string searchJson = Encoding.UTF8.GetString(client.Serializer.Serialize(myQueryHere));  

But this method now returns void instead of the JSON it used to. ConnectionStatus also doesn't exist so I can no longer see the json i'm sending, does anyone know of a way? CallDetails.RequestBodyInBytes is available but that returns null.


回答1:


Take a look at the documentation for NEST 2.x on Connecting. CallDetails.RequestBodyInBytes will be null unless you set .DisableDirectStreaming() on ConnectionSettings that is passed to the constructor of ElasticClient

var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));  
var settings = new ConnectionSettings(connectionPool) 
    .DisableDirectStreaming();

var client = new ElasticClient(settings);

now a copy of the request and response bytes will be exposed on the response CallDetails

var response = client.Search<Document>();

var requestJson = Encoding.UTF8.GetString(response.CallDetails.RequestBodyInBytes);
var responseJson = Encoding.UTF8.GetString(response.CallDetails.ResponseBodyInBytes);

Whilst developing it may be useful to log out all requests and responses.




回答2:


Serialize method now requires stream, on which it will write raw json query - Working properly For Nest 5.3.0 :

        var stream = new System.IO.MemoryStream();
        nestClient.Serializer.Serialize(query, stream);
        var jsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());


来源:https://stackoverflow.com/questions/36551724/serialize-query-from-nest-client-elastic-search-2-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!