SSM整合elasticsearch的详细配置

时间: 2023-10-16 10:12:18 浏览: 35
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 的详细配置步骤。

相关推荐

最新推荐

recommend-type

SSM整合中的Log4j日志的配置详情

主要介绍了SSM整合中的Log4j的配置详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

使用SpringBoot整合ssm项目的实例详解

Spring Boot 现在已经成为 Java 开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成。这篇文章主要介绍了使用SpringBoot整合ssm项目,需要的朋友可以参考下
recommend-type

基于javaWeb 项目SSM配置要点及可能遇到的问题和解决方法

下面小编就为大家带来一篇基于javaWeb 项目SSM配置要点及可能遇到的问题和解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

SSM整合+火车票查询系统.docx

SSM框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,搭建的一个简单的火车票查询系统
recommend-type

ssm框架下web项目,web.xml配置文件的作用(详解)

下面小编就为大家带来一篇ssm框架下web项目,web.xml配置文件的作用(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。