0

I need to create an index with mapping using a json string provided from the input. I am trying to pass the json mapping string in a lowlevel client since my usecase has a long list of mappings which I can not do using a high-level client. However, it is creating a null mapping with the lowlevel client I am using. Below is a simple example of my mapping json string.

mappingString = {
"mappings" : {  
"exchangeid" : {
          "type" : "double"
        }
}

Below is the code snippet which I am using to create an index and then lowlevel request to create the mapping.

        CreateIndexResponse createIndexResponse = ElasticAccessor.Client.Indices.Create(IndexName, c => c
        .InitializeUsing(indexConfig));

        // Put mapping request
        StringResponse putMappingRequest = ElasticAccessor.Client.LowLevel.DoRequest<StringResponse>(HttpMethod.PUT, 
            "/" + IndexName + "/_mapping", 
            PostData.String(this.mappingString));

Any help or suggestion is greatly appreciated.

1 Answer 1

2

The mapping string JSON format is not correct for an Update Mapping API call. It's closer to the format used when creating an index with a mapping, but still not correct.

To make a call with the low level client to create an index and mapping at the same time

var client = new ElasticClient();

var IndexName = "my-index";

var request = @"{
    ""mappings"": {
        ""properties"": {
            ""exchangeid"": { ""type"": ""double"" }
        }
    }
}";

var response = client.LowLevel.Indices.Create<StringResponse>(IndexName, request);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the response. The mapping thing works. However, I was wondering how can I attach IndexSettings such as NumberOfReplicas = 1, NumberOfShards = 2 etc. in this lowlevel client along with mappings. I used to do it in using a indexConfig in the high-level client as follows. CreateIndexResponse createIndexResponse = ElasticAccessor.Client.Indices.Create(IndexName, c => c .InitializeUsing(indexConfig));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.