Nest Inferred mappings output


#1

Hi,

I'm using the Nest library and trying to get what the inferred mapping is in JSON.


(Mark Walkom) #2

And are you looking for assistance with this?


(Russ Cam) #3

You can capture the request and response using OnRequestCompleted() in conjunction with .DisableDirectStreaming()

void Main()
{
	var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
	var defaultIndex = "default-index";
	var connectionSettings = new ConnectionSettings(pool)
			.DefaultIndex(defaultIndex)
			.PrettyJson()
			.DisableDirectStreaming()
			.OnRequestCompleted(response =>
				{
					// log out the request
					if (response.RequestBodyInBytes != null)
					{
						Console.WriteLine(
							$"{response.HttpMethod} {response.Uri} \n" +
							$"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
					}
					else
					{
						Console.WriteLine($"{response.HttpMethod} {response.Uri}");
					}
                    
                    Console.WriteLine();

					// log out the response
					if (response.ResponseBodyInBytes != null)
					{
						Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
								 $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
								 $"{new string('-', 30)}\n");
					}
					else
					{
						Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
								 $"{new string('-', 30)}\n");
					}
				});
				
	var client = new ElasticClient(connectionSettings);
	
	if (client.IndexExists(defaultIndex).Exists)
		client.DeleteIndex(defaultIndex);

    client.CreateIndex(defaultIndex, c => c
        .Mappings(m => m
            .Map<Document>(mm => mm
                .AutoMap()
            )
        )
    );

}

public class Document
{
    public string Field1 { get; set;} 
}

this yields

PUT http://localhost:9200/default-index?pretty=true 
{
  "mappings": {
    "document": {
      "properties": {
        "field1": {
          "type": "string"
        }
      }
    }
  }
}

You can also use a web debugging proxy such as Fiddler to capture traffic.


#4

Hi Russ,

Thanks for the help on that!
Is there any way else besides making a request out to the cluster to get that json mapping?


(Russ Cam) #5
  1. Use an InMemoryConnection on ConnectionSettings

    or

  2. pass the CreateIndexDescriptor or CreateIndexRequest to client.Serializer


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.