1-新建工程

springboot检索之使用JestClient整合elasticsearch测试_搜索

2-选中web,elasticsearch模块

springboot检索之使用JestClient整合elasticsearch测试_elasticsearch_02

springboot检索之使用JestClient整合elasticsearch测试_spring_03

springboot默认使用spring data elasticsearch模块进行操作

3-

springboot检索之使用JestClient整合elasticsearch测试_elasticsearch_04

springboot检索之使用JestClient整合elasticsearch测试_spring_05

springboot默认支持两种技术来和es交互

   Jest(默认不生效),需要导入jest的工具包(io.searchbox.client.JestClient)

   springdata elasticsearch

springboot检索之使用JestClient整合elasticsearch测试_elasticsearch_06

client节点信息  clusterNodes,clusterName

ElasticsearchTemplate操作elasticsearch

编写一个ElasticsearchRepository的子接口来操作elasticsearch

springboot检索之使用JestClient整合elasticsearch测试_spring_07

springboot检索之使用JestClient整合elasticsearch测试_搜索_08

4-来到maven中央仓库 搜索jest

springboot检索之使用JestClient整合elasticsearch测试_spring_09

springboot检索之使用JestClient整合elasticsearch测试_spring_10

选择6版本的

<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.0</version>
</dependency>

修改pom.xml文件

springboot检索之使用JestClient整合elasticsearch测试_搜索_11

5-编写application.properties

spring.elasticsearch.jest.uris=http://192.168.3.18:9200

springboot检索之使用JestClient整合elasticsearch测试_elasticsearch_12

6-启动,连接池连到9200端口

springboot检索之使用JestClient整合elasticsearch测试_搜索_13

7-新建bean包 Article类

package com.example.springbootelasticsearch.bean;

import io.searchbox.annotations.JestId;

public class Article {

@JestId //标记主键
private Integer id;

private String author;
private String title;
private String content;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}

8-编写测试类

package com.example.springbootelasticsearch;

import com.example.springbootelasticsearch.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class SpringbootElasticsearchApplicationTests {

@Autowired
JestClient jestClient;

@Test
void contextLoads() {
//给elasticsearch中索引(保存)一个文档
Article article = new Article();
article.setId(1);
article.setTitle("好消息");
article.setAuthor("zhangsan");
article.setContent("hello world");

//构建一个索引功能,不能大小写混着写
//Index index = new Index.Builder(article).index("testElasticSearch").type("news").id("id").build();
//Index index = new Index.Builder(article).index("testElasticSearch").type("news").build();
Index index = new Index.Builder(article).index("test").type("news").build();

try {
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}

}

}

9-运行测试类

查看所有索引

springboot检索之使用JestClient整合elasticsearch测试_搜索_14

查询结果

springboot检索之使用JestClient整合elasticsearch测试_elasticsearch_15

10-测试类中新增

//测试搜索
@Test
public void search(){
//查询表达式,修改属性值为article的属性content
String queryString="{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"hello\"\n" +
" }\n" +
" }\n" +
"}";

//构建搜索功能
Search search = new Search.Builder(queryString).addIndex("test").addType("news").build();

//执行
try {
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}

springboot检索之使用JestClient整合elasticsearch测试_spring_16

运行测试方法,打印出

2019-11-29 17:39:08.817  INFO 9940 --- [           main] .SpringbootElasticsearchApplicationTests : Started SpringbootElasticsearchApplicationTests in 2.741 seconds (JVM running for 4.002)
{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"test","_type":"news","_id":"1","_score":0.2876821,"_source":{"id":1,"author":"zhangsan","title":"好消息","content":"hello world"}}]}}
2019-11-29 17:39:09.086 INFO 9940 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'

 11-查看官方文档

springboot检索之使用JestClient整合elasticsearch测试_搜索_17

springboot检索之使用JestClient整合elasticsearch测试_elasticsearch_18

springboot检索之使用JestClient整合elasticsearch测试_搜索_19