SpringMVC整合请参考, SpringMVC框架搭建
SpringMVC整合ElasticSearch步骤如下:
-
在maven引入Elastic jar包,在pom.xml中添加如下内容:
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.2.2</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>6.2.2</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.2.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.3.RELEASE</version> </dependency>
-
在applicationContext.xml文件中引入spring-elastic.xml
<import resource="spring-elastic.xml"/>
-
创建spring-elastic.xml文件,配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.elastic" /> <!-- 必须指向存放repository文件的包 --> <elasticsearch:repositories base-package="com.climber.elastic.repository" /> <!-- 配置Client --> <elasticsearch:transport-client id="client" cluster-nodes="192.168.0.116:9300"/> <!-- 配置搜索模板 --> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client" /> </bean> </beans>
-
创建Bean
创建Stu,具体代码如下:
package com.climber.elastic; import java.io.Serializable; import java.util.Date; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; /** * index:是否设置索引, store是否存储数据,type:数据类型,analyzer:分词粒度选择,searchAnalyzer:查询进行分词处理 * ik_smart:进行最小粒度分词,ik_max_word进行最大粒度分词 * @author Derlin * */ @Document(indexName = "stu", type = "doc") public class Stu implements Serializable { private static final long serialVersionUID = 1L; @Id @Field(index=true, store = true, type = FieldType.Long) private Long id; @Field(index = true, analyzer = "ik_max_word", store = true, searchAnalyzer = "ik_smart", type = FieldType.Text) private String stuId; @Field(index = true, analyzer = "ik_max_word", store = true, searchAnalyzer = "ik_smart", type = FieldType.Text) private String stuName; @Field(index = true, store = true, type = FieldType.Date) private Date createTime; @Field(index = true, analyzer = "ik_max_word", store = true, searchAnalyzer = "ik_smart", type = FieldType.Text) private String sex; public Stu() { } public Stu(Long id, String stuId, String stuName, Date createTime) { this.id = id; this.stuId = stuId; this.stuName = stuName; this.createTime = createTime; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getStuId() { return stuId; } public void setStuId(String stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
-
通过Repository进行CURD,定义Repository如下
@Repository public interface StudentRepository extends ElasticsearchRepository<Stu, Long> { /** * @param stuId * @return */ Stu getByStuId(String stuId); /** * @param stuName * @return */ List<Stu> getListByStuName(String stuName); /** * @param stuName * @param pageable * @return */ Page<Stu> getPageByStuName(String stuName, Pageable pageable); }
-
在Service层定义接口
public interface EsService { public void createDocument(Stu stu); public Stu getByStuId(String stuId); }
-
定义接口实现
@Service("esService") public class EsServiceImp implements EsService { @Resource private StudentRepository studentRepository; @Override public void createDocument(Stu stu) { studentRepository.save(stu); } @Override public Stu getByStuId(String stuId) { return studentRepository.getByStuId(stuId); } }
-
Controller调用如下
@Controller @RequestMapping("/es") public class EsController { @Resource private EsService esService; @RequestMapping("/elastic") public void elastic(){ Stu st = new Stu(6L, "006", "小陈", new Date()); esService.createDocument(st); Stu stu = esService.getByStuId("006"); System.out.println(stu.getStuName()); } }
-
执行http://localhost:8080/elastic/es/elastic.do后,用浏览器输入http://192.168.0.116:9200/stu/_search?q=*&pretty即可查看数据已保存到Elastic