【问题标题】:How Do I View the REST JSON created by the NEST API?如何查看 NEST API 创建的 REST JSON?
【发布时间】:2016-07-11 03:07:21
【问题描述】:

我正在尝试在 NEST API(或在使用 NEST 时通过底层 ElasticSearch.NET API)中找到可以看到由 NEST 查询创建的 JSON 的位置。这是我在部分代码示例中总结的问题 -

using NEST;

// create any query 
var elasticResponse = elasticClient.Search<Hotel>(s => s
   .Query(q => // ... build up the rest of my query ... 

elasticResponse.ApiCall.HttpMethod // is "POST" - good! 
elasticResponse.ApiCall.HttpStatusCode // is 200 (OKAY) - good! 
elasticResponse.ApiCall.Success // is true - good! 

// ISSUE: 
// I've tried viewing the generated JSON through these members (in no particular order) without success ... 

elasticResponse.ApiCall.RequestBodyInBytes // is always empty before and after use of the elasticResponse object. 

elasticResponse.ApiCall.DebugInformation // doesn't provide useful info about body. 

elasticResponse.CallDetails.RequestBodyInBytes // is always null.

我正在使用 Elastic Search 2.3.1 和 NEST 2.4.1。

我希望看到 POST 数据或生成的查询的地方没有它;它们要么为空要么为 null,并且由于查询成功,因此没有显示错误条件。

在构建查询时,我还查看了查询描述符的可用成员,并认为我可能能够在完成查询后立即捕获 RESTful 调用主体,但还没有发现任何有用的东西。

ConnectionSettings 出现 .OnRequestCompletedcallback 但这似乎也没有显示查询。

我浏览了官方的documentation,但无济于事。寻求帮助。谢谢你。

【问题讨论】:

标签: elasticsearch nest


【解决方案1】:

我通过使用配置设置找到了一个答案 -

使用.DisableDirectStreaming(true);ConnectionSettings(instance) Disable Direct Streaming 属性设置为true,并将其传递给您的新Elastic NEST 客户端对象。这将告诉 Elastic 缓冲请求和响应,并导致这两个值分别在 .RequestBodyInBytes.ResponseBodyInBytes 属性上可用。

然后您可以使用 .NET 对字节进行解码,例如

global::System.Text.UTF8Encoding.Default.GetString(elasticResponse.ApiCall.RequestBodyInBytes);

并查看原始查询,例如在我的情况下 -

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "shortDescription": {
                    "value": "mile"
                  }
                }
              },
              {
                "term": {
                  "shortDescription": {
                    "value": "museum"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "name": {
                    "boost": 1.1,
                    "value": "pacific"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【讨论】:

  • 附加说明 - 此设置在低级客户端上表示为 Elasticsearch.Net.ElasticLowLevelClient instance .Settings.DisableDirectStreaming(true);
猜你喜欢
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多