springboot2整合elasticsearch7,并生成elasticsearch工具类和生成调用例子
时间: 2023-08-11 22:34:18 浏览: 118
好的,我可以为您提供相关的信息和代码示例。首先,您需要确保您的Spring Boot项目中包含以下依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.0.0</version>
</dependency>
```
接下来,您需要在application.properties文件中添加以下配置:
```properties
spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=localhost:9300
```
然后,您可以编写Elasticsearch工具类来执行一些常见的操作,例如索引文档、搜索文档等。以下是一个示例工具类:
```java
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
@Component
public class ElasticsearchUtil {
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 索引文档
* @param indexName 索引名称
* @param id 文档ID
* @param sourceMap 文档内容
* @throws IOException
*/
public void indexDocument(String indexName, String id, Map<String, Object> sourceMap) throws IOException {
IndexRequest request = new IndexRequest(indexName);
if (id != null) {
request.id(id);
}
request.source(sourceMap);
restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
/**
* 更新文档
* @param indexName 索引名称
* @param id 文档ID
* @param sourceMap 文档内容
* @throws IOException
*/
public void updateDocument(String indexName, String id, Map<String, Object> sourceMap) throws IOException {
UpdateRequest request = new UpdateRequest(indexName, id);
request.doc(sourceMap);
restHighLevelClient.update(request, RequestOptions.DEFAULT);
}
/**
* 批量索引文档
* @param indexName 索引名称
* @param sourceMaps 文档内容列表
* @throws IOException
*/
public void bulkIndexDocuments(String indexName, Map<String, Object>[] sourceMaps) throws IOException {
BulkRequest request = new BulkRequest();
for (Map<String, Object> sourceMap : sourceMaps) {
IndexRequest indexRequest = new IndexRequest(indexName);
indexRequest.source(sourceMap);
request.add(indexRequest);
}
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
}
/**
* 批量更新文档
* @param indexName 索引名称
* @param idList 文档ID列表
* @param sourceMap 文档内容
* @throws IOException
*/
public void bulkUpdateDocuments(String indexName, String[] idList, Map<String, Object> sourceMap) throws IOException {
BulkRequest request = new BulkRequest();
for (String id : idList) {
UpdateRequest updateRequest = new UpdateRequest(indexName, id);
updateRequest.doc(sourceMap);
request.add(updateRequest);
}
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
}
/**
* 获取文档
* @param indexName 索引名称
* @param id 文档ID
* @return
* @throws IOException
*/
public Map<String, Object> getDocument(String indexName, String id) throws IOException {
GetRequest request = new GetRequest(indexName, id);
return restHighLevelClient.get(request, RequestOptions.DEFAULT).getSourceAsMap();
}
/**
* 搜索文档
* @param indexName 索引名称
* @param keyword 关键词
* @param size 每页数量
* @param from 起始位置
* @return
* @throws IOException
*/
public SearchHits searchDocuments(String indexName, String keyword, int size, int from) throws IOException {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("content", keyword));
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);
searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse.getHits();
}
}
```
最后,您可以在您的代码中使用ElasticsearchUtil类,并根据您的需求调用它的方法来执行所需的操作。以下是一个示例调用:
```java
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RestController
public class DemoController {
@Autowired
private ElasticsearchUtil elasticsearchUtil;
@GetMapping("/index")
public String index() throws IOException {
Map<String, Object> sourceMap = new HashMap<>();
sourceMap.put("title", "Elasticsearch 7 教程");
sourceMap.put("content", "Elasticsearch 7 入门教程");
elasticsearchUtil.indexDocument("my_index", null, sourceMap);
return "索引文档成功!";
}
@GetMapping("/search")
public String search(@RequestParam("keyword") String keyword,
@RequestParam("size") int size,
@RequestParam("from") int from) throws IOException {
SearchHits searchHits = elasticsearchUtil.searchDocuments("my_index", keyword, size, from);
return searchHits.toString();
}
}
```
希望这些代码示例可以帮助您整合Elasticsearch 7到您的Spring Boot项目中,并提供了一些常见的操作示例。
阅读全文