Ragmar
Ragmar

Reputation: 350

Change ElasticSearch track_total_hits in NEST

I was running thought examples of ElasticSearch, and read this link that says that there is a default set at 10,000, which also can be changed on the search calls, like on this example

GET twitter/_search
{
    "track_total_hits": 100,
     "query": {
        "match" : {
            "message" : "Elasticsearch"
        }
     }
}

The problem is, I'm trying to do the same on NEST, but I don't manage to replicate it. The only thing similar that I found, only accept a Boolean value and not a number. It is possible to change the total through NEST?

Here is the code that I tried:

var results = elasticClient.Search<MyClass>(s => s
             .Query(q => q.QueryString(q2 => q2.Query(readLine)
             .Fields(f => f.Field(p => p.MyField)))).TrackTotalHits(true));

Upvotes: 4

Views: 1592

Answers (1)

Andrey Borisko
Andrey Borisko

Reputation: 4609

As stated by @russcam here at the moment you can do it via casting ISearchRequest to IRequest<SearchRequestParameters>:

var client = new ElasticClient();

var searchResponse = client.Search<Document>(s =>
{
    IRequest<SearchRequestParameters> request = s;
    request.RequestParameters.SetQueryString("track_total_hits", 1000);
    
    return s;
});

It will apply it as querystring parameter

Upvotes: 3

Related Questions