文章目录
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的自动配置,如下图。
<dependency> <groupId>org.springframework.boot <dependency> <groupId>io.searchbox @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,Integer> { }
- 编写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<MyBook,Integer> { //只写方法声明,不用实现 public List<MyBook> 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