SpringBoot - 检索Elasticsearch
概述
Elasticsearch是一个分布式搜索服务,提供Restful API,底层基于Lucene,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能
SpringBoot整合Elasticsearch
环境准备
-
Docker下载Elasticsearch镜像并启动容器
docker search elasticsearch elasticsearch启动时默认占用2G的堆内存空间,内存不够会启动失败,所以必须限制它的内存空间 9200web页面访问端口,9300elasticsearch分布式节点间通信端口 docker run -e ES_JAVA_OPTS "-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 elasticsearch
-
浏览器访问IP:9200显示如下页面即启动成功
-
创建项目,引入依赖如下
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/io.searchbox/jest --> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>6.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
配置application.properties
#使用jest和ES交互时需要配置访问的uri spring.elasticsearch.jest.uris=http://IP:9200 #使用SpringData Elasticsearch和ES交互时需要配置 #集群名字,web界面显示 spring.data.elasticsearch.cluster-name=docker-cluster #集群节点,注意是9300端口 spring.data.elasticsearch.cluster-nodes=IP:9300
说明
SpringBoot默认支持两种技术和Elasticsearch交互
- Jest(默认不生效)需要导入Jest的工具包:io.searchbox.client.JestClient
- SpringData Elasticsearch
- Client节点信息:clusterNodes和clusterName
- ElasticSearchTemplate 操作ES
- 编写一个ElasticSearchRepository的子接口来操作ES
测试和Elasticsearch交互
1.使用jestClient
@Autowired
JestClient jestClient;
@Test
public void testSave() {
//1.给ES中索引(保存)一个文档
Article article = new Article();
article.setId(1);
article.setAuthor("chenyu");
article.setTitle("Tell you how to brag");
article.setContent("puniangyi");
//构建一个索引功能
Index index = new Index.Builder(article).index("yubrother").type("666").build();
try {
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testSearch(){
// 查询表达式
String json =
"{\n"
+ " \"query\" : {\n"
+ " \"match\" : {\n"
+ " \"content\" : \"puniangyi\"\n"
+ " }\n"
+ " }\n"
+ "}";
//构建搜索功能
Search search = new Search.Builder(json).addIndex("yubrother").addType("666").build();
try {
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
2.使用ElasticsearchRepository的子接口
//ElasticsearchRepository支持jpa规范
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
public Book findByBookNameLike(String bookName);
}
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Test
public void test01(){
Book book = new Book();
book.setId(1);
book.setBookName("西游记");
book.setAuthor("吴承恩");
bookRepository.index(book);
}
@Test
public void test02(){
//Optional可以进行空值检测
Optional<Book> book = bookRepository.findById(1);
System.out.println(book);
}
3.使用ElasticsearchTemplate
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Test
public void test03(){
Book book = new Book();
book.setId(2);
book.setBookName("三国演义");
book.setAuthor("罗贯中");
IndexQuery indexQuery = new IndexQueryBuilder().withId((book.getId()).toString()).withObject(book).build();
elasticsearchTemplate.index(indexQuery);
}