文章目录
1. 创建SpringBoot工程
1.1 选择ElasticSearch
- 1. 创建SpringBoot工程
- 1.1 选择ElasticSearch
- 1.2 配置ElasticSearch
- 1.3 小节
- 2. 测试ElasticSearch
- 2.1 使用Jest操作ElasticSearch
- 2.2 使用SpringData操作ElasticSearch
- 3. 小结
创建SpringBoot
工程,选择ElasticSearch
在application.properties
文件中配置ElasticSearch
。
注意:在构建完工程后,打开项目的pom.xml
文件,可以发现有如下的依赖。因此,可以看出SpringBoot
默认使用的是SpringData
的ElasticSearch
模块进行操作的。可以进入来确认一下,打开SpringBoot
的autoConfigure
包,看一下ElasticSearch
的自动配置,如下图。
org.springframework.boot
spring-boot-starter-data-elasticsearch
SpringBoot
默认支持两种技术来和ElasticSearch
交互:
Jest
(默认不生效),需要导入jest的工具包(io.searchbox.client.JestClient)
SpringData
的ElasticSearch
模块(默认方式)
- 去
Maven
中央仓库搜索Jest依赖,本文选择如下版本
io.searchbox
jest
5.3.3
- 在
application.properties
配置文件中,做如下配置
spring.elasticsearch.jest.uris=http://192.168.1.108:9200
- 编写测试代码,其中
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();
}
}
}
-
测试结果
在浏览器中输入如下地址,可以查看存入ElasticSearch
中的数据:http://192.168.1.108:9200/scorpios/article/1
- 从
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();
}
}
}
-
测试结果
- 打开
pom.xml
中的依赖 application.properties
配置文件中添加如下配置:
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.1.108:9200
- 编写
BookRepository
接口
public interface BookRepository extends ElasticsearchRepository {
}
- 编写
MyBook
类,注意@Document
注解
@Document(indexName ="scorpios",type = "book")
public class MyBook {
private Integer id;
private String name;
private String author;
......
}
- 测试代码:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {
@Autowired
BookRepository bookRepository;
@Test
public void test02(){
MyBook myBook = new MyBook(1, "红楼梦", "曹雪芹");
bookRepository.index(myBook);
}
}
- 搜索代码
public interface BookRepository extends ElasticsearchRepository {
//只写方法声明,不用实现
public List findByBookNameLike(String bookName);
}
- 使用
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {
@Autowired
BookRepository bookRepository;
@Test
public void test02(){
for(MyBook book:bookRepository.findByBookNameLike("西游记")){
System.out.println(book);
}
}
}
3. 小结
本文使用了两种方式来操作ElasticSearch
,SpringBoot
默认使用的是SpringData
里的模块。上面只是简单的使用,具体深入的学习,可以参考ElasticSearchd
的官方文档。
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html