一、创建SpringBoot工程并配置ElasticSearch
1、创建SpringBoot工程,选择ElasticSearch
2、在application.properties文件中配置ElasticSearch
注意:在构建完工程后,打开项目的pom.xml文件,可以发现有如下的依赖。因此,可以看出SpringBoot默认使用的是SpringData的ElasticSearch模块进行操作的。我们可以进入来确认一下,打开SpringBoot的autoConfigure包,看一下ElasticSearch的自动配置,如下图。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
3、小节
SpringBoot默认支持两种技术来和ElasticSearch交互:
(1)、Jest(默认不生效),需要导入jest的工具包(io.searchbox.client.JestClient)
(2)、SpringData的ElasticSearch模块(默认方式)
二、测试ElasticSearch
1、测试使用Jest的ElasticSearch
(1)、去Maven中央仓库搜索Jest依赖,本文选择如下版本;
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
(2)、在application.properties配置文件中,做如下配置;
spring.elasticsearch.jest.uris=http://192.168.1.108:9200
(3)、编写测试代码,其中Article类的id属性要加@JestId注解,如下:
public class Article {
@JestId
private int id;
......
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {
@Autowired
JestClient jestClient;
@Test
public void contextLoads() {
Article article = new Article(1, "西游记", "吴承恩", "师徒四人西天取经");
//构建一个索引功能:在scorpios索引下,类型为article
Index build = new Index.Builder(article).index("scorpios").type("article").build();
try {
jestClient.execute(build);
} catch (IOException e) {
e.printStackTrace();
}
}
}
(4)、测试结果:
在浏览器中输入如下地址,可以查看存入ElasticSearch中的数据:http://192.168.1.108:9200/scorpios/article/1
(5)、从ElasticSearch中获取数据的测试代码:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {
@Autowired
JestClient jestClient;
@Test
public void testSearch(){
//查询条件可参考ElasticSearch的官方文档
String json ="{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"取经\"\n" +
" }\n" +
" }\n" +
"}";
//构建搜索
Search build = new Search.Builder(json).addIndex("scorpios").addType("article").build();
try {
SearchResult result = jestClient.execute(build);
System.out.println("搜索结果:"+result.getJsonString());
System.out.println("搜索结果:"+result.getSourceAsString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
(6)、测试结果:
2、测试使用SpringData的ElasticSearch
(1)、打开pom.xml中的依赖
(2)、application.propertis配置文件中添加如下配置:
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.1.108:9200
(3)、编写BookRepository接口
public interface BookRepository extends ElasticsearchRepository<MyBook,Integer> {
}
(4)、编写MyBook类,注意@Document注解
@Document(indexName ="scorpios",type = "book")
public class MyBook {
private Integer id;
private String name;
private String author;
......
}
(5)、测试代码:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {
@Autowired
BookRepository bookRepository;
@Test
public void test02(){
MyBook myBook = new MyBook(1, "红楼梦", "曹雪芹");
bookRepository.index(myBook);
}
}
(6)、搜索代码:
public interface BookRepository extends ElasticsearchRepository<MyBook,Integer> {
//只写方法声明,不用实现
public List<MyBook> findByBookNameLike(String bookName);
}
(7)、使用
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {
@Autowired
BookRepository bookRepository;
@Test
public void test02(){
for(MyBook book:bookRepository.findByBookNameLike("西游记")){
System.out.println(book);
}
}
}
三、小结
本文使用了两种方式来操作ElasticSearch,SpringBoot默认使用的是SpringData里的模块。上面只是简单的使用,具体深入的学习,可以参考ElasticSearchd的官方文档。
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html