我想捕获发送到Elastic的查询,我正在使用Nest 7.6.1。
我如何查看请求的查询。这是代码:

ISearchResponse<Document> initialResponse = this.clientBundle.GetClient().Search<Document>
                (
                    scr => scr
                     .From(0)
                     .Take(scrollSize)
                     .Query(q =>
                                q.Bool(y => boolQuery)
                            )
                     .Scroll(scrollTimeout));

最佳答案

默认情况下,NEST直接序列化到请求流,并从响应流反序列化,但这可以全局禁用或基于每个请求禁用,以便将它们缓冲在内存中,并可以在响应的API调用详细信息中使用

var client = new ElasticClient(settings);

var searchResponse = client.Search<object>(s => s
    .RequestConfiguration(r => r
        .DisableDirectStreaming()
    )
);

var requestJson = Encoding.UTF8.GetString(searchResponse.ApiCall.RequestBodyInBytes);

如果要查看所有请求和响应,则可能需要在DisableDirectStreaming(...)上设置ConnectionSettings并配置使用 OnRequestCompleted(...) to print these out的委托(delegate)
var defaultIndex = "default_index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

var settings = new ConnectionSettings(pool)
    .DefaultIndex(defaultIndex)
    .PrettyJson()
    .OnRequestCompleted(callDetails =>
    {
        if (callDetails.RequestBodyInBytes != null)
        {
            Console.WriteLine(
                $"{callDetails.HttpMethod} {callDetails.Uri} \n" +
                $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
        }
        else
        {
            Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
        }

        Console.WriteLine();

        if (callDetails.ResponseBodyInBytes != null)
        {
            Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                     $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
                     $"{new string('-', 30)}\n");
        }
        else
        {
            Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                     $"{new string('-', 30)}\n");
        }
    });

var client = new ElasticClient(settings);

var searchResponse = client.Search<object>();
本质上,这就是.EnableDebugMode()上的ConnectionSettings所做的。

如果您希望能够在不发出实际请求的情况下看到请求,则可以使用RequestResponseSerializer序列化请求
var client = new ElasticClient(settings);

ISearchRequest searchRequest = new SearchDescriptor<object>()
    .Query(q => q
        .Match(m => m
            .Field("foo")
            .Query("bar")
        )
    );

var json = client.RequestResponseSerializer.SerializeToString(searchRequest);
使用这种方法,应将请求转换为请求接口(interface)以正确序列化,例如SearchDescriptor<T>-> ISearchRequest
另一种方法是使用InMemoryConnection
var defaultIndex = "default_index";
var settings = new ConnectionSettings(new InMemoryConnection())
    .DefaultIndex(defaultIndex)
    .DisableDirectStreaming();

var client = new ElasticClient(settings);

var response = client.Search<object>(s => s
    .Query(q => q
        .Match(m => m
            .Field("foo")
            .Query("bar")
        )
    )
);

var json = Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
和以前一样,OnRequestCompleted(...)可以与这种方法一起使用来捕获所有请求。

09-10 15:41