Home > Back-end >  How to add mapping via go-elasticsearch package
How to add mapping via go-elasticsearch package

Time:05-10

Basically when I am trying to insert a new document to non-existing index, it is automatically sets to dynamic mapping. But I have issues where in sometimes I want to change the data type of the fields on the ES.

I want to set it via my go-lang service but looks like go-elasticsearch package doesn't support it? correct me if I am wrong

CodePudding user response:

You can create an index along with its mapping using go-elasticsearch/esapi go-elasticsearch/esapi

Create a request as below:

mapping := `{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1
      },
      "mappings": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "message": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
        }
}`

// Index - pass index name
// Body - pass mapping, settings etc
indexReq := esapi.IndicesCreateRequest{
    Index: "my-index",
    Body: strings.NewReader(string(mapping)),
}

resp, err := indexReq.Do(ctx, elasticclient)
if err != nil {
    // handle error
}

by this way you can create a new index with specific mapping

  • Related