ElasticSearch
SQL:如果是大数据,搜索十分缓慢,需要建立索引
ElasticSearch:搜索(百度、淘宝……)
ES和Solr的比较
ElasticSearch简介
Solr简介
对比
- 当单纯地对已有数据进行搜索是,Solr更快
- 当实时建立索引是,Solr会产生io阻塞,查询性能较差,ElasticSearch具有明显优势
- 随着数据量的增加,Solr的搜索效率会变低,而ElasticSearch却没有明显的变化
ES vs Solr总结
ElasticSearch安装
Windows下
- 解压就可以使用
- 熟悉目录
- 启动
安装可视化界面
下载地址:https://github.com/mobz/elasticsearch-head
跨域配置:http.cors.enabled: true
http.cors.allow-origin: "*"
初学把索引当作数据库(可以建立索引,文档)
这个head把它当作数据展示工具,后面所有的查询Kibana做
Kibana安装
官网:https://www.elastic.co/cn/kibana/
Kibana版本要和ElasticSearch一致
好处:拆箱即用
访问测试:localhost:5601
开发工具
汉化:修改配置即可
IK分词器插件
什么是IK分词器
如果使用中文,建议使用IK分词器
安装
地址:https://github.com/medcl/elasticsearch-analysis-ik
elasticsearch-plugin list
查看安装的插件
查看不同分词效果
有些词需要我们手动添加到字典中
IK分词器增加自己的配置
IKAnalyzer.cfg.xml
Rest风格说明
基础测试
创建一个索引
1
2PUT /索引名/类型名/文档id
{请求体}完成了自动增加的索引,数据也成功添加了
指定字段的类型
通过
GET
获取具体信息查看默认信息
1
GET test3
如果自己的文档字段没有指定,ES会给我们默认配置字段类型
关于文档的基本操作(重点)
基本操作
添加数据
获取数据
1
GET kuangshen/user/1
更新数据 PUT
结果中version会改变
POST _update 推荐使用这种更新方式
简单的搜索
1 | GET kuangshen/user/1 |
简单的条件查询
1 | GET kuangshen/user/_search?q=name:xxxx |
复杂操作 select(排序、分页、高亮、模糊查询、精准查询)
https://www.bilibili.com/video/BV17a4y1x7zq?p=11&spm_id_from=pageDriver
集成SpringBoot
找文档
- 找到原生的依赖
- 找到对象
- 分析类中的方法
配置基本的项目
问题:一定要保证我们导入的依赖和ES版本一致
源码中的对象拿来即用
具体的API测试
创建索引
1
2
3
4
5void testCreate() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("1_index");
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}测试索引
1
2
3
4
5void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("1_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}删除索引
1
2
3
4
5void testDelete() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("1_index");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}创建文档
1
2
3
4
5
6
7
8
9
10void testAddDocuments() throws IOException {
User user = new User("dogegg", 3);
IndexRequest request = new IndexRequest("1_index");
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
System.out.println(index.status());
}判断文档是否存在
1
2
3
4
5
6
7void testIsExists() throws IOException {
GetRequest request = new GetRequest("1_index", "1");
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = client.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}获取文档信息
1
2
3
4
5
6void testGetDocument() throws IOException {
GetRequest request = new GetRequest("1_index", "1");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println(response);
}修改文档
1
2
3
4
5
6
7
8void testUpdateDocument() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("1_index", "1");
updateRequest.timeout("1s");
User user = new User("appp", 5);
updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}删除文档
1
2
3
4
5
6void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("1_index", "1");
request.timeout("1s");
DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
System.out.println(delete.status());
}批量插入文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19void testBulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> userList = new ArrayList<>();
userList.add(new User("1", 1));
userList.add(new User("2", 2));
userList.add(new User("3", 3));
userList.add(new User("4", 4));
for (int i = 0; i < userList.size(); i++) {
bulkRequest.add(new IndexRequest("1_index")
.id(""+(i+1))
.source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
}
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(response.hasFailures());
}