Administrator
Published on 2023-07-05 / 75 Visits
0

elasticsearch mapping

一、mapping基本操作

1.新建mapping

添加名字和年龄字段

PUT /student2/_mapping
{
  "properties": {
    "age": {
      "type": "integer"
    },
    "id": {
      "type": "keyword"
    },
    "info": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "name": {
      "type": "text"
    },
    "sex": {
      "type": "keyword"
    },
    "addr":{
      "type": "geo_point"
    },
    "ip":{
      "type": "ip"
    }
  }
}
{
  "acknowledged" : true
}

2.添加新字段

PUT /student/_mapping
{
  "properties": {
    "name":{
      "type": "text"
    },
    "age":{
      "type": "integer"
    },
    "sex": {
      "type": "keyword"
    }
  }
}
{
  "acknowledged" : true
}

3.修改mapping中已存在的字段

PUT /student/_mapping
{
  "properties": {
    "name":{
      "type": "integer"
    },
    "age":{
      "type": "integer"
    },
    "sex": {
      "type": "keyword"
    }
  }
}
{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[show-data-2][172.17.0.10:9302][indices:admin/mapping/put]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "mapper [name] of different type, current_type [text], merged_type [integer]"
  },
  "status": 400
}
3.1 结论

由此得出结论,已存mapping中的字段不可修改

4.删除mapping中的字段

POST /student/_update/1
{
  "script":"ctx._source.remove(\"age\")"
}
{
  "_index" : "student",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 16,
  "_primary_term" : 3
}

5.获取索引中的mapping

GET /student/_mapping
{
  "student" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"
        },
        "name" : {
          "type" : "text"
        },
        "sex" : {
          "type" : "keyword"
        }
      }
    }
  }
}