• ElasticSearch中RESTFUL API(创建索引、增删改查、DSL搜索、高亮显示、聚合)


    RESTful API

            在Elasticsearch中,提供了功能丰富的RESTful API的操作,包括基本的CRUD、创建索引、删除索引等操作。

    创建非结构化索引

            在Lucene中,创建索引是需要定义字段名称以及字段的类型的,在Elasticsearch中提供了非结构化的索引,就是不需要创建索引结构,即可写入数据到索引中,实际上在Elasticsearch底层会进行结构化操作,此操作对用户是透明的。

    创建空索引:

    1. put /test
    2. {
    3. "settings": {
    4. "index": {
    5. "number_of_shards": "2",
    6. "number_of_replicas": "0"
    7. }
    8. }
    9. }

     删除索引:

    DELETE /test

    插入数据

    URL规则: POST /{索引}/{类型}/{id}

    1. POST /test/user/1001
    2. # 数据
    3. {
    4. "id": 1001,
    5. "name": "张三",
    6. "age": 20,
    7. "sex": "男"
    8. }
    9. # 响应数据
    10. {
    11. "_index": "test",
    12. "_type": "user",
    13. "_id": "1001",
    14. "_version": 1,
    15. "result": "created",
    16. "_shards": {
    17. "total": 2,
    18. "successful": 1,
    19. "failed": 0
    20. },
    21. "_seq_no": 0,
    22. "_primary_term": 1
    23. }

     注意:非结构化的索引,不需要事先创建,直接插入数据默认创建索引。

    不指定id插入数据:

    1. POST /test/user
    2. # 数据
    3. {
    4. "id": 1001,
    5. "name": "张三",
    6. "age": 20,
    7. "sex": "男"
    8. }

    更新数据

    在Elasticsearch中,文档数据是不能修改的,但是可以通过覆盖的方式进行更新

    1. PUT /test/user/1001
    2. # 数据
    3. {
    4. "id": 1001,
    5. "name": "张三",
    6. "age": 21,
    7. "sex": "女"
    8. }
    9. # 响应数据
    10. {
    11. "_index": "test",
    12. "_type": "user",
    13. "_id": "1001",
    14. "_version": 2,
    15. "result": "updated",
    16. "_shards": {
    17. "total": 2,
    18. "successful": 1,
    19. "failed": 0
    20. },
    21. "_seq_no": 2,
    22. "_primary_term": 1
    23. }

    通过上面示例可以看到数据已经被覆盖了。 问题来了,可以局部更新吗? -- 可以的。 前面不是说,文档数据不能更新吗?

    事实上,其实是这样的: 在内部,依然会查询到这个文档数据,然后进行覆盖操作,步骤如下:         1. 从旧文档中检索JSON

            2. 修改它

            3. 删除旧文档

            4. 索引新文档

    1. 注意: 这儿多了_update标识符
    2. POST /user/1001/_update
    3. # 数据
    4. {
    5. "doc": {
    6. "age": 23
    7. }
    8. }
    9. # 响应数据
    10. {
    11. "_index": "test",
    12. "_type": "user",
    13. "_id": "1001",
    14. "_version": 3,
    15. "result": "updated",
    16. "_shards": {
    17. "total": 2,
    18. "successful": 1,
    19. "failed": 0
    20. },
    21. "_seq_no": 3,
    22. "_primary_term": 1
    23. }

    删除数据

    在Elasticsearch中,删除文档数据,只需要发起DELETE请求即可。

    DETELE /test/user/1001

     注意:删除一个文档也不会立即从磁盘上移除,它只是被标记成已删除。Elasticsearch将会在你之后添加更多索引的 时候才会在后台进行删除内容的清理。

    搜索数据

    根据id搜索数据

    1. GET /test/user/QHi1UoIBpyNh4YQ4T1Sq
    2. # 响应数据如下:
    3. {
    4. "_index": "test",
    5. "_type": "user",
    6. "_id": "QHi1UoIBpyNh4YQ4T1Sq",
    7. "_version": 1,
    8. "_seq_no": 1,
    9. "_primary_term": 1,
    10. "found": true,
    11. "_source": {
    12. "id": 1001,
    13. "name": "张三",
    14. "age": 20,
    15. "sex": "男"
    16. }
    17. }

    搜索全部数据     【为了更好展示全部数据效果,先插入一些数据】

    1. GET /test/user/_search
    2. # 响应数据(默认返回10条数据)
    3. {
    4. "took": 918,
    5. "timed_out": false,
    6. "_shards": {
    7. "total": 1,
    8. "successful": 1,
    9. "skipped": 0,
    10. "failed": 0
    11. },
    12. "hits": {
    13. "total": {
    14. "value": 4,
    15. "relation": "eq"
    16. },
    17. "max_score": 1.0,
    18. "hits": [
    19. {
    20. "_index": "test",
    21. "_type": "user",
    22. "_id": "QHi1UoIBpyNh4YQ4T1Sq",
    23. "_score": 1.0,
    24. "_source": {
    25. "id": 1001,
    26. "name": "张三",
    27. "age": 20,
    28. "sex": "男"
    29. }
    30. },
    31. {
    32. "_index": "test",
    33. "_type": "user",
    34. "_id": "1002",
    35. "_score": 1.0,
    36. "_source": {
    37. "id": 1002,
    38. "name": "李四",
    39. "age": 23,
    40. "sex": "女"
    41. }
    42. },
    43. {
    44. "_index": "test",
    45. "_type": "user",
    46. "_id": "1003",
    47. "_score": 1.0,
    48. "_source": {
    49. "id": 1003,
    50. "name": "王五",
    51. "age": 27,
    52. "sex": "男"
    53. }
    54. },
    55. {
    56. "_index": "test",
    57. "_type": "user",
    58. "_id": "1004",
    59. "_score": 1.0,
    60. "_source": {
    61. "id": 1004,
    62. "name": "赵六",
    63. "age": 29,
    64. "sex": "女"
    65. }
    66. }
    67. ]
    68. }
    69. }

    关键字搜素数据:

    1. # 查询年龄等于20的用户
    2. GET /test/user/_search?q=age:20
    3. # 响应数据
    4. {
    5. "took": 1,
    6. "timed_out": false,
    7. "_shards": {
    8. "total": 1,
    9. "successful": 1,
    10. "skipped": 0,
    11. "failed": 0
    12. },
    13. "hits": {
    14. "total": {
    15. "value": 1,
    16. "relation": "eq"
    17. },
    18. "max_score": 1.0,
    19. "hits": [
    20. {
    21. "_index": "test",
    22. "_type": "user",
    23. "_id": "QHi1UoIBpyNh4YQ4T1Sq",
    24. "_score": 1.0,
    25. "_source": {
    26. "id": 1001,
    27. "name": "张三",
    28. "age": 20,
    29. "sex": "男"
    30. }
    31. }
    32. ]
    33. }
    34. }

    DSL搜索

            Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。 DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。

    示例:查询年龄等于20的用户

    1. POST /test/user/_search
    2. # 请求体
    3. {
    4. "query": {
    5. "match": {
    6. "age": 20
    7. }
    8. }
    9. }
    10. # 响应数据
    11. {
    12. "took": 1,
    13. "timed_out": false,
    14. "_shards": {
    15. "total": 1,
    16. "successful": 1,
    17. "skipped": 0,
    18. "failed": 0
    19. },
    20. "hits": {
    21. "total": {
    22. "value": 1,
    23. "relation": "eq"
    24. },
    25. "max_score": 1.0,
    26. "hits": [
    27. {
    28. "_index": "test",
    29. "_type": "user",
    30. "_id": "QHi1UoIBpyNh4YQ4T1Sq",
    31. "_score": 1.0,
    32. "_source": {
    33. "id": 1001,
    34. "name": "张三",
    35. "age": 20,
    36. "sex": "男"
    37. }
    38. }
    39. ]
    40. }
    41. }

     示例:查询年龄大于22岁的女性用户:

    目前数据库中的数据:

    1. POST /test/user/_search
    2. # 请求数据
    3. {
    4. "query": {
    5. "bool": {
    6. "filter": {
    7. "range": {
    8. "age": {
    9. "gt": 22
    10. }
    11. }
    12. },
    13. "must": {
    14. "match": {
    15. "sex": "女"
    16. }
    17. }
    18. }
    19. }
    20. }
    21. # 响应数据
    22. {
    23. "took": 2,
    24. "timed_out": false,
    25. "_shards": {
    26. "total": 1,
    27. "successful": 1,
    28. "skipped": 0,
    29. "failed": 0
    30. },
    31. "hits": {
    32. "total": {
    33. "value": 2,
    34. "relation": "eq"
    35. },
    36. "max_score": 0.6931471,
    37. "hits": [
    38. {
    39. "_index": "test",
    40. "_type": "user",
    41. "_id": "1002",
    42. "_score": 0.6931471,
    43. "_source": {
    44. "id": 1002,
    45. "name": "李四",
    46. "age": 23,
    47. "sex": "女"
    48. }
    49. },
    50. {
    51. "_index": "test",
    52. "_type": "user",
    53. "_id": "1004",
    54. "_score": 0.6931471,
    55. "_source": {
    56. "id": 1004,
    57. "name": "赵六",
    58. "age": 29,
    59. "sex": "女"
    60. }
    61. }
    62. ]
    63. }
    64. }

    全文搜索

    1. POST /test/user/_search
    2. # 请求数据
    3. {
    4. "query": {
    5. "match": {
    6. "name": "张三 李四"
    7. }
    8. }
    9. }
    10. # 响应数据
    11. {
    12. "took": 7,
    13. "timed_out": false,
    14. "_shards": {
    15. "total": 1,
    16. "successful": 1,
    17. "skipped": 0,
    18. "failed": 0
    19. },
    20. "hits": {
    21. "total": {
    22. "value": 2,
    23. "relation": "eq"
    24. },
    25. "max_score": 2.4079456,
    26. "hits": [
    27. {
    28. "_index": "test",
    29. "_type": "user",
    30. "_id": "QHi1UoIBpyNh4YQ4T1Sq",
    31. "_score": 2.4079456,
    32. "_source": {
    33. "id": 1001,
    34. "name": "张三",
    35. "age": 20,
    36. "sex": "男"
    37. }
    38. },
    39. {
    40. "_index": "test",
    41. "_type": "user",
    42. "_id": "1002",
    43. "_score": 2.4079456,
    44. "_source": {
    45. "id": 1002,
    46. "name": "李四",
    47. "age": 23,
    48. "sex": "女"
    49. }
    50. }
    51. ]
    52. }
    53. }

    高亮显示

    1. POST /test/user/_search
    2. # 请求数据
    3. {
    4. "query": {
    5. "match": {
    6. "name": "张三 李四"
    7. }
    8. },
    9. "highlight": {
    10. "fields": {
    11. "name": {}
    12. }
    13. }
    14. }
    15. # 响应数据
    16. {
    17. "took": 6,
    18. "timed_out": false,
    19. "_shards": {
    20. "total": 1,
    21. "successful": 1,
    22. "skipped": 0,
    23. "failed": 0
    24. },
    25. "hits": {
    26. "total": {
    27. "value": 2,
    28. "relation": "eq"
    29. },
    30. "max_score": 2.4079456,
    31. "hits": [
    32. {
    33. "_index": "test",
    34. "_type": "user",
    35. "_id": "QHi1UoIBpyNh4YQ4T1Sq",
    36. "_score": 2.4079456,
    37. "_source": {
    38. "id": 1001,
    39. "name": "张三",
    40. "age": 20,
    41. "sex": "男"
    42. },
    43. "highlight": {
    44. "name": [
    45. ""
    46. ]
    47. }
    48. },
    49. {
    50. "_index": "test",
    51. "_type": "user",
    52. "_id": "1002",
    53. "_score": 2.4079456,
    54. "_source": {
    55. "id": 1002,
    56. "name": "李四",
    57. "age": 23,
    58. "sex": "女"
    59. },
    60. "highlight": {
    61. "name": [
    62. ""
    63. ]
    64. }
    65. }
    66. ]
    67. }
    68. }

    聚合

    在Elasticsearch中,支持聚合操作,类似SQL中的group by操作。

    1. POST /test/user/_search
    2. # 请求数据
    3. {
    4. "aggs": {
    5. "all_interests": {
    6. "terms": {
    7. "field": "age"
    8. }
    9. }
    10. }
    11. }
    12. # 响应结果
    13. {
    14. "took": 25,
    15. "timed_out": false,
    16. "_shards": {
    17. "total": 1,
    18. "successful": 1,
    19. "skipped": 0,
    20. "failed": 0
    21. },
    22. "hits": {
    23. "total": {
    24. "value": 4,
    25. "relation": "eq"
    26. },
    27. "max_score": 1.0,
    28. "hits": [
    29. {
    30. "_index": "test",
    31. "_type": "user",
    32. "_id": "QHi1UoIBpyNh4YQ4T1Sq",
    33. "_score": 1.0,
    34. "_source": {
    35. "id": 1001,
    36. "name": "张三",
    37. "age": 20,
    38. "sex": "男"
    39. }
    40. },
    41. {
    42. "_index": "test",
    43. "_type": "user",
    44. "_id": "1002",
    45. "_score": 1.0,
    46. "_source": {
    47. "id": 1002,
    48. "name": "李四",
    49. "age": 23,
    50. "sex": "女"
    51. }
    52. },
    53. {
    54. "_index": "test",
    55. "_type": "user",
    56. "_id": "1003",
    57. "_score": 1.0,
    58. "_source": {
    59. "id": 1003,
    60. "name": "王五",
    61. "age": 27,
    62. "sex": "男"
    63. }
    64. },
    65. {
    66. "_index": "test",
    67. "_type": "user",
    68. "_id": "1004",
    69. "_score": 1.0,
    70. "_source": {
    71. "id": 1004,
    72. "name": "赵六",
    73. "age": 29,
    74. "sex": "女"
    75. }
    76. }
    77. ]
    78. },
    79. "aggregations": {
    80. "all_interests": {
    81. "doc_count_error_upper_bound": 0,
    82. "sum_other_doc_count": 0,
    83. "buckets": [
    84. {
    85. "key": 20,
    86. "doc_count": 1
    87. },
    88. {
    89. "key": 23,
    90. "doc_count": 1
    91. },
    92. {
    93. "key": 27,
    94. "doc_count": 1
    95. },
    96. {
    97. "key": 29,
    98. "doc_count": 1
    99. }
    100. ]
    101. }
    102. }
    103. }

  • 相关阅读:
    backurl: heytapbrowser://main/iflow?sub_target=only_enter_iflow
    FISCO BCOS | 构建第一个区块链应用程序
    记录如何用php将敏感文字内容替换为星号的方法
    7-4 USB接口的定义 (10分)
    Go语言学习笔记—golang包管理
    求生之路2服务器搭建插件安装及详细的游戏参数配置教程windows
    Kubernetes 系统化学习之 资源清单篇(三)
    2024第十五届蓝桥杯 C/C++ B组 参赛经历分享(以及部分题解)
    Unity物体查找方式
    vue2.x引入threejs
  • 原文地址:https://blog.csdn.net/weixin_44799217/article/details/126073773