Ioannis Dontas
Ioannis Dontas

Reputation: 659

ElasticSearch Nest Client initialize results var

I am using the Nest client to query ElasticSearch and according to a parameter I create different queries using a switch statement

switch (parameter)
{
    case 1:
    var results = this.ConnectedClient.Search<ElasticSearchProject>(...
    break;
    case 2:
    var results = this.ConnectedClient.Search<ElasticSearchProject>(...
    break;
}

When I try to process the results outside the case statement I can't because the results var does not exist in this context.

I tried to declare the results var outsize the case but it has to be initialized.

How can I work out a solution.

Upvotes: 2

Views: 231

Answers (1)

Paige Cook
Paige Cook

Reputation: 22555

In this case, I use a SearchDescriptor class and set the required search settings on that and pass it to my Search method call.

So for your example.

var searchDescriptor = new SearchDescriptor<ElasticSearchProject>();
 //You can also set options here like Types, Indexes, Fields, Rows, Start

 switch (parameter)
 {
     case 1:
        //Set parameter 1 specific search options here...
        searchDescriptor.Query(...
        break;
     case 2:
        //Set parameter 2 specific search options here...
        searchDescriptor.Query(...
        break;
 } 

 var results = this.ConnectdClient.Search<ElasticSearchProject>(searchDescriptor);

Upvotes: 1

Related Questions