ElasticSearch, NEST, C# - How to set rest_total_hits_as_int request parameter?

1k Views Asked by At

I'm working on updating code to handle an update from ElasticSearch 6 to 7. This version changes how the total number of hits works (documentation). I'm new to using this library and am having difficulty seeing how I can use the NEST API to set rest_total_hits_as_int as a request parameter.

I see that the selector parameter has a RequestConfiguration method. But, the RequestConfigurationDescriptor class makes no mention of this request parameter.

I have searched PRs in the ElasticSearch.Net GitHub repo for rest_total_hits_as_int and TotalHits (hoping to find a commit where the option to specify this request parameter was added), but I did not find anything helpful.

Can anyone provide assistance on how to set this request parameter in C#?

1

There are 1 best solutions below

0
On BEST ANSWER

It's available on the search API in NEST 7.x

var client = new ElasticClient();

var response = client.Search<object>(s => s
    .TotalHitsAsInteger(true)
);

or

var client = new ElasticClient();
var request = new SearchRequest<object>()
{
    TotalHitsAsInteger = true
};

var response = client.Search<object>(request);

Both produce

POST http://localhost:9200/<default_index>/_search?typed_keys=true&rest_total_hits_as_int=true 
{}

One thing to note is that rest_total_hits_as_int doesn't really make any difference to the high level client - It controls the JSON structure for how total hits is returned in the response, but NEST 7.x deserializes both JSON number and JSON object to the same type. NEST 7.x must be used with Elasticsearch 7.x.