How do you debug your Nest queries?

前端 未结 8 1912
失恋的感觉
失恋的感觉 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:23

    Using the latest elastic search 5+, I was able to get mine (thanks to Adrian Carr's method) with the following:

    var jsonOutput = System.Text.Encoding.UTF8.GetString(
        response.ApiCall.RequestBodyInBytes
    )
    

    Which gave me the following output:

    {
       "from":0,
       "size":0,
       "query":{
          "bool":{
          ...
          }
       }
    }
    
    0 讨论(0)
  • 2021-02-05 14:25

    You can use EnableTrace or ConnectionStatusHandler. More details here.

    0 讨论(0)
  • 2021-02-05 14:26

    Elasticsearch.Net and NEST: the .NET clients [7.x]

    ref: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/debug-information.html`

    1. Enable DisableDirectStreaming in ES Global setting like following.

    ref code

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

    2.) var response = client.Search<Project>(s =>....)

    3.)

    var jsonOutput = System.Text.Encoding.UTF8.GetString(
                result.ApiCall.RequestBodyInBytes);
    

    Above can be achieved on a per request basis with following.

    var response = client.Search<Project>(s => s
        .RequestConfiguration(r => r
            .DisableDirectStreaming() 
        )
        .Query(q => q
            .MatchAll()
        )
    );
    
    0 讨论(0)
  • 2021-02-05 14:27

    Really easily. If this is my code that searches:

    var results = client.Search<SearchItem>(s => s.AllIndices()
        .Query(q =>
                q.Term(p => p.LastName, searchItem.LastName)
                && q.Term(p => p.FirstName, searchItem.FirstName)
                && q.Term(p => p.ApplicationCode, searchItem.ApplicationCode)
                )
        .Size(1000)
        );
    var list = results.Documents.ToList();
    

    I then set a breakpoint on the line above. Then, in Visual Studio Immediate Window, I enter this:

    ?results.ConnectionStatus
    

    and it gives me this:

    {StatusCode: 200, 
        Method: POST, 
        Url: http://localhost:9200/_all/searchitem/_search, 
        Request: {
      "size": 1000,
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "lastName": {
                  "value": "carr"
                }
              }
            },
            {
              "term": {
                "firstName": {
                  "value": "adrian"
                }
              }
            }
          ]
        }
      }
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-05 14:28

    You can get the values of search request URL and JSON request body as under:

    var requestURL = response.RequestInformation.RequestUrl;
    var jsonBody = Encoding.UTF8.GetString(response.RequestInformation.Request);
    

    You can find other useful properties in RequestInformation for debugging.

    0 讨论(0)
  • 2021-02-05 14:29

    Tested in Nest 6.2.0

    You can now just do:

    #if DEBUG
      connectionSettings.EnableDebugMode(details =>
      {
        Logger.Debug($"ES Request: {Encoding.UTF8.GetString(details.RequestBodyInBytes ?? new byte[0])}");
        Logger.Verbose($"ES Response: {Encoding.UTF8.GetString(details.ResponseBodyInBytes ?? new byte[0])}");
      });
    #endif
    

    The call EnableDebugMode will automatically call DisableDirectStreaming, PrettyJson, and then OnRequestCompleted, passing in the function you gave it.

    NOTE: There may be a bug somewhere in their response stuff. The response seems to be lacking some closing brackets for some reason.

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