Import mapping via index template

I want to import an index template with a mapping for the fields of the index pmdata* in order to set as type of the field counter_value float.
After the import of the template with counter_value type float, I imported an index with counter_value: 17 (so a long number).
After that, I got again the mapping of the index and the type of counter_value was long and not float as it was in my initial mapping:
"counter_value" : {
"type" : "long"
}

Seems that the import of the template even if it was successful, didn't "affect" the mapping of the index and the type of the counter_value is long instead of float.

How could it be possible to import a template before we have any index in order to have the proper type for the field?

Thank you in advance!

Yes. That's the goal of index templates.
You might have done something wrong.

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Hi David,

Thank you for the prompt answer.
Actually I did all the described actions via curl commands.

I try to import an index template via (with no pmdata* indices in elasticsearch):
curl -k -XPUT --header 'Authorization: Basic YWRtaW46cUUzRSZGRSMyUXNsUzkmcQ==' "https://10.254.88.15:9200/_template/template_1?pretty" -H 'Content-Type: application/json' -d @pmdata_map_float_to_template_v2.json

where the content of pmdata_map_float_to_template_v2.json is the below :

{
  "index_patterns" : "pmdata*",
  "template" : {
    "settings": {
      "index.refresh_interval": "5s"
    },
    "mappings": {
      "_default_": {
        "properties": {
          "counter_display_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "counter_group_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "counter_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "counter_value": {
            "type": "float"
          },
          "date": {
            "type": "date"
          },
          "distinguished_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "is_dynamic": {
            "type": "boolean"
          },
          "measurement_time": {
            "type": "date"
          },
          "vnf_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "vnf_type": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

After that I get

{
  "acknowledged" : true
}

Then I import a pmdata index which has as counter_value the number 7 for example.
Then I am checking the mapping of the index with
curl -k -XGET --header 'Authorization: Basic YWRtaW46cUUzRSZGRSMyUXNsUzkmcQ==' "https://10.254.88.15:9200/pmdata*/_mapping?pretty"

and I am getting the below :

 {
  "pmdata-zts6-2021-03-05" : {
    "mappings" : {
      "properties" : {
        "counter_display_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "counter_group_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "counter_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "counter_value" : {
          "type" : "long"
        },
        "date" : {
          "type" : "date"
        },
        "distinguished_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "is_dynamic" : {
          "type" : "boolean"
        },
        "measurement_time" : {
          "type" : "date"
        },
        "vnf_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "vnf_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

I was expecting that since the imported template was with type float for counter_value, that even it was imported an integer (here 7) , still in mapping the type of the counter_value would be float.

Sorry for the long text.
Thank you again!

Hi @dadoonet ,

Do you have any update for this?
Thank you again.

As I said, it's definitely better to provide a full and simple recreation script. That takes less time for readers to reproduce so we can eventually answer and with a better response time.

Anyway, here is what you did.

DELETE /_template/template_1
PUT /_template/template_1
{
  "index_patterns": "pmdata*",
  "template": {
    "mappings": {
      "properties": {
        "counter_value": {
          "type": "float"
        }
      }
    }
  }
}


DELETE pmdata1
POST pmdata1/_doc
{
  "counter_value": 7
}

GET pmdata1/_mapping

And the fix:

DELETE /_template/template_1
PUT /_template/template_1
{
  "index_patterns": "pmdata*",
  "mappings": {
    "properties": {
      "counter_value": {
        "type": "float"
      }
    }
  }
}


DELETE pmdata1
POST pmdata1/_doc
{
  "counter_value": 7
}

GET pmdata1/_mapping

As you can see the template level does not exist. See Create or update index template API | Elasticsearch Reference [7.12] | Elastic

BTW, you should switch to the new template API. Index templates | Elasticsearch Reference [7.12] | Elastic

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