SpringMVC整合ElasticSearch

SpringMVC整合请参考, SpringMVC框架搭建

SpringMVC整合ElasticSearch步骤如下:

  1. 在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>
    
  2. 在applicationContext.xml文件中引入spring-elastic.xml

     <import resource="spring-elastic.xml"/>
    
  3. 创建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>
    
  4. 创建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;
     	}
     
     }
    
  5. 通过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);
         
     }
    
  6. 在Service层定义接口

     public interface EsService {
     	
     	public void createDocument(Stu stu);
     	
     	public Stu getByStuId(String stuId);
     }
    
  7. 定义接口实现

     @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);
     	}
     	
     }
    
  8. 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());
     	}
     	
     }
    
  9. 执行http://localhost:8080/elastic/es/elastic.do后,用浏览器输入http://192.168.0.116:9200/stu/_search?q=*&pretty即可查看数据已保存到Elastic

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值