我使用的是springboot的2.3.5.RELEASE版本和Elasticsearch的7.6.0版本,这里贴出Elasticsearch的依赖,其它的依赖按照自己的需求自行添加。
<!-- elasticsearch start -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.0</version>
</dependency>
<!-- elasticsearch end -->
配置RestHighLevelClient,我这里写死了,实际需求中HttpHost的构造参数,我们会写在配置文件中,然后多环境根据读取配置文件来设值
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")));
return client;
}
}
大概写了一些常用的方法,很简单的使用一下,还有很多功能暂未探索
@Api(tags = {"es基础服务"})
@RequestMapping("/es")
@RestController
public class EsBaseController {
@Autowired
private RestHighLevelClient restHighLevelClient;
@ApiOperation(value="创建索引")
@GetMapping(value = "/creatIndex")
public ResponseVo creatIndex(String indexName) throws IOException {
//创建索引请求
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
//创建执行请求
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
return ResponseVo.success(createIndexResponse);
}
@ApiOperation(value="获取索引")
@GetMapping(value = "/getIndex")
public ResponseVo getIndex(String indexName) throws IOException {
GetIndexRequest getIndexRequest =new GetIndexRequest(indexName);
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
return ResponseVo.success(exists);
}
@ApiOperation(value="删除索引")
@GetMapping(value = "/deleteIndex")
public ResponseVo deleteIndex(String indexName) throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
return ResponseVo.success(delete.isAcknowledged());
}
@ApiOperation(value="添加文档信息")
@PostMapping(value = "/addDocument")
public ResponseVo addDocument(@RequestBody BookDto dto) throws IOException {
Book book = dto;
IndexRequest indexRequest = new IndexRequest(dto.getIndexName());
indexRequest.id(book.getId());
indexRequest.timeout(TimeValue.timeValueSeconds(1));
indexRequest.timeout("1s");
indexRequest.source(JSON.toJSONString(book), XContentType.JSON);
//发送请求
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
return ResponseVo.success(indexResponse);
}
@ApiOperation(value="获取文档信息")
@GetMapping(value = "/getDocument")
public ResponseVo getDocument(String indexName, String id) throws IOException {
GetRequest getRequest = new GetRequest(indexName, id);
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
if(exists){
//获取文档信息
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
return ResponseVo.success(documentFields);
}else {
return ResponseVo.success();
}
}
@ApiOperation(value="更新文档信息")
@PostMapping(value = "/updateDocument")
public ResponseVo updateDocument(@RequestBody BookDto dto) throws IOException {
Book book = dto;
UpdateRequest updateRequest =new UpdateRequest(dto.getIndexName(), dto.getId());
updateRequest.timeout("1s");
updateRequest.doc(JSON.toJSONString(book),XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
return ResponseVo.success(update);
}
@ApiOperation(value="删除文档信息")
@GetMapping(value = "/deleteDocument")
public ResponseVo deleteDocument(String indexName, String id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
return ResponseVo.success(deleteResponse);
}
@ApiOperation(value="批量插入文档信息")
@PostMapping(value = "/batchInsertDocument")
public ResponseVo batchInsertDocument(@RequestBody BookBatchDto batchDto) throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
if (batchDto.getBooks() != null && batchDto.getBooks().size() > 0){
for (Book book:batchDto.getBooks()) {
bulkRequest.add(new IndexRequest(batchDto.getIndexName()).id(book.getId()).source(JSON.toJSONString(book),XContentType.JSON));
}
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
return ResponseVo.success(bulk);
}
@ApiOperation(value="查询文档信息")
@PostMapping(value = "/searchDocumen")
public ResponseVo searchDocumen(@RequestBody BookDto dto) throws IOException {
SearchRequest searchRequest = new SearchRequest(dto.getIndexName());
//构建搜索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//QueryBuilders.matchAllQuery查询所有
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(matchAllQueryBuilder);
//构建分页
searchSourceBuilder.from();
searchSourceBuilder.size();
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
List<Map<String, Object>> list = new ArrayList<>();
for (SearchHit searchHit : search.getHits().getHits()) {
list.add(searchHit.getSourceAsMap());
}
return ResponseVo.success(list);
}
}
定义的几个用来负载数据的实体Book,BookDto,BookBatchDto,关于ResponseVo只是一个自定义的响应实体,自行定义就好了,之前的文章中也有贴过
@Data
public class Book {
private String id;
private String title;
private BigDecimal price;
}
======================================================
@Data
public class BookDto extends Book {
private String indexName;
}
=======================================================
@Data
public class BookBatchDto {
private String indexName;
private List<Book> books;
}