本文最后更新于:1 年前
Elasticsearch(ES)
97-实用开发篇-ES简介11:07
https://www.bilibili.com/video/BV15b4y1a7yG/
源码:
https://github.com/elastic/elasticsearch
IK分词器:https://github.com/medcl/elasticsearch-analysis-ik
- Elasticsearch是一个分布式全文搜索引擎
先分词,然后根据关键字获取对应的数据的索引ID,倒排索引,创建文档,使用文档
1 2 3 4 5 6
| 验证:http://localhost:9200/
postman请求: 创建books索引:PUT http://localhost:9200/books 查询books索引:GET http://localhost:9200/books 删除books索引:DELETE http://localhost:9200/books
|
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
|
一、配置分词器
创建索引并指定规则
请求类型:PUT
请求地址:http://localhost:9200/books
请求体:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| { "mappings": { "properties": { "id": { "type": "keyword" }, "name": { "type": "text", "analyzer": "ik_max_word", "copy_to": "all" }, "type": { "type": "keyword" }, "description": { "type": "text", "analyzer": "ik_max_word", "copy_to": "all" }, "all": { "type": "text", "analyzer": "ik_max_word" } } } }
|
响应结果:
1 2 3 4 5
| { "acknowledged": true, "shards_acknowledged": true, "index": "books" }
|
二、创建文档
1. 添加数据
请求类型:PUT
请求地址:
生成随机id http://localhost:9200/books/_doc
生成指定id(请求体可不加id属性) http://localhost:9200/books/_doc/1
生成指定id http://localhost:9200/books/_create/1
请求体:
1 2 3 4 5 6
| { "id": 1, "name": "Spring 5设计模式", "type": "计算机理论", "description": "深入Spring源码剖析Spring源码中蕴含的10大设计模式" }
|
响应结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { "_index": "books", "_type": "_doc", "_id": "CSBqNokB3XgdIoLs0aEz", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
|
2. 查询数据
请求类型:GET
请求地址:http://localhost:9200/books/_doc/1
请求体:
响应结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "_index": "books", "_type": "_doc", "_id": "1", "_version": 1, "_seq_no": 1, "_primary_term": 1, "found": true, "_source": { "id": 1, "name": "Spring 5设计模式", "type": "计算机理论", "description": "深入Spring源码剖析Spring源码中蕴含的10大设计模式" } }
|
3. 查询全部
请求类型:GET
请求地址:http://localhost:9200/books/_search
请求体:
响应结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| { "took": 8, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "books", "_type": "_doc", "_id": "CSBqNokB3XgdIoLs0aEz", "_score": 1.0, "_source": { "id": 1, "name": "Spring 5设计模式", "type": "计算机理论", "description": "深入Spring源码剖析Spring源码中蕴含的10大设计模式" } }, { "_index": "books", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "id": 1, "name": "Spring 5设计模式", "type": "计算机理论", "description": "深入Spring源码剖析Spring源码中蕴含的10大设计模式" } } ] } }
|
4. 根据内容查询
请求类型:GET
请求地址:http://localhost:9200/books/_search?q=name:spring
请求体:
响应结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| { "took": 29, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 0.18232156, "hits": [ { "_index": "books", "_type": "_doc", "_id": "CSBqNokB3XgdIoLs0aEz", "_score": 0.18232156, "_source": { "id": 1, "name": "Spring 5设计模式", "type": "计算机理论", "description": "深入Spring源码剖析Spring源码中蕴含的10大设计模式" } }, { "_index": "books", "_type": "_doc", "_id": "1", "_score": 0.18232156, "_source": { "id": 1, "name": "Spring 5设计模式", "type": "计算机理论", "description": "深入Spring源码剖析Spring源码中蕴含的10大设计模式" } } ] } }
|
5. 删除数据
请求类型:DELETE
请求地址:http://localhost:9200/books/_doc/1
响应结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { "_index": "books", "_type": "_doc", "_id": "1", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
|
6. 更新数据
如果数据不存在,就会新增一条数据;如果数据存在,则会更新数据。全覆盖式更新。
请求类型:POST/PUT
请求地址:http://localhost:9200/books/_doc/1
请求体:
1 2 3
| { "name": "Spring 5设计模式222" }
|
响应结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { "_index": "books", "_type": "_doc", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 4, "_primary_term": 1 }
|
7. 更新数据非全覆盖式修改
这里的POST和PUT两中请求类型的区别:
POST:当数据一样时,并不会进行任何操作,“result” : “noop”,版本并不会新增
PUT:“result” : “updated”, //一直是更新,无论是否一致 版本都会新增
请求类型:POST/PUT
请求地址:http://localhost:9200/books/_update/1
请求体:
1 2 3 4 5
| { "doc": { "name": "Spring 5设计模式" } }
|
响应结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { "_index": "books", "_type": "_doc", "_id": "1", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 5, "_primary_term": 1 }
|