How do you debug your Nest queries?

前端 未结 8 1911
失恋的感觉
失恋的感觉 2021-02-05 14:02

I\'m new to Nest, and I very likely am not creating my query like I think I am. My question is more along the lines of teach a man to fish rather than give me a fish. However,

8条回答
  •  清酒与你
    2021-02-05 14:36

    NEST is Baroque of .NET APIs. For 2.1+ on call level:

    IElasticClient client = new ElasticClient();
    var searchDescriptor = new SearchDescriptor();
    var query = Query.Term(...);
    var pretty = query.ToPrettyString(query);
    var json = client.ToRawRequest(searchDescriptor.Query(descriptor => query));
    

    On configuration level:

        var settings = new ConnectionSettings()
                         .PrettyJson().DisableDirectStreaming()
                         .OnRequestCompleted(details=> Debug.WriteLine(Encoding.UTF8.GetString(details.RequestBodyInBytes)));
    

    On response level look into CallDetails.RequestBodyInBytes.

    Used extensions:

        /// 
        /// Converts search to raw JSON request for debugging.
        /// 
        /// The type.
        /// The self.
        /// The search descriptor.
        /// The string.
        public static string ToRawRequest(this IElasticClient self, SearchDescriptor searchDescriptor) where T : class
        {
            using (var output = new MemoryStream())
            {
                self.Serializer.Serialize(searchDescriptor, output);
                output.Position = 0;
                var rawQuery = new StreamReader(output).ReadToEnd();
                return rawQuery;
            }
        }
    
        /// 
        /// Prints query into string.
        /// 
        /// The self.
        /// The value.
        public static string ToPrettyString(this QueryContainer self)
        {
            using (var settings = new ConnectionSettings())
            {
                var visitor = new DslPrettyPrintVisitor(settings);
                self.Accept(visitor);
                return visitor.PrettyPrint.Replace(Environment.NewLine, string.Empty);
            }                                                                         
        }
    

提交回复
热议问题