Nest Tutorial Series: inferring Index Names

Time:2020-10-13

Nest Tutorial Series: three ways to infer Index Names

catalog
  • Nest Tutorial Series: three ways to infer Index Names
    • Set default index on connection
    • Set. Net class mapping index name
    • Specify the index name explicitly when creating the request
    • summary

Set default index on connection

structureConnectionSettingsCall theDefaultIndex()Method to specify the default index name. The default index name is used when the specific index name cannot be resolved for the request

var settings = new ConnectionSettings()
    .DefaultIndex("defaultindex"); 
    
var client = new ElasticClient(settings);
var searchResponse = client.Search();

At this time, when the request is sent, theURLas follows

"http://localhost:9200/defaultindex/_search"

Set. Net class mapping index name

structureConnectionSettingsCall theDefaultMappingFor()Method to configure the mapped index name for the class object

var settings = new ConnectionSettings()
    .DefaultMappingFor(m => m
        .IndexName("projects")
    );

var client = new ElasticClient(settings);
var searchResponse = client.Search();

At this time, when the request is sent, theURLas follows

"http://localhost:9200/projects/_search"
  • DefaultMappingFor()The index name usage priority of method configuration mapping is higher thanDefaultIndex()Method to set the default index name
var settings = new ConnectionSettings()
   .DefaultIndex("defaultindex") 
   .DefaultMappingFor(m => m
       .IndexName("projects") 
   );

var client = new ElasticClient(settings);

var projectSearchResponse = client.Search();

At this time, when the request is sent, theURLas follows

"http://localhost:9200/projects/_search"

But here’s how to write it

var objectSearchResponse = client.Search();

Actual when sending requestURLas follows

"http://localhost:9200/defaultindex/_search"
  • DefaultMappingFor()It can also be used to specify other default values of the type, including property names, properties for document IDS, and so on

Specify the index name explicitly when creating the request

For API calls that require an index name, you can explicitly provide the index name in the request

var settings = new ConnectionSettings();
var client = new ElasticClient(settings);

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

At this time, when the request is sent, theURLas follows

"http://localhost:9200/some-other-index/_search"

When the specified index name is displayed in the request, it takes precedence overDefaultMappingFor()Method to configure the index name andDefaultIndex()Method to set the default index name

var settings = new ConnectionSettings()
    .DefaultIndex("defaultindex")
    .DefaultMappingFor(m => m
        .IndexName("projects")
    );

var client = new ElasticClient(settings);

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

At this time, when the request is sent, theURLas follows

"http://localhost:9200/some-other-index/_search"

summary

When executing the request, the priority of index name value is

  1. Specify the index name explicitly when creating the request
  2. Set. Net class mapping index name
  3. Set default index on connection

Portal of official website