How to get the exsiting mapping in an ElasticSearch index

.net, c#, elasticsearch, nest

Solution

You can use either overload to pull down the mapping for a specific index/type (but currently not plural indices or types)

client.GetMapping(g => g.Index("myindex").Type("mytype")));

versus

client.GetMapping(new GetMappingRequest {Index = "myindex", Type = "mytype"});

I cannot be sure what will happen when you implicitly supply `<object>` (it may explode; I'm not on a Windows machine to test it), but you obviously do not know the type (`T`) to put there and need something.

Unfortunately, the current limitation with the above (assuming it works with `<object>`) is that you must supply an `Index`, and optionally its `Type`. If you do not specify the `Type`, but the `Index` contains more than one type, then it will just pick the first one that gets returned. And I doubt that is what you want, which is why I created an issue for it on GitHub after discussing it with Greg (one of the NEST developers).

Fortunately, there is always a fallback in NEST, which is to go to the lower level Elasticsearch.NET APIs. There you can make your `IndicesGetMapping` request. Reviewing the generated tests can be found here, which will probably help to understand it better for the generated request.

var response = client.IndicesGetMapping("test_1,test_2");

// Deserialized JSON:
//
// response.test_1.mappings.type_1.properties
// response.test_1.mappings.type_2.properties
// response.test_2.mappings.type_a.properties

Note, could also use these overloads too:

// First parameter is indices (comma separated, potentially wildcarded list)
// _all is a special placeholder to [shockingly] specify all
var response = client.IndicesGetMapping("_all", "_all");
var response = client.IndicesGetMapping("index1,index2", "_all");
// Enjoy the loops:
var response = client.IndicesGetMappingForAll();

These can be found in `IElasticsearchClient.Generated` (huge file, so search for "`IndicesGetMapping`").

Problem

Using Nest and C# I would like to examine the mapping present in an index. ``` var settings = new ConnectionSettings(new Uri("http://localhost:9200")); var client = new ElasticClient(settings); var status = client.Status(); ``` This will return the available indices of the ES server. But I would also like to know what types are mapped in those indices. I tried using: ``` var mapping = client.GetMapping(???); ``` But these methods and overloads seem to need the name of the mapping. Which is exactly what I am trying to find out. I cannot find the proper documentation for this situation.

Original source