:::info 为什么要用es? ::: Elastic 的底层是开源库 Lucene。但是,你没法直接用 Lucene,必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装,提供了 REST API 的操作接口,开箱即用。
:::info 数据管理分级? :::
- Cluster(比对mysql)
- Node(比对mysql)
- Index(索引组,类似于mysql单个表)
- Type(记录分组,逻辑分组)
- Document(单条记录)
:::info 使用方式? :::
curl -X GET 'http://localhost:9200/_cat/indices?v' // 查看当前节点的所有 Indexcurl 'localhost:9200/_mapping?pretty=true' // 列出每个 Index 所包含的 Typecurl -X PUT 'localhost:9200/weather' // 新建一个名叫weather的 Indexcurl -X DELETE 'localhost:9200/weather' // 删除这个 Index令:index=accounts/type=personcurl -X PUT 'localhost:9200/accounts/person/10001' -d '{"user": "李四","title": "工程师","desc": "系统管理"}' // 新增带ID记录curl -X POST 'localhost:9200/accounts/person' -d '{"user": "李四","title": "工程师","desc": "系统管理"}' // 新增无ID记录curl -X DELETE 'localhost:9200/accounts/person/1' // 删除记录返回:{"_index":"accounts","_type":"person","_id":"AV3qGfrC6jMbsbXb6k1p", // POST返回随机字符串"_version":1, // PUT更新后版本递增"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true // 执行结果}curl 'localhost:9200/accounts/person/1?pretty=true' // 查看指定ID记录{"_index" : "accounts","_type" : "person","_id" : "1","_version" : 1,"found" : true, // 搜索结果"_source" : { // 原始记录"user" : "张三","title" : "工程师","desc" : "数据库管理"}}curl 'localhost:9200/accounts/person/_search' // 返回type下所有记录{"took":2, // 耗时"timed_out":false, // 是否超时"_shards":{"total":5,"successful":5,"failed":0}, // 执行节点信息"hits":{ // 命中信息"total":2, // 总量"max_score":1.0, // 最高匹配度"hits":[ // 命中列表{"_index":"accounts","_type":"person","_id":"AV3qGfrC6jMbsbXb6k1p","_score":1.0, // 匹配度(最大为1)"_source": { // 原记录"user": "李四","title": "工程师","desc": "系统管理"}},{"_index":"accounts","_type":"person","_id":"1","_score":1.0,"_source": {"user" : "张三","title" : "工程师","desc" : "数据库管理,软件开发"}}]}}curl 'localhost:9200/accounts/person/_search' -d ' // 带参搜索{"query" : { // queryString"match" : { // 模糊匹配"desc" : "软件 系统" // desc字段中 包含 [软件] or [系统]},"size" : 20, // 指定单页返回20个(默认10)"from": 10 // 指定列表起始锚点(默认0)-----------------------------------"bool": { // desc字段中 包含 [软件] and [系统]"must": [{ "match": { "desc": "软件" } },{ "match": { "desc": "系统" } }]}}}}'
