SSM整合elasticsearch的详细配置
时间: 2023-10-16 08:12:18 浏览: 89
SSM(Spring + SpringMVC + MyBatis)框架整合 Elasticsearch 可以实现高效的全文搜索功能。以下是详细配置步骤:
1. 引入 Elasticsearch 依赖
在 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.1</version>
</dependency>
```
2. 配置 Elasticsearch 连接
在 Spring 的配置文件(比如 applicationContext.xml)中添加以下配置:
```xml
<!-- 配置 Elasticsearch 连接 -->
<bean id="restHighLevelClient" class="org.elasticsearch.client.RestHighLevelClient">
<constructor-arg name="builder">
<bean class="org.elasticsearch.client.RestClient">
<constructor-arg name="builder">
<bean class="org.elasticsearch.client.RestClientBuilder">
<constructor-arg name="hosts">
<list>
<bean class="org.apache.http.HttpHost">
<constructor-arg name="hostname" value="localhost" />
<constructor-arg name="port" value="9200" />
<constructor-arg name="scheme" value="http" />
</bean>
</list>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
```
其中,`HttpHost` 的参数可以根据实际情况进行修改。
3. 编写 Elasticsearch 操作工具类
```java
@Component
public class ElasticsearchUtils {
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 创建索引
*
* @param indexName 索引名称
* @throws IOException
*/
public void createIndex(String indexName) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(indexName);
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
}
/**
* 删除索引
*
* @param indexName 索引名称
* @throws IOException
*/
public void deleteIndex(String indexName) throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
}
/**
* 添加文档
*
* @param indexName 索引名称
* @param id 文档 ID
* @param source 文档内容
* @throws IOException
*/
public void addDocument(String indexName, String id, Map<String, Object> source) throws IOException {
IndexRequest request = new IndexRequest(indexName).id(id).source(source);
restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
/**
* 删除文档
*
* @param indexName 索引名称
* @param id 文档 ID
* @throws IOException
*/
public void deleteDocument(String indexName, String id) throws IOException {
DeleteRequest request = new DeleteRequest(indexName).id(id);
restHighLevelClient.delete(request, RequestOptions.DEFAULT);
}
/**
* 更新文档
*
* @param indexName 索引名称
* @param id 文档 ID
* @param source 更新内容
* @throws IOException
*/
public void updateDocument(String indexName, String id, Map<String, Object> source) throws IOException {
UpdateRequest request = new UpdateRequest(indexName, id).doc(source);
restHighLevelClient.update(request, RequestOptions.DEFAULT);
}
/**
* 查询文档
*
* @param indexName 索引名称
* @param query 查询条件
* @return 查询结果
* @throws IOException
*/
public List<Map<String, Object>> searchDocuments(String indexName, QueryBuilder query) throws IOException {
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query);
request.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
List<Map<String, Object>> result = new ArrayList<>();
for (SearchHit hit : hits) {
result.add(hit.getSourceAsMap());
}
return result;
}
}
```
该工具类包含了 Elasticsearch 的基本操作,包括创建/删除索引、添加/删除/更新文档、查询文档等。
4. 编写 Elasticsearch 操作接口
```java
public interface ElasticsearchService {
void createIndex(String indexName) throws IOException;
void deleteIndex(String indexName) throws IOException;
void addDocument(String indexName, String id, Map<String, Object> source) throws IOException;
void deleteDocument(String indexName, String id) throws IOException;
void updateDocument(String indexName, String id, Map<String, Object> source) throws IOException;
List<Map<String, Object>> searchDocuments(String indexName, QueryBuilder query) throws IOException;
}
```
5. 实现 Elasticsearch 操作接口
```java
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {
@Autowired
private ElasticsearchUtils elasticsearchUtils;
@Override
public void createIndex(String indexName) throws IOException {
elasticsearchUtils.createIndex(indexName);
}
@Override
public void deleteIndex(String indexName) throws IOException {
elasticsearchUtils.deleteIndex(indexName);
}
@Override
public void addDocument(String indexName, String id, Map<String, Object> source) throws IOException {
elasticsearchUtils.addDocument(indexName, id, source);
}
@Override
public void deleteDocument(String indexName, String id) throws IOException {
elasticsearchUtils.deleteDocument(indexName, id);
}
@Override
public void updateDocument(String indexName, String id, Map<String, Object> source) throws IOException {
elasticsearchUtils.updateDocument(indexName, id, source);
}
@Override
public List<Map<String, Object>> searchDocuments(String indexName, QueryBuilder query) throws IOException {
return elasticsearchUtils.searchDocuments(indexName, query);
}
}
```
6. 使用 Elasticsearch
可以在任何需要使用 Elasticsearch 的地方注入 `ElasticsearchService` 接口,并调用相应的方法即可实现 Elasticsearch 相关操作。例如:
```java
@Autowired
private ElasticsearchService elasticsearchService;
public void testElasticsearch() throws IOException {
// 创建索引
elasticsearchService.createIndex("test_index");
// 添加文档
Map<String, Object> source = new HashMap<>();
source.put("title", "test title");
source.put("content", "test content");
elasticsearchService.addDocument("test_index", "1", source);
// 查询文档
QueryBuilder query = QueryBuilders.termQuery("title", "test");
List<Map<String, Object>> result = elasticsearchService.searchDocuments("test_index", query);
System.out.println(result);
// 删除文档
elasticsearchService.deleteDocument("test_index", "1");
// 删除索引
elasticsearchService.deleteIndex("test_index");
}
```
以上就是 SSM 框架整合 Elasticsearch 的详细配置步骤。
阅读全文