Get raw query from NEST client

前端 未结 8 705
太阳男子
太阳男子 2021-01-30 08:33

Is it possible to get the raw search query from the NEST client?

var result = client.Search(s => s
             


        
相关标签:
8条回答
  • 2021-01-30 08:52

    Use result.ConnectionStatus.Request.

    0 讨论(0)
  • 2021-01-30 08:58

    For NEST / Elasticsearch.NET v6.0.2, use the ApiCall property of the IResponse object. You can write a handy extension method like this:

    public static string ToJson(this IResponse response)
    {
        return Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
    }
    

    Or, if you want to log all requests made to Elastic, you can intercept responses with the connection object:

    var node = new Uri("https://localhost:9200");
    var pool = new SingleNodeConnectionPool(node);
    var connectionSettings = new ConnectionSettings(pool, new HttpConnection());
    connectionSettings.OnRequestCompleted(call =>
    {
        Debug.Write(Encoding.UTF8.GetString(call.RequestBodyInBytes));
    });
    
    0 讨论(0)
  • 2021-01-30 08:59

    Before making Request, from Nest Query - For Nest 5.3.0 :

    var stream = new System.IO.MemoryStream();
    elasticClient.Serializer.Serialize(query, stream );
    var jsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());
    
    0 讨论(0)
  • 2021-01-30 09:03

    In ElasticSearch 5.x, the RequestInformation.Request property does not exist in ISearchResponse<T>, but similar to the answer provided here you can generate the raw query JSON using the Elastic Client Serializer and a SearchDescriptor. For example, for the given NEST search query:

    var results = elasticClient.Search<User>(s => s
        .Index("user")
        .Query(q => q                    
            .Exists(e => e
                .Field("location")
            )
        )            
    );
    

    You can get the raw query JSON as follows:

    SearchDescriptor<User> debugQuery = new SearchDescriptor<User>()
        .Index("user")
        .Query(q => q                    
            .Exists(e => e
                .Field("location")
            )
        )
    ;
    
    using (MemoryStream mStream = new MemoryStream())
    {
        elasticClient.Serializer.Serialize(debugQuery, mStream);
        string rawQueryText = Encoding.ASCII.GetString(mStream.ToArray());
    }
    
    0 讨论(0)
  • 2021-01-30 09:04

    You can get raw query json from RequestInformation:

    var rawQuery = Encoding.UTF8.GetString(result.RequestInformation.Request);
    

    Or enable trace on your ConnectionSettings object, so NEST will print every request to trace output

    var connectionSettings = new ConnectionSettings(new Uri(elasticsearchUrl));
    connectionSettings.EnableTrace(true);
    var client = new ElasticClient(connectionSettings); 
    

    NEST 7.x

    Enable debug mode when creating settings for a client:

    var settings = new ConnectionSettings(connectionPool)
        .DefaultIndex("index_name")
        .EnableDebugMode()
    var client = new ElasticClient(settings); 
    

    then your response.DebugInformation will contain information about request sent to elasticsearch and response from elasticsearch. Docs.

    0 讨论(0)
  • 2021-01-30 09:04

    How about using Fiddler ?! :)

    0 讨论(0)
提交回复
热议问题