:::info 为什么要用es? ::: Elastic 的底层是开源库 Lucene。但是,你没法直接用 Lucene,必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装,提供了 REST API 的操作接口,开箱即用。

    :::info 数据管理分级? :::

    1. Cluster(比对mysql)
    2. Node(比对mysql)
    3. Index(索引组,类似于mysql单个表)
    4. Type(记录分组,逻辑分组)
    5. Document(单条记录)

    :::info 使用方式? :::

    1. curl -X GET 'http://localhost:9200/_cat/indices?v' // 查看当前节点的所有 Index
    2. curl 'localhost:9200/_mapping?pretty=true' // 列出每个 Index 所包含的 Type
    3. curl -X PUT 'localhost:9200/weather' // 新建一个名叫weather的 Index
    4. curl -X DELETE 'localhost:9200/weather' // 删除这个 Index
    5. 令:index=accounts/type=person
    6. curl -X PUT 'localhost:9200/accounts/person/10001' -d '{"user": "李四","title": "工程师","desc": "系统管理"}' // 新增带ID记录
    7. curl -X POST 'localhost:9200/accounts/person' -d '{"user": "李四","title": "工程师","desc": "系统管理"}' // 新增无ID记录
    8. curl -X DELETE 'localhost:9200/accounts/person/1' // 删除记录
    9. 返回:
    10. {
    11. "_index":"accounts",
    12. "_type":"person",
    13. "_id":"AV3qGfrC6jMbsbXb6k1p", // POST返回随机字符串
    14. "_version":1, // PUT更新后版本递增
    15. "result":"created",
    16. "_shards":{"total":2,"successful":1,"failed":0},
    17. "created":true // 执行结果
    18. }
    19. curl 'localhost:9200/accounts/person/1?pretty=true' // 查看指定ID记录
    20. {
    21. "_index" : "accounts",
    22. "_type" : "person",
    23. "_id" : "1",
    24. "_version" : 1,
    25. "found" : true, // 搜索结果
    26. "_source" : { // 原始记录
    27. "user" : "张三",
    28. "title" : "工程师",
    29. "desc" : "数据库管理"
    30. }
    31. }
    32. curl 'localhost:9200/accounts/person/_search' // 返回type下所有记录
    33. {
    34. "took":2, // 耗时
    35. "timed_out":false, // 是否超时
    36. "_shards":{"total":5,"successful":5,"failed":0}, // 执行节点信息
    37. "hits":{ // 命中信息
    38. "total":2, // 总量
    39. "max_score":1.0, // 最高匹配度
    40. "hits":[ // 命中列表
    41. {
    42. "_index":"accounts",
    43. "_type":"person",
    44. "_id":"AV3qGfrC6jMbsbXb6k1p",
    45. "_score":1.0, // 匹配度(最大为1)
    46. "_source": { // 原记录
    47. "user": "李四",
    48. "title": "工程师",
    49. "desc": "系统管理"
    50. }
    51. },
    52. {
    53. "_index":"accounts",
    54. "_type":"person",
    55. "_id":"1",
    56. "_score":1.0,
    57. "_source": {
    58. "user" : "张三",
    59. "title" : "工程师",
    60. "desc" : "数据库管理,软件开发"
    61. }
    62. }
    63. ]
    64. }
    65. }
    66. curl 'localhost:9200/accounts/person/_search' -d ' // 带参搜索
    67. {
    68. "query" : { // queryString
    69. "match" : { // 模糊匹配
    70. "desc" : "软件 系统" // desc字段中 包含 [软件] or [系统]
    71. },
    72. "size" : 20, // 指定单页返回20个(默认10)
    73. "from": 10 // 指定列表起始锚点(默认0)
    74. -----------------------------------
    75. "bool": { // desc字段中 包含 [软件] and [系统]
    76. "must": [
    77. { "match": { "desc": "软件" } },
    78. { "match": { "desc": "系统" } }
    79. ]
    80. }
    81. }
    82. }
    83. }'