SpringMVC整合ElasticSearch


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"/>
1
創建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
————————————————
版權聲明:本文為CSDN博主「derlinchen」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/derlinchen/article/details/85692525


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM