Sahil Sharma
Sahil Sharma

Reputation: 4227

c# nest: how to dynamically append elasticsearch search queries to multi search nest query?

Currently, we have fixed 3 search queries in our multi-search query. The code looks like:

var results = elasticClient.MultiSearch(a => a
                           .Search<StockBaseEntity>(s => s
                           .Type("<docType>")
                           .Index(<indexName>)
                           .Take(<count>)
                           .Query(qq => qq
                           ...
                           .Search<StockBaseEntity>(s => s
                           .Type("<docType>")
                           .Index(<indexName>)
                           .Take(<count>)
                           .Query(qq => qq
                           ....
                           .Search<StockBaseEntity>(s => s
                           .Type("<docType>")
                           .Index(<indexName>)
                           .Take(<count>)
                           .Query(qq => qq
                           ....

All three search queries have some different query parameters, for example, first query returns "type1" doc, second & third return "type2 and type3" docs respectively.

We want to build this multi search nest query in such a way that we can have any number of search nest queries in multi searach query (and not only 3). It could be 3/4/5 or any number of search queries based on some condition. This can be achived if we can append search queries to multisearch? can we do this?

I read this article but can't get same for nest version 5.X and I have no clue how to write query with QueryContainer?

Upvotes: 0

Views: 1424

Answers (1)

Rob
Rob

Reputation: 9979

One option is to use MultiSearchRequest and combine it with your search descriptors.

var multiSearchRequest = new MultiSearchRequest{};

multiSearchRequest.Operations = new Dictionary<string, ISearchRequest>();
multiSearchRequest.Operations["search1"] = new SearchDescriptor<object>().Query(q => q.MatchAll());
multiSearchRequest.Operations["search2"] = new SearchDescriptor<object>().Query(q => q.MatchAll());
multiSearchRequest.Operations["search3"] = new SearchDescriptor<object>().Query(q => q.MatchAll());

elasticClient.MultiSearch(multiSearchRequest);

Hope that helps.

Upvotes: 2

Related Questions