How to get exsiting display in ElasticSearch index

Using Nest and C #, I would like to examine the mapping present in the index.

var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var client = new ElasticClient(settings);
var status = client.Status();

      

This will return the available ES server indexes. But I would also like to know what types are displayed in these indices. I tried using:

var mapping = client.GetMapping(???);

      

But these methods and overloads seem to need a display name. This is exactly what I am trying to figure out. I cannot find the correct documentation for this situation.

+3


source to share


2 answers


You can use either overloading to pull the mapping for a specific index / type (but these are not multiple indexes or types currently)

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

      

against

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

      

I can't be sure what happens when you implicitly put <object>

(it might explode, I'm not on a Windows machine to test it), but you obviously don't know the type ( T

) to put in there and need something.

Unfortunately, the current limit with the above (assuming it works with <object>

) is that you have to specify Index

and optionally it Type

. If you don't specify Type

, but Index

contains more than one type, then it will simply select the first one to be returned. And I doubt this is what you want, so I created an issue for it on GitHub after discussing with Greg (one of the NEST developers).



Fortunately, there is always a fallback in NEST that has to move to the lower level Elasticsearch.NET APIs. There you can make your request IndicesGetMapping

. An overview of the generated tests can be found here which will probably help you better understand it for the generated query.

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. You can also use these overloads:

// 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();

      

This can be found inIElasticsearchClient.Generated

(huge file, so search for IndicesGetMapping

").

+3


source


This post is old. I am sharing a solution that worked for me in Elasticsearch Nest 5.0. I only have one index and getting a display like this

var mapping = client.GetMapping(new GetMappingRequest()).Mappings.AsEnumerable().FirstOrDefault().Value;

      



Hope can help others.

0


source







All Articles