0
votes

I am using Nest 6.2 with ES 6.6

I have the following code which runs fine:

        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;

But I know there is an issue with the underlying query, because all documents from the index are returned. So I would like to be able to see the raw Json that is generated by the lambda expression so I can see the result running in the Head plugin or Fiddler etc.

If I use a SearchRequest Object and pass that to the Search method, would I then be able to see the Query Json?

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

I am having trouble building the corresponding query using the SearchRequest approach and cannot find decent examples showing how to do so.

Anyone know?

1

1 Answers

4
votes

You can get the JSON for any NEST request using SerializeToString extension method

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);

which yields

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