• ElasticSearch中的映射(mapping)


            前面博客中创建的索引以及插入数据,都是由Elasticsearch进行自动判断类型,有些时候是需要进行明确字段类型的,否则,自动判断的类型和实际需求有时候是不相符的。

    自动判断的规则如下:

    JSON typeField type
    Boolean: true or false"boolean"
    Whole number: 123"long"
    Floating point: 123.45"double"
    String, valid date: "2014-09-15""date"
    String: "foo bar""string"

    Elasticsearch中支持的类型如下:

    类型表示的数据类型
    Stringstring , text , keyword
    Whole numberbyte , short , integer , long
    Floating pointfloat , double
    Booleanboolean
    Datedate

    注意:

    • string类型在ElasticSearch 旧版本中使用较多,从ElasticSearch 5.x开始不再支持string,由text和 keyword类型替代。
    • text 类型,当一个字段是要被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合。
    • keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。

    创建明确类型的索引:

    1. PUT /study
    2. # 请求数据
    3. {
    4. "settings": {
    5. "index": {
    6. "number_of_shards": "2",
    7. "number_of_replicas": "0"
    8. }
    9. },
    10. "mappings": {
    11. "properties": {
    12. "name": {
    13. "type": "text"
    14. },
    15. "age": {
    16. "type": "integer"
    17. },
    18. "mail": {
    19. "type": "keyword"
    20. },
    21. "hobby": {
    22. "type": "text"
    23. }
    24. }
    25. }
    26. }
    27. # 响应数据
    28. {
    29. "acknowledged": true,
    30. "shards_acknowledged": true,
    31. "index": "study"
    32. }

    这儿可能存在的错误,参考博文:ElasticSearch 7.x 报错:Root mapping definition has unsupported parameters_Sunny Feng的博客-CSDN博客

    查看映射:

    1. GET /study/_mapping
    2. # 响应数据
    3. {
    4. "study": {
    5. "mappings": {
    6. "properties": {
    7. "age": {
    8. "type": "integer"
    9. },
    10. "hobby": {
    11. "type": "text"
    12. },
    13. "mail": {
    14. "type": "keyword"
    15. },
    16. "name": {
    17. "type": "text"
    18. }
    19. }
    20. }
    21. }
    22. }

    插入数据:

    1. POST /study/_bulk
    2. # 请求数据
    3. {"index":{"_index":"study"}}
    4. {"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"}
    5. {"index":{"_index":"study"}}
    6. {"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"}
    7. {"index":{"_index":"study"}}
    8. {"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"}
    9. {"index":{"_index":"study"}}
    10. {"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳"}
    11. {"index":{"_index":"study"}}
    12. {"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影"}
    13. # 响应结果
    14. {
    15. "took": 25,
    16. "errors": false,
    17. "items": [
    18. {
    19. "index": {
    20. "_index": "study",
    21. "_type": "_doc",
    22. "_id": "SHjOVIIBpyNh4YQ4CVSN",
    23. "_version": 1,
    24. "result": "created",
    25. "_shards": {
    26. "total": 1,
    27. "successful": 1,
    28. "failed": 0
    29. },
    30. "_seq_no": 0,
    31. "_primary_term": 1,
    32. "status": 201
    33. }
    34. },
    35. {
    36. "index": {
    37. "_index": "study",
    38. "_type": "_doc",
    39. "_id": "SXjOVIIBpyNh4YQ4CVSN",
    40. "_version": 1,
    41. "result": "created",
    42. "_shards": {
    43. "total": 1,
    44. "successful": 1,
    45. "failed": 0
    46. },
    47. "_seq_no": 0,
    48. "_primary_term": 1,
    49. "status": 201
    50. }
    51. },
    52. {
    53. "index": {
    54. "_index": "study",
    55. "_type": "_doc",
    56. "_id": "SnjOVIIBpyNh4YQ4CVSN",
    57. "_version": 1,
    58. "result": "created",
    59. "_shards": {
    60. "total": 1,
    61. "successful": 1,
    62. "failed": 0
    63. },
    64. "_seq_no": 1,
    65. "_primary_term": 1,
    66. "status": 201
    67. }
    68. },
    69. {
    70. "index": {
    71. "_index": "study",
    72. "_type": "_doc",
    73. "_id": "S3jOVIIBpyNh4YQ4CVSN",
    74. "_version": 1,
    75. "result": "created",
    76. "_shards": {
    77. "total": 1,
    78. "successful": 1,
    79. "failed": 0
    80. },
    81. "_seq_no": 1,
    82. "_primary_term": 1,
    83. "status": 201
    84. }
    85. },
    86. {
    87. "index": {
    88. "_index": "study",
    89. "_type": "_doc",
    90. "_id": "THjOVIIBpyNh4YQ4CVSN",
    91. "_version": 1,
    92. "result": "created",
    93. "_shards": {
    94. "total": 1,
    95. "successful": 1,
    96. "failed": 0
    97. },
    98. "_seq_no": 2,
    99. "_primary_term": 1,
    100. "status": 201
    101. }
    102. }
    103. ]
    104. }

    注意:通过上面的数据库中数据可以看到,默认生成的是_doc类型的数据。

    测试搜索:

    1. POST /study/_search
    2. # 请求数据
    3. {
    4. "query": {
    5. "match": {
    6. "hobby": "音乐"
    7. }
    8. }
    9. }
    10. # 响应数据
    11. {
    12. "took": 2,
    13. "timed_out": false,
    14. "_shards": {
    15. "total": 2,
    16. "successful": 2,
    17. "skipped": 0,
    18. "failed": 0
    19. },
    20. "hits": {
    21. "total": {
    22. "value": 2,
    23. "relation": "eq"
    24. },
    25. "max_score": 1.0470967,
    26. "hits": [
    27. {
    28. "_index": "study",
    29. "_type": "_doc",
    30. "_id": "THjOVIIBpyNh4YQ4CVSN",
    31. "_score": 1.0470967,
    32. "_source": {
    33. "name": "孙七",
    34. "age": 24,
    35. "mail": "555@qq.com",
    36. "hobby": "听音乐、看电影"
    37. }
    38. },
    39. {
    40. "_index": "study",
    41. "_type": "_doc",
    42. "_id": "SnjOVIIBpyNh4YQ4CVSN",
    43. "_score": 0.8527901,
    44. "_source": {
    45. "name": "王五",
    46. "age": 22,
    47. "mail": "333@qq.com",
    48. "hobby": "羽毛球、篮球、游泳、听音乐"
    49. }
    50. }
    51. ]
    52. }
    53. }

  • 相关阅读:
    R语言使用plot函数可视化数据散点图,使用sub参数为可视化图像添加副标题
    利用噪声构建美妙的 CSS 图形
    pytorch -- torch.nn网络结构
    服务端请求伪造(SSRF)及漏洞复现
    Clickhouse实时数仓建设
    力扣--深度优先算法/回溯算法40.组合总和 Ⅱ
    低代码:降低技术能力要求,提升软件开发效率
    MYSQL的Java操作器——JDBC
    python基础命令
    YoloV5改进实战:使用MPDIoU改进YoloV5
  • 原文地址:https://blog.csdn.net/weixin_44799217/article/details/126091706