elasticsearch 映射(mapping)及查看、创建、修改、删除

说明

映射(mapping),相当于 mysql中的 表结构

可以设置文档(document)的字段名、类型、如何存储和索引 等

如:

  • 设置字段可以全文索引
  • 设置哪些属性包含地理位置
  • 设置是否可以被索引

自动创建

即使不创建映射,在第一次保存文档时,elasticsearch 会自动创建映射,猜测 各个字段的类型

POST /toutiao/_doc
{
    "title": "JAVA从入门到放弃",
    "content": "JAVA高手的技巧,java基础、高级",
    "author":"李雷",
    "like":229,
    "date":"2021-08-30"
}

查看映射

发请求,如下:

GET /toutiao/_mapping

显示结果如下:

{
  "blog" : {
    "mappings" : {
      "properties" : {
        "author" : {
          "type" : "text",           //字符串默认为text
          "fields" : {
            "keyword" : {            //默认有keyword属性
              "type" : "keyword",    //keyword类型,用于做精确匹配检索
              "ignore_above" : 256
            }
          }
        },
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "date" : {
          "type" : "date"        //日期默认为date
        },
        "like" : {
          "type" : "long"        //数字默认为long
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

创建 索引且设置映射

在保存 文档(document)之前就创建映射
在第一次保存文档时,会自动创建映射

注意: 创建映射后 无法修改

删除之前创建的索引:

delete /toutiao

创建索引,并设置映射:

PUT /toutiao
{
  "mappings": {
    "properties": {
      "title":    { "type": "text" },  
      "content":  { "type": "text"  }, 
      "author":   { "type": "keyword"  },
      "like":   { "type": "integer"  },
      "date": {"type":"date"}
    }
  }
}

执行结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "blog"
}

JSON数据说明

  • 字段名:根据业务需求,可任意填写

  • type:类型,详见

  • index:控制该字段是否可以查询到,默认为 true。官方文档

    • true:字段会被索引,可以用来进行搜索
    • false:字段不会被索引,不能用来搜索
  • analyzer:分词器,这里的 ik_max_word 即使用 ik 分词器,后面会有专门的章节学习

查看映射

GET /blog/_mapping

结果如下, 比保存文档时默认创建的映射,简洁清晰:

{
  "blog" : {
    "mappings" : {
      "properties" : {
        "author" : {
          "type" : "keyword"
        },
        "content" : {
          "type" : "text"
        },
        "date" : {
          "type" : "date"
        },
        "like" : {
          "type" : "integer"
        },
        "title" : {
          "type" : "text"
        }
      }
    }
  }
}

已存在索引 添加映射

如果只创建索引,可以添加映射

PUT /blog/_mapping
{
    "properties":{
        "abstract":{
            "type":"keyword",
            "index":false
        }

    }
}

执行结果:

{
  "acknowledged" : true
}

查看映射

get /blog/_mapping

执行结果:

{
  "blog" : {
    "mappings" : {
      "properties" : {
        "abstract" : {                //新添加的字段
          "type" : "keyword",
          "index" : false
        },
        "author" : {
          "type" : "keyword"
        },
        "content" : {
          "type" : "text"
        },
        "date" : {
          "type" : "date"
        },
        "like" : {
          "type" : "integer"
        },
        "title" : {
          "type" : "text"
        }
      }
    }
  }
}

修改映射

已存在的映射,只能添加字段,不能修改已有的字段类型

这与mysql不同,mysql可以修改表结构

这是因为,一旦修改映射,就会影响已经存在的数据,会导致问题

可通过以下方式,达到修改映射的功能:

数据不重要,可以停止服务

删除索引,重新建映射

数据重要、不能停服务

https://www.malaoshi.top/show_1IX1ogV3uEVI.html

删除映射

目前不能删除


原文出处:/show_1IX1mRiUCFTs.html