Elasticsearch
Posted 默辨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch相关的知识,希望对你有一定的参考价值。
文章目录
该篇博文根据B站UP主遇见狂神说的课程——【狂神说Java】ElasticSearch7.6.x最新完整教程通俗易懂整理而出
10、集成Spring Boot
10.1、入门环境搭建
1、创建Spring Boot项目
2、选择依赖时,需要勾选Spring Date Elasticsearch依赖
3、修改配置
由于我们本地安装的ES版本为7.6.1,IDEA中的环境需要与之匹配,所以修改对应的版本信息,配置如图示:
4、创建配置类
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")));
return client;
}
}
注意:ES的核心配置类为RestClientAutoConfiguration,但其本质是RestClientConfigurations类
至此,基本的ES环境搭建已经完成,现在只需要 编写具体的业务测试类即可
10.2、新建索引
测试创建索引,
等同于直接使用命令:PUT mobian_test_api
测试代码:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
void createIndex() throws IOException {
// 1、创建索引请求
CreateIndexRequest request = new CreateIndexRequest("mobian_test_api");
// 2、客户端执行对应的请求IndicesClient ,请求后获得响应
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
}
测试结果:
使用Kibana查看我们创建的索引信息
10.3、删除索引
测试代码:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("mobian_test_api2");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete);
}
}
测试结果:
10.4、查找索引
判断对应索引是否存在
测试代码:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void getIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("mobian_test_api");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("mobian_test_api 索引是否存在:" + exists);
request = new GetIndexRequest("mobian_test_api3");
exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("mobian_test_api3 索引是否存在:" + exists);
}
}
测试结果:
获取对应的索引信息
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void findIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("mobian_test_api");
GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
System.out.println(getIndexResponse);
System.out.println(getIndexResponse.getIndices());
}
}
测试结果:
10.5、新建文档
由于涉及到对象和JSON字符串之间的转换,可以引入fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
测试代码:
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void addDocument() throws IOException {
// 1.创建对象
User user = new User("mobian", 4);
// 2.创建请求
IndexRequest request = new IndexRequest("mobian_test_api");
// 类比理解 put /mobian_test_api/_doc/1
request.id("1");
request.timeout("1s");
// 3.将我们的数据以json字符串的格式放入请求中
request.source(JSON.toJSONString(user), XContentType.JSON);
// 4.客户端发送对应的请求,获取具体的响应结果
IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
System.out.println(index.status());
}
}
测试结果:
10.6、删除文档
测试代码:
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
// 删除文档
@Test
public void deleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("mobian_test_api", "1");
DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(delete);
System.out.println(delete.status());
}
}
测试结果:
10.7、修改文档
测试代码:
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
// 修改文档
@Test
public void updateDocument() throws IOException {
// 构建新的对象
UpdateRequest updateRequest = new UpdateRequest("mobian_test_api", "1");
User user = new User("默辨", 18);
updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update);
System.out.println(update.status());
}
}
测试结果:
10.8、查找文档
判断对应的文档是否存在
测试代码:
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void findDocument() throws IOException {
GetRequest getRequest = new GetRequest("mobian_test_api", "1");
// 设置不返回 _source的上下文,该处不设置不影响查询结果
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println("mobian_test_api索引中是否存在文档为1的记录:" + exists);
getRequest = new GetRequest("mobian_test_api", "3");
exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println("mobian_test_api索引中是否存在文档为3的记录:" + exists);
}
}
测试结果:由于我们只添加了文档为1的记录,所以当记录的id为3时,查询状态对false。
查询具体的文档信息
测试代码:
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
// 查找对应的文档信息
@Test
public void getDocument() throws IOException {
GetRequest getRequest = new GetRequest("mobian_test_api", "1");
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
// 想要查询什么信息,直接调用对应的方法即可
System.out.println(documentFields.getSource());
//返回的全部内容和使用命令返回的全部内容是一样的
System.out.println(documentFields);
}
}
测试结果:
总结步骤:
1、封装我们想要完成操作的对象
2、使用调用RestHighLevelClient类中对应操作的方法
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
}
10.9、批量插入
测试代码:
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
// 批量新增文档
@Test
public void batchDocument() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> users = new ArrayList<>();
users.add(new User("mobian1", 22));
users.add(new User("mobian2", 22));
users.add(new User("mobian3", 22));
users.add(new User("mobian4", 22));
users.add(new User("mobian5", 22));
users.add(new User("mobian6", 22));
// 批量处理
for (int i = 0; i < users.size(); i++) {
// 如果想要完成修改操作,只需要修改该步即可
bulkRequest.add(new IndexRequest("mobian_test_api")
.id("" + (i + 1))
.source(JSON.toJSONString(users.get(i)), XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk);
System.out.println(bulk.status());
}
}
测试结果:
10.10、复杂查询
测试代码:
@SpringBootTest
public class DocumentApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
// 复杂条件查询
@Test
public void searchDocument() throws IOException {
SearchRequest searchRequest = new SearchRequest("mobian_test_api");
// 构建搜索条件,这里有点类似于MyBatis-Plus中的wrapper条件构造器
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 精确匹配
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "22");
// 匹配所有
// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
sourceBuilder.query(termQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 高亮
// sourceBuilder.highlighter();
// 分页
// sourceBuilder.from();
// sourceBuilder.size();
searchRequest.source(sourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(search.getHits()));
for (SearchHit documentFields : search.getHits().getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
}
测试结果:
11、项目实战
该部分主要分为三部分,爬取指定界面的数据,展示爬取出来的数据,高亮展示爬取出来的数据。
此处的案例:爬取京东商城以python为关键字的数据信息(标题、价格、图片),再获取ES索引库中对应的数据信息,并且完成关键字(python)的高亮显示。
11.1、环境搭建
1、创建一个Spring Boot项目
2、引入主要的maven依赖
<dependencies>
<!-- ES组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 热部署组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactIdElasticsearch笔记九之优化
使用标准库Ruby将数据标记到Elasticsearch批量中
Elasticsearch:如何在 Elasticsearch 中正确使用同义词功能