1.环境准备
Linux中使用docker安装ELK详解
Elastic_search官方文档
2. springboot集成elasticsearch
2.1 导入pom.xml依赖
根据自己es版本选择相应的依赖maven版本
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.6.0</elasticsearch.version>
</properties>
<dependencies>
<!-- SpringBoot与elasticsearch整合的相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
2.2 编写es配置
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
));
return client;
}
}
2.3 编写服务类
public interface EsService {
void createIndex(String indexName) throws IOException;
boolean existsIndex(String indexName) throws IOException;
void delIndex(String indexName) throws IOException;
void getDocument(String indexName,String docName) throws IOException;
void addDocument(String indexName,String docName, Object obj) throws IOException;
void updateDocment(String indexName,String docName, Object obj) throws IOException;
void deleteDocment(String indexName,String docName) throws IOException;
void search(String indexName, TermQueryBuilder query) throws IOException;
void searchHighLight(String indexName, TermQueryBuilder query) throws IOException;
}
@Service
public class EsServiceImpl implements EsService {
private RestHighLevelClient restHighLevelClient;
public EsServiceImpl(RestHighLevelClient restHighLevelClient) {
this.restHighLevelClient = restHighLevelClient;
}
@Override
public void createIndex(String indexName) throws IOException {
// 1. 创建索引请求
CreateIndexRequest firstIndex = new CreateIndexRequest(indexName);
// 2. 客户端执行创建索引的请求
CreateIndexResponse response = restHighLevelClient.indices().create(firstIndex, RequestOptions.DEFAULT);
System.out.println(response);
}
@Override
public void delIndex(String indexName) throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete);
}
@Override
public boolean existsIndex(String indexName) throws IOException {
// 1. 创建一个get请求获取指定索引的信息
GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
// 2. 客户端执行请求判断索引是否存在
return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
}
@Override
public void addDocument(String indexName,String docName, Object obj) throws IOException {
// 2. 创建请求并指定索引
IndexRequest indexRequest = new IndexRequest(indexName);
// 3. 创建的规则:put /indexName/_doc/docName
// 设置ID
indexRequest.id(docName);
// 设置超时时间
indexRequest.timeout(TimeValue.timeValueSeconds(1));
// 4. 将数据放入到请求中
indexRequest.source(JSON.toJSONString(obj), XContentType.JSON);
// 5. 使用客户端发送请求
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(index));
}
@Override
public void updateDocment(String indexName,String docName, Object obj) throws IOException {
// 1. 创建一个更新请求的信息,绑定索引名称和需要更新的文档ID
UpdateRequest updateRequest = new UpdateRequest(indexName, docName);
// 设置超时时间
updateRequest.timeout(TimeValue.timeValueSeconds(1));
// 2. 封装需要更新的文档信息
updateRequest.doc(JSON.toJSONString(obj), XContentType.JSON);
// 3. 使用update更新文档
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(updateResponse));
}
@Override
public void deleteDocment(String indexName, String docName) throws IOException {
// 1. 创建一个删除的请求,绑定索引名和需要删除的文档ID
DeleteRequest deleteRequest = new DeleteRequest(indexName, docName);
// 2. 发起删除请求
DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response));
}
@Override
public void getDocument(String indexName, String docName) throws IOException {
GetRequest getRequest = new GetRequest(indexName,docName);
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
System.out.println(documentFields);
}
@Override
public void search(String indexName,TermQueryBuilder query) throws IOException {
// 1. 创建批量搜索请求,并绑定索引
SearchRequest searchRequest = new SearchRequest(indexName);
// 2. 构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(query).timeout(TimeValue.timeValueSeconds(60));
// 3. 将查询条件放入搜索请求request中
searchRequest.source(sourceBuilder);
// 4. 发起查询请求获取数据
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response));
System.out.println(JSON.toJSONString(response.getHits().getHits()));
}
@Override
public void searchHighLight(String indexName, TermQueryBuilder query) throws IOException {
// 1. 创建批量搜索请求,并绑定索引
SearchRequest searchRequest = new SearchRequest(indexName);
// 2. 构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 文字高亮
sourceBuilder.highlighter();
sourceBuilder.query(query).timeout(TimeValue.timeValueSeconds(60));
// 3. 将查询条件放入搜索请求request中
searchRequest.source(sourceBuilder);
// 4. 发起查询请求获取数据
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
}
}
2.4 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
class ElasticsearchDemoApplicationTests {
@Autowired
private EsService esService;
@Test
void createIndex() throws IOException {
esService.createIndex("yx-0176");
}
@Test
void existsIndex() throws IOException {
esService.existsIndex("yx-0176");
}
@Test
void delIndex() throws IOException {
esService.delIndex("yx-0176");
}
@Test
void addDocument() throws IOException {
Map map = new HashMap();
map.put("name","zs");
map.put("age","20");
esService.addDocument("yx-0176","test",map);
}
@Test
void updateDocment() throws IOException {
Map map = new HashMap();
map.put("name","zs");
map.put("age","21");
esService.updateDocment("yx-0176","test",map);
}
@Test
void deleteDocment() throws IOException {
esService.deleteDocment("yx-0176","test");
}
@Test
void getDocment() throws IOException {
esService.getDocument("yx-0176","test");
}
@Test
void search() throws IOException {
// 【注意:这里有个坑,当中文查询时,如果使用ik分词器会查询不到数据,属性需要使用xxx.keyword才能查询到数据】
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name.keyword", "zs");
// QueryBuilders.matchAllQuery();
esService.search("yx-0176",termQueryBuilder);
esService.searchHighLight("yx-0176",termQueryBuilder);
}
}