Elasticsearch-核心篇(6)-映射操作

川长思鸟来 2022-09-06 15:19 223阅读 0赞

文章目录

  • 一、创建映射
    • 1.1 创建时映射
    • 1.2 映射修改
    • 1.3 添加时映射
  • 二、查看映射

一、创建映射

  1. 有了索引库,等于有了数据库中的database
  2. 接下来就需要建索引库(index)中的映射,类似于数据库(database)中的表结构(table)
  3. 创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)
  4. 如果不指定映射将会默认按照给定的类型自动添加对应映射

1.1 创建时映射

  1. 可以在创建索引时指定映射

    • 其中mappings.properties为固定结构,指定创建映射属性

    PUT index
    {
    “mappings” : {

    1. "properties" : {
    2. "id" : {
    3. "type" : "long"
    4. },
    5. "title" : {
    6. "type" : "text"
    7. }
    8. }

    }
    }

  2. 创建完成后将会返回创建信息

    {
    “index” : {

    1. "aliases" : { },
    2. "mappings" : {
    3. "properties" : {
    4. "id" : {
    5. "type" : "long"
    6. },
    7. "title" : {
    8. "type" : "text"
    9. }
    10. }
    11. },
    12. "settings" : {
    13. "index" : {
    14. "creation_date" : "1623072136760",
    15. "number_of_shards" : "1",
    16. "number_of_replicas" : "1",
    17. "uuid" : "aatM8ty0QQuZWXo-99lvnA",
    18. "version" : {
    19. "created" : "7080099"
    20. },
    21. "provided_name" : "index"
    22. }
    23. }

    }
    }

1.2 映射修改

  1. 可以对已经存在的索引添加映射
  2. 需要使用 PUT /_mapping
  3. 修改时只需要指定properties即可,不需要指定mappings

    PUT index/_mapping
    {
    “properties” : {

    1. "id" : {
    2. "type" : "long"
    3. },
    4. "title" : {
    5. "type" : "text"
    6. }

    }
    }

  4. 修改结果

    {
    “acknowledged” : true
    }

1.3 添加时映射

  1. 如果不明确指定,则将会在添加数据时自动判定
  2. 创建普通索引,此时没有映射信息

    {
    “index” : {

    1. "aliases" : { },
    2. "mappings" : { },
    3. "settings" : {
    4. "index" : {
    5. "creation_date" : "1623072418993",
    6. "number_of_shards" : "1",
    7. "number_of_replicas" : "1",
    8. "uuid" : "682iz4t_TRa-1Nc7bfm-ng",
    9. "version" : {
    10. "created" : "7080099"
    11. },
    12. "provided_name" : "index"
    13. }
    14. }

    }
    }

  3. 向索引中添加数据

    POST index/_doc/1
    {
    “id”: 3,

    1. "title": "华为手机",
    2. "category": "华为"

    }

  4. 将会自动识别对应值的映射

    {
    “index” : {

    1. "aliases" : { },
    2. "mappings" : {
    3. "properties" : {
    4. "category" : {
    5. "type" : "text",
    6. "fields" : {
    7. "keyword" : {
    8. "type" : "keyword",
    9. "ignore_above" : 256
    10. }
    11. }
    12. },
    13. "id" : {
    14. "type" : "long"
    15. },
    16. "title" : {
    17. "type" : "text",
    18. "fields" : {
    19. "keyword" : {
    20. "type" : "keyword",
    21. "ignore_above" : 256
    22. }
    23. }
    24. }
    25. }
    26. },
    27. "settings" : {
    28. "index" : {
    29. "creation_date" : "1623072418993",
    30. "number_of_shards" : "1",
    31. "number_of_replicas" : "1",
    32. "uuid" : "682iz4t_TRa-1Nc7bfm-ng",
    33. "version" : {
    34. "created" : "7080099"
    35. },
    36. "provided_name" : "index"
    37. }
    38. }

    }
    }

二、查看映射

  1. 查看索引完全信息,内容包含映射信息

    GET index

    {
    “index” : {

    1. "aliases" : { },
    2. "mappings" : {
    3. "properties" : {
    4. "category" : {
    5. "type" : "text",
    6. "fields" : {
    7. "keyword" : {
    8. "type" : "keyword",
    9. "ignore_above" : 256
    10. }
    11. }
    12. },
    13. "id" : {
    14. "type" : "long"
    15. },
    16. "title" : {
    17. "type" : "text",
    18. "fields" : {
    19. "keyword" : {
    20. "type" : "keyword",
    21. "ignore_above" : 256
    22. }
    23. }
    24. }
    25. }
    26. },
    27. "settings" : {
    28. "index" : {
    29. "creation_date" : "1623072418993",
    30. "number_of_shards" : "1",
    31. "number_of_replicas" : "1",
    32. "uuid" : "682iz4t_TRa-1Nc7bfm-ng",
    33. "version" : {
    34. "created" : "7080099"
    35. },
    36. "provided_name" : "index"
    37. }
    38. }

    }
    }

  2. 指定查询索引信息

    GET index/_mapping

    {
    “index” : {

    1. "mappings" : {
    2. "properties" : {
    3. "category" : {
    4. "type" : "text",
    5. "fields" : {
    6. "keyword" : {
    7. "type" : "keyword",
    8. "ignore_above" : 256
    9. }
    10. }
    11. },
    12. "id" : {
    13. "type" : "long"
    14. },
    15. "title" : {
    16. "type" : "text",
    17. "fields" : {
    18. "keyword" : {
    19. "type" : "keyword",
    20. "ignore_above" : 256
    21. }
    22. }
    23. }
    24. }
    25. }

    }
    }

发表评论

表情:
评论列表 (有 0 条评论,223人围观)

还没有评论,来说两句吧...

相关阅读