Search code examples
nestelasticsearch-7

How to get mapping for an index in elasticsearch nest?


I have written following functions and getting response like shown below:

  public async Task<GetMappingResponse> ShowMapping(string indexname)
        {
            var result =await _client.Indices.GetMappingAsync(new GetMappingRequest(indexname));
            return result;
        }

Response:

{
    "apiCall": {
        "auditTrail": [
            {
                "event": 5,
                "node": {
                    "clientNode": false,
                    ......
                    ......                  
                    "name": null,
                    "settings": {},
                    "uri": "http://localhost:9200/"
                },
                "path": "pure/_mapping",
                "ended": "2020-01-22T11:25:48.2324298Z",
                "started": "2020-01-22T11:25:47.836833Z",
                "exception": null
            }
        ],
        "debugInformation": "Successful (200) low level call on GET: /pure/_mapping\r\n# Audit trail of this API call:\r\n - [1] PingSuccess: Node: http://localhost:9200/ Took: 00:00:00.1716082\r\n - [2] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.3955968\r\n# Request:\r\n<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>\r\n
         #Response:\r\n{\"pure\":{\"mappings\":{\"properties\":{\"ID\":{\"type\":\"integer\"},\"Name\":{\"type\":\"text\"},\"category\":{\"type\":\"nested\",\"properties\":{\"catid\":{\"type\":\"integer\"},\"catname\":{\"type\":\"text\"}}}}}}}\r\n",
        "deprecationWarnings": [],
         ..........
         ..........
        "uri": "http://localhost:9200/pure/_mapping",
        "connectionConfiguration": {}
    }
}

As you can see the debuginformation field does have my required response, can anyone help me how to get that format of mapping like we get in kibana.

Required Response:

{
    "pure": {
        "mappings": {
            "properties": {
                "ID": {
                    "type": "integer"
                },
                "Name": {
                    "type": "text"
                },
                "category": {
                    "type": "nested",
                    "properties": {
                        "catid": {
                            "type": "integer"
                        },
                        "catname": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    }
}

Solution

  • With NEST, the JSON response is deserialized into a .NET type to work with, in this case, GetMappingResponse. You can iterate over properties on this type to inspect the mapping.

    If you'd prefer to get the JSON response as a string, you can use the low level client to do so. The low level client is exposed on the high level client (NEST)

    var client = new ElasticClient();
    var indexName = "pure";
    var response = client.LowLevel.Indices.GetMapping<StringResponse>(indexName);
    
    // the JSON string response
    var jsonMapping = response.Body;