frostshoxx
frostshoxx

Reputation: 536

Elasticsearch Nest 6 - Get index metadata

Currently, I can retrieve the index mapping metadata from the following command on Kibana

GET /[indexName]/_mapping/[documentType]

Is there a way to do that on Elasticsearch Nest Client? If not, what other options would I have?

Upvotes: 0

Views: 1288

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

You can retrieve it with

var defaultIndex = "default-index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

var settings = new ConnectionSettings(pool)
    .DefaultIndex(defaultIndex);

var client = new ElasticClient(settings);

var mappingResponse = client.GetMapping<MyDocument>();

which will send a request to

GET http://localhost:9200/default-index/_mapping/mydocument

In this case

  • index will be "default-index", the default index configured on Connection Settings
  • type will be "mydocument", inferred from the POCO type MyDocument

You can specify index and/or type explicitly if you want to

var mappingResponse = client.GetMapping<MyDocument>(m => m
    .Index("foo")
    .Type("bar")
);

which sends the following request

GET http://localhost:9200/foo/_mapping/bar

As well as target all indices and/or all types

var mappingResponse = client.GetMapping<MyDocument>(m => m
    .AllIndices()
    .AllTypes()
);

which sends the following request

GET http://localhost:9200/_mapping

Upvotes: 1

Related Questions