1.创建springboot项目
Pom文件引入elasticsearch依赖
1<dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 4</dependency>
2. yml配置文件添加elasticsearch依赖
1#elasticsearch 配置 2spring: 3 data: 4 elasticsearch: 5 cluster-name: elasticsearch 6 cluster-nodes: 127.0.0.1:9300 7 repositories: 8 enabled: true
3. 创建Document 实体类和对应repository
实体类
1@Data 2@Document(indexName = "book",type = "_doc") 3public class BookBean { 4 5 @Id 6 private String id; 7 private String title; 8 private String author; 9 private String postDate; 10 11 public BookBean(){} 12 13 public BookBean(String id, String title, String author, String postDate){ 14 this.id=id; 15 this.title=title; 16 this.author=author; 17 this.postDate=postDate; 18 } 19 20 @Override 21 public String toString() { 22 return "BookBean{" + 23 "id='" + id + '\'' + 24 ", title='" + title + '\'' + 25 ", author='" + author + '\'' + 26 ", postDate='" + postDate + '\'' + 27 '}'; 28 } 29}
对应repository
1public interface BookRepository extends ElasticsearchRepository<BookBean,String> { 2 3}
4.创建测试类
1@RunWith(SpringRunner.class) 2@SpringBootTest 3public class TestEs { 4 5 @Autowired 6 private ElasticsearchTemplate elasticsearchTemplate; 7 8 @Autowired 9 private BookRepository bookRepository; 10 11*/ 12 /** 13 * 创建索引 14 */ 15 @Test 16 public void createIndex() { 17 // 创建索引,会根据BookBean类的@Document注解信息来创建 18 elasticsearchTemplate.createIndex(BookBean.class); 19 // 配置映射,会根据Item类中的id、Field等字段来自动完成映射 20 elasticsearchTemplate.putMapping(BookBean.class); 21 } 22 23 /** 24 * 删除索引 25 */ 26 @Test 27 public void deleteBookIndex() { 28 elasticsearchTemplate.deleteIndex("book"); 29 } 30 31 /** 32 * 删除所有 33 */ 34 @Test 35 public void delete() { 36 bookRepository.deleteAll(); 37 } 38 39 /** 40 * 新增 41 */ 42 @Test 43 public void insert() { 44 BookBean book = new BookBean("2", "ES教程", "程裕强", "2019-10-01"); 45 bookRepository.save(book); 46 } 47 /** 48 * 批量新增 49 */ 50 @Test 51 public void insertList() { 52 List<BookBean> list = new ArrayList<>(); 53 list.add(new BookBean("3", "ES教程", "程裕强", "2019-10-01")); 54 list.add(new BookBean("4", "ES教程", "程裕强", "2019-10-01")); 55 list.add(new BookBean("5", "ES教程", "程裕强", "2019-10-01")); 56 list.add(new BookBean("6", "ES教程", "程裕强", "2019-10-01")); 57 //接收对象集合,实现批量新增 58 bookRepository.saveAll(list); 59 } 60 61 /** 62 * 多字段匹配同一内容 63 * title、author 匹配同一内容 “ES教程” 64 */ 65 @Test 66 public void multiMatchQuery() { 67 68 QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("ES教程", "title", "author"); 69 Iterable<BookBean> search = bookRepository.search(queryBuilder); 70 for (BookBean bo : search) { 71 System.out.println(bo); 72 } 73 } 74 75 /** 76 * 多字段查询,成功 77 */ 78 @Test 79 public void matchQuery() { 80 JSONObject map = new JSONObject(); 81 map.put("title", "ES教程"); 82 map.put("author", "程裕强"); 83 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 84 for (String in : map.keySet()) { 85 String str = map.getString(in); 86 boolQueryBuilder.should(QueryBuilders.matchQuery(in, str)); 87 } 88 int pageSize = 10; 89 int pagenum = 1; 90 // 第几页,页面大小 91 Pageable pageable = PageRequest.of(pagenum, pageSize); 92 93 //构建查询 94 NativeSearchQuery build = new NativeSearchQueryBuilder() 95 .withPageable(pageable) 96 .withQuery(boolQueryBuilder) 97 .build(); 98 //返回查询结果 99 Page<BookBean> search = bossRepository.search(build); 100 System.out.println(search.getTotalElements()); 101 System.out.println(search.getTotalPages()); 102 search.forEach(System.out::println); 103 } 104 105 106 107 108 109}

ES快速构建java项目会放在git上