Index name inferenceedit

Many endpoints within the Elasticsearch API expect to receive one or more index names as part of the request, in order to know what index/indices a request should operate on.

NEST has a number of ways in which the index name(s) can be specified

Default Index name on Connection Settingsedit

A default index name can be specified on ConnectionSettings using .DefaultIndex(). This is the default index name to use when no other index name can be resolved for a request

var settings = new ConnectionSettings()
    .DefaultIndex("defaultindex");
var resolver = new IndexNameResolver(settings);
var index = resolver.Resolve<Project>();
index.Should().Be("defaultindex");

Mapping an Index name for a .NET typeedit

A index name can be mapped for CLR types using .MapDefaultTypeIndices() on ConnectionSettings.

var settings = new ConnectionSettings()
    .DefaultMappingFor<Project>(m => m
        .IndexName("projects")
    );
var resolver = new IndexNameResolver(settings);
var index = resolver.Resolve<Project>();
index.Should().Be("projects");

.DefaultMappingFor<T>() can also be used to specify the index name, as well as be used to specify the type name and POCO property that should be used as the id for the document

var settings = new ConnectionSettings()
    .DefaultMappingFor<Project>(m => m
        .IndexName("projects")
    );
var resolver = new IndexNameResolver(settings);
var index = resolver.Resolve<Project>();
index.Should().Be("projects");

An index name for a POCO provided using .MapDefaultTypeIndices() or .DefaultMappingFor<T>() will take precedence over the default index name set on ConnectionSettings. This way, the client can be configured with a default index to use if no index is specified, and a specific index to use for different POCO types.

var settings = new ConnectionSettings()
    .DefaultIndex("defaultindex")
    .DefaultMappingFor<Project>(m => m
        .IndexName("projects")
    );
var resolver = new IndexNameResolver(settings);
var index = resolver.Resolve<Project>();
index.Should().Be("projects");

Explicitly specifying Index name on the requestedit

For API calls that expect an index name, the index name can be explicitly provided on the request

var client = TestClient.Default;
var response = client.Search<Project>(s => s.Index("some-other-index")); 
var requestUri = response.ApiCall.Uri;

requestUri.Should().NotBeNull();
requestUri.LocalPath.Should().StartWith("/some-other-index/");

Provide the index name on the request

When an index name is provided on a request, it will take precedence over the default index name and any index name specified for the POCO type using .MapDefaultTypeIndices() or .DefaultMappingFor<T>()

var client = new ElasticClient(new AlwaysInMemoryConnectionSettings()
    .DefaultIndex("defaultindex")
    .DefaultMappingFor<Project>(m => m
        .IndexName("projects")
    ));

var response = client.Search<Project>(s => s.Index("some-other-index")); 

response.ApiCall.Uri.Should().NotBeNull();
response.ApiCall.Uri.LocalPath.Should().StartWith("/some-other-index/");

Provide the index name on the request

In summary, the order of precedence for determining the index name for a request is

  1. Index name specified on the request
  2. Index name specified for the generic type parameter in the request using .MapDefaultTypeIndices() or .DefaultMappingFor<T>()
  3. Default index name specified on ConnectionSettings