【问题标题】:How do you build a Nest SearchRequest object and see the raw JSON in query?如何构建 Nest SearchRequest 对象并在查询中查看原始 JSON?
【发布时间】:2019-03-01 08:19:16
【问题描述】:

我正在使用 Nest 6.2 和 ES 6.6

我有以下运行良好的代码:

        var response = elasticClient.Search<PropertyRecord>(s => s
            .Query(q => q.Terms(
                c => c
                    .Field(f => f.property_id_orig)
                    .Terms(listOfPropertyIds) // a list of 20 ids say... 
            ))
            .From(0)
            .Take(100) // just pull up to a 100... 
        );


        if (!response.IsValid)
            throw new Exception(response.ServerError.Error.Reason);

        return response.Documents;

但我知道底层查询存在问题,因为返回了索引中的所有文档。所以我希望能够看到由 lambda 表达式生成的原始 Json,这样我就可以看到在 Head 插件或 Fiddler 等中运行的结果。

如果我使用 SearchRequest 对象并将其传递给 Search 方法,那么我是否能够看到 Query Json?

        var request = new SearchRequest
        {
            // and build equivalent query here
        };

我在使用 SearchRequest 方法构建相应查询时遇到了问题,并且找不到合适的示例来说明如何做到这一点。

有人知道吗?

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    您可以使用SerializeToString extension method 获取任何 NEST 请求的 JSON

    var client = new ElasticClient();
    
    var listOfPropertyIds = new [] { 1, 2, 3 };
    
    // pull the descriptor out of the client API call
    var searchDescriptor = new SearchDescriptor<PropertyRecord>()
        .Query(q => q.Terms(
            c => c
                .Field(f => f.property_id_orig)
                .Terms(listOfPropertyIds) // a list of 20 ids say... 
        ))
        .From(0)
        .Take(100);
    
    var json = client.RequestResponseSerializer.SerializeToString(searchDescriptor, SerializationFormatting.Indented);
    
    Console.WriteLine(json);
    

    产生

    {
      "from": 0,
      "query": {
        "terms": {
          "property_id_orig": [
            1,
            2,
            3
          ]
        }
      },
      "size": 100
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      相关资源
      最近更新 更多