c# - 如何建立Nest SearchRequest对象并在查询中查看原始JSON?

标签 c# elasticsearch nest

我正在将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方法构建相应的查询时遇到了麻烦,无法找到说明如何执行此操作的示例。

有人知道吗

最佳答案

您可以使用 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
}

关于c# - 如何建立Nest SearchRequest对象并在查询中查看原始JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54935505/

相关文章:

c# - Metro 风格应用程序的发布问题

elasticsearch - Elasticsearch没有获得足够的文件描述符

c# - Elasticsearch 查询第一次慢

c# - 如何以编程方式添加契约(Contract)属性?

c# - 如何将 Web API 添加到现有的 ASP.NET MVC (5) Web 应用程序项目?

c# - 谁负责在传出消息的 SOAP header 中设置 To WS(Ws-Addressing) 命名空间?

elasticsearch 使用 aggs 过滤数组数据

elasticsearch - ElasticSearch中的“关联”数据

elasticsearch - 如何从弹性嵌套中排除某些值

ElasticSearch Nest 插入/更新