Get raw query from NEST client

前端 未结 8 798
太阳男子
太阳男子 2021-01-30 08:33

Is it possible to get the raw search query from the NEST client?

var result = client.Search(s => s
             


        
8条回答
  •  攒了一身酷
    2021-01-30 09:03

    In ElasticSearch 5.x, the RequestInformation.Request property does not exist in ISearchResponse, but similar to the answer provided here you can generate the raw query JSON using the Elastic Client Serializer and a SearchDescriptor. For example, for the given NEST search query:

    var results = elasticClient.Search(s => s
        .Index("user")
        .Query(q => q                    
            .Exists(e => e
                .Field("location")
            )
        )            
    );
    

    You can get the raw query JSON as follows:

    SearchDescriptor debugQuery = new SearchDescriptor()
        .Index("user")
        .Query(q => q                    
            .Exists(e => e
                .Field("location")
            )
        )
    ;
    
    using (MemoryStream mStream = new MemoryStream())
    {
        elasticClient.Serializer.Serialize(debugQuery, mStream);
        string rawQueryText = Encoding.ASCII.GetString(mStream.ToArray());
    }
    

提交回复
热议问题