Reputation: 182
Is there a means to reuse a fluent query for both a Client.Search and a Client.Count in ElasticSearch via Nest?
Here is the start of a snippet defining a search fluent definition.
System.Func<SearchDescriptor<Documents.City>, ISearchRequest> x = s =>
s.Index(IndexNames.Cities).From(0)
.Size(100)
.Query(q => q.Bool(.....
The change for count would be SearchDescriptor -> CountDescriptor and ISearchRequest -> ICountRequest. It appears that the query needs to be written twice as the fluent markup will not compile without upfront knowledge of the types at play.
The essential question is, are there any neat maintainable methods for using a Nest query to execute both Search and Count requests?
Upvotes: 6
Views: 3027
Reputation: 9979
Instead of reusing search part, try to do it with query:
Func<QueryContainerDescriptor<T>, QueryContainer> query =
q => q.MatchAll();
var searchResponse = await ElasticClient().SearchAsync<T>(s => s
.Query(query));
var countResponse = await ElasticClient().CountAsync<T>(s => s.Query(query));
Hope that helps.
Upvotes: 8