user3554960
user3554960

Reputation: 11

How to reuse same Nest client Elastic Search, but have all indices injected when declaring the client

I have 3 indices in Elastic Search and I will be querying them one at a time (meaning - I want results from only 1 index at any moment). How to declare ElasticSearch client and reuse?

Adding index name in the SearchRequest doesn't look like an option because when I don't give any default index name when initiating the client, it gives exception. Adding code below, any help is appreciated.

string cloudid = "something";
            var credentials = new BasicAuthenticationCredentials("something", "something");
            var connectionPool = new CloudConnectionPool(cloudid, credentials);
            var settings = new ConnectionSettings(connectionPool);
            var client = new ElasticClient(settings); //EXCEPTION HERE THAT - Index Name is NULL

ISearchRequest searchRequest = new SearchRequest("indexname")
            {
                Query = new TermQuery { Field = Infer.Field<Doctor>(d => d.FirstName), Value = "FirstName73069" }, 
                Size = 10000
            };

            var secondSearchResponse = await client.SearchAsync<Doctor>(searchRequest);

This is the code I have and it breaks at line 5(added comment). Note: I have to use SearchRequest object for my use case. Please suggest accordingly.

using Nest 7.17.4 version.

Upvotes: 1

Views: 167

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

Default index name is used when when no other index name can be resolved for a request.

If you want to map different CLR types to different indexes while creating connection, you can use DefaultMappingFor

new ConnectionSettings()
.DefaultIndex("defaultindex")
.DefaultMappingFor<Index1>(m => m.IndexName("index1"))
.DefaultMappingFor<Index2>(m => m.IndexName("index2"))

Above query defines default index as "defaultindex" and respective indexes for models Index1 and Index2

On using new SearchRequest() , request will be queried against "index1" , inferred from settings.

You can also specify index name as SearchRequest("index1")

Upvotes: 0

Related Questions