Spring Boot与检索(ElasticSearch)
[Elasticsearch: 权威指南中文文档]https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
检索
- 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选。他可以快速的存储、搜索和分析海量数据。Spring Boot通过整合Spring Data ElasticSearch为我们提供了非常便捷的检索功能支持;
- Elasticsearch是一个分布式搜索服务,提供Restful API,底层基于Lucene,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,github等大型的站点也是采用了ElasticSearch作为其搜索服务,
Spring Boot整合ElasticSearch测试
- springboot默认支持两种方式与ES交互
- Jest(默认不生效)- 需要导入jest的工具包(io.searchbox.client;)
- SpringData ElasticSearch
Jest
-
导入依赖
<dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>6.3.0</version> </dependency>
-
属性配置
@ConfigurationProperties(prefix = "spring.elasticsearch.jest") public class JestProperties { /** * Comma-separated list of the Elasticsearch instances to use. */ private List<String> uris = new ArrayList<>(Collections.singletonList("http://localhost:9200"));
spring.elasticsearch.jest.uris默认使用
http://localhost:9200
,修改为自己的地址spring.elasticsearch.jest.uris=http://192.168.11.220:9200
-
测试
Student.java
public class Student { @JestId Integer id; String name; String desc; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; } }
test
import cn.net.credit.been.Student; import io.searchbox.client.JestClient; import io.searchbox.core.DocumentResult; import io.searchbox.core.Index; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import java.io.IOException; @SpringBootTest class SpringbootElasticApplicationTests { @Resource JestClient jestClient; @Test public void testJest() { // 给ES people索引Student类型中保存一个文档 Student s1 = new Student(); s1.setId(1); s1.setName("zhangsan"); s1.setDesc("xxxxxxxx"); // 构建一个索引功能 Index build = new Index.Builder(s1).index("people").type("Student").build(); try { jestClient.execute(build); } catch (IOException e) { e.printStackTrace(); } } }
{ _index: "people", _type: "Student", _id: "1", _version: 1, _seq_no: 0, _primary_term: 1, found: true, _source: { id: 1, name: "zhangsan", desc: "xxxxxxxx" } }
SpringData ElasticSearch
Client 节点信息 clusterNodes,clusterName
ElasticsearchTemplate操作ES
ElasticsearchRepository的子接口操作ES
-
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
-
属性配置
spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=192.168.11.220:9300
-
测试
Teacher.java
@Document(indexName = "p", type = "Teacher") public class Teacher { Integer id; String name; String desc; @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
TeacherRepository.java
import cn.net.credit.been.Teacher; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface TeacherRepository extends ElasticsearchRepository<Teacher, Integer> { }
test
package cn.net.credit; import cn.net.credit.Repository.TeacherRepository; import cn.net.credit.been.Student; import cn.net.credit.been.Teacher; import io.searchbox.client.JestClient; import io.searchbox.core.DocumentResult; import io.searchbox.core.Index; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import java.io.IOException; @SpringBootTest class SpringbootElasticApplicationTests { @Resource TeacherRepository teacherRepository; @Test public void testRepository() { Teacher t1 = new Teacher(); t1.setId(1); t1.setName("lisi"); t1.setDesc("yyyyyyy"); teacherRepository.index(t1); } }
{ _index: "p", _type: "Teacher", _id: "1", _version: 1, _seq_no: 0, _primary_term: 1, found: true, _source: { id: 1, name: "lisi", desc: "yyyyyyy" } }