创建映射字段

  1. PUT /索引库名/_mapping
  2. {
  3. "properties": {
  4. "字段名": {
  5. "type": "类型",
  6. "index": true
  7. "store": true
  8. "analyzer": "分词器"
  9. }
  10. }
  11. }

https://www.elastic.co/guide/en/elasticsearch/reference/7.3/mapping-params.html

字段名:任意填写,下面指定许多属性,例如:

  • type:类型,可以是text、long、short、date、integer、object等
  • index:是否索引,默认为true
  • store:是否存储,默认为false
  • analyzer:指定分词器

    1. PUT /lagou-company-index/_mapping/
    2. {
    3. "properties": {
    4. "name": {
    5. "type": "text",
    6. "analyzer": "ik_max_word"
    7. },
    8. "job": {
    9. "type": "text",
    10. "analyzer": "ik_max_word"
    11. },
    12. "logo": {
    13. "type": "keyword",
    14. "index": "false"
    15. },
    16. "payment": {
    17. "type": "float"
    18. }
    19. }
    20. }

    给lagou-company-index这个索引库设置了4个字段:

  • name:企业名称

  • job: 需求岗位
  • logo:logo图片地址
  • payment:薪资

并且给这些字段设置了一些属性

查看索引映射

  1. GET /索引名称/_mapping

查看所有索引映射关系

  1. GET _all/_mapping

修改索引映射关系

  1. PUT /索引库名/_mapping
  2. {
  3. "properties": {
  4. "字段名": {
  5. "type": "类型",
  6. "index": true
  7. "store": true
  8. "analyzer": "分词器"
  9. }
  10. }
  11. }

:::info 注意:修改映射只能增加字段, 做其它更改只能删除索引 重新建立映射 :::

一次性创建索引和映射

  1. put /索引库名称
  2. {
  3. "settings": {
  4. "索引库属性名": "索引库属性值"
  5. },
  6. "mappings": {
  7. "properties": {
  8. "字段名": {
  9. "映射属性名": "映射属性值"
  10. }
  11. }
  12. }
  13. }
  1. PUT /lagou-employee-index
  2. {
  3. "settings": {},
  4. "mappings": {
  5. "properties": {
  6. "name": {
  7. "type": "text",
  8. "analyzer": "ik_max_word"
  9. }
  10. }
  11. }
  12. }

老版本type

  1. # 获取某个索引信息
  2. GET /library/_mapping
  3. # 获取某个索引下某个type的映射信息
  4. GET /library/_mapping/books
  5. # 获取这个集群内所有的映射信息
  6. GET /_all/_mapping
  7. # 获取这个集群内某两个或多个type的映射信息
  8. GET /_all/_mapping/books,bank_account
  9. Hello hello are you you can call me if you want to come over for a little time and I have a lot of of them them and the other two are is
  10. # 更新修改Mapping映射
  11. # 很遗憾,mapping一旦建立,就不能修改现有的字段映射
  12. # 如果要推到现有的映射,你得重新建立一个索引,然后重新定义映射
  13. # 然后把之前索引里的数据导入到新建立的索引里
  14. # ------具体方法-------
  15. # 1. 给现有的索引定义一个别名,并且把现有的索引指向这个别名,运行步骤2
  16. # 2. 运行: PUT /现有索引/_alias/别名A
  17. # 3. 新创建一个索引,定义好最新的映射
  18. # 4. 将别名指向新的索引,并且取消之前索引的指向,运行步骤5
  19. # 5. 运行
  20. POST /_aliases
  21. {
  22. "actions": [
  23. {"remove": {"index":"现有索引名", "alias":"别名A"}},
  24. {"add": {"index":"新建索引名", "alias":"别名A"}}
  25. ]
  26. }
  27. # 注: 通过这几个步骤就实现了索引的平滑过渡,并且是零停机的
  28. # 删除映射
  29. DELETE /library/books
  30. DELETE /library/books/_mapping
  31. DELETE /library/_mapping/books