文章目录
- 5.SpringBoot集成ElasticSearch7.x
- 1.添加依赖
- 2.yml配置
- 3.创建文档对象
- 4.继承ElasticsearchRepository
- 5.注入ElasticsearchRestTemplate
- 6.SpringBoot操作ElasticSearch
- 1.ElasticsearchRestTemplate索引操作
- 2.ElasticsearchRepository文档操作
- 3.ElasticsearchRestTemplate文档操作
- 4.ElasticsearchRestTemplate数据检索
- 完整测试代码
5.SpringBoot集成ElasticSearch7.x
1.添加依赖
本文使用springboot版本为2.7
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>
spring官网es对应版本关系 https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/versions.html
2.yml配置
spring:data:elasticsearch:repositories:enabled: true #打开elasticsearch仓库,默认trueelasticsearch:#username:#password:#path-prefix:uris: http://127.0.0.1:9200connection-timeout: 60000 #连接elasticsearch超时时间socket-timeout: 30000
3.创建文档对象
/*** @author moshangshang* createIndex默认为true自动创建索引*/
@Data
@AllArgsConstructor
@Document(indexName ="books",createIndex = true)
public class Books implements Serializable {@Idprivate Integer id;//指定字段类型和分词器@Field(type = FieldType.Text, analyzer = "ik_max_word")private String name;@Field(type = FieldType.Text, analyzer = "ik_max_word")private String describe;}
public enum FieldType {Text,Integer,Long,Date,Float,Double,Boolean,Object,Auto,Nested,Ip,Attachment,Keyword
}
@GeoPointField //地理位置类型字段
private GeoPoint location;
@Field(index = false) //不进行索引
4.继承ElasticsearchRepository
/*** @author moshangshang*/
public interface BookMapper extends ElasticsearchRepository<Books,String> {
}
自定义接口查询方法
List<Book> findByNameAndPrice(String name, Integer price);
等同于
{"query": {"bool" : {"must" : [{ "query_string" : { "query" : "?", "fields" : [ "name" ] } },{ "query_string" : { "query" : "?", "fields" : [ "price" ] } }]}}
}
详细接口方法命名见官方文档:
https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/repositories/elasticsearch-repository-queries.html
5.注入ElasticsearchRestTemplate
@Autowiredprivate ElasticsearchRestTemplate elasticsearchRestTemplate;
建议通过elasticsearchRestTemplate进行es操作,ElasticsearchRepository也可直接操作,但多数方法已过时,不建议用
6.SpringBoot操作ElasticSearch
1.ElasticsearchRestTemplate索引操作
/*** 操作文档索引* 如果文档的createIndex设置为true自动创建,则可以直接创建映射会自动创建索引* 如果为false,直接创建映射会返回Document的json,但索引并未创建*/@GetMapping("/create/index")public Document createIndex(){// 获取操作的索引文档对象IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Books.class);//判断索引是否存在log.info("exists:{}",indexOperations.exists());//删除索引文档log.info("delete:{}",indexOperations.delete());//创建索引,如果存在则会抛异常log.info("create:{}",indexOperations.create());//创建映射,返回Documentlog.info("createMapping:{}",indexOperations.createMapping());indexOperations.delete();//创建索引同时创建映射,如果存在索引则会抛异常log.info("createWithMapping{}",indexOperations.createWithMapping());//更新映射mappinglog.info("putMapping:{}",indexOperations.putMapping());// 配置映射return indexOperations.createMapping();}
2.ElasticsearchRepository文档操作
/*** 发送保存数据,会自动创建索引文档并保存数据* 如果文档的createIndex设置为false,该方法会创建索引*/@GetMapping("/data/{id}")public String data(@PathVariable Integer id){//新增数据,等同于PUT/POST方式发送数据bookMapper.save(new Books(id,"四大名著西游记111","著名的神话小说"));//查所有数据bookMapper.findAll().forEach(System.out::println);bookMapper.findAllById(Arrays.asList("1", "10")).forEach(System.out::println);bookMapper.findById(id.toString()).ifPresent(System.out::println);//查所有数据按id排序bookMapper.findAll(Sort.by("id")).forEach(System.out::println);//删除指定id数据bookMapper.deleteById(String.valueOf(id));//分页查询Page<Books> booksPage = bookMapper.findAll(Pageable.ofSize(2));//效果等同于booksPage2.getContent().forEach()log.info("总页数:{}",booksPage.getTotalPages());System.out.println("第1页");booksPage.forEach(System.out::println);int i = 1;//判断是否有下一页,hasPrevious方法判断是否有前一页while (booksPage.hasNext()){i++;booksPage = bookMapper.findAll(booksPage.nextPageable());log.info("第{}页",i);booksPage.forEach(System.out::println);}return "success";}
3.ElasticsearchRestTemplate文档操作
注意:直接执行可能会删除时更新操作还未执行完,全部删除时会导致更新的数据还在
@GetMapping("/data1/{id}")public UpdateResponse data1(@PathVariable Integer id){//添加 elasticsearchRestTemplate.save(new Books(id,"四大名著西游记111","著名的神话小说"));elasticsearchRestTemplate.save(new Books(1,"四大名著西游记111","著名的神话小说"),new Books(2,"四大名著西游记111","著名的神话小说"),new Books(3,"四大名著西游记111","著名的神话小说"));/*** 修改 跟新增是相同。若id已存在,覆盖其他所有字段,* 若某个字段没有值,则为null。无法修改单个字段*/elasticsearchRestTemplate.save(new Books(3,"四大名著西游记222",null));//修改部分字段Document document = Document.create();document.put("name", "三国演义");document.put("describe", "著名的小说");//需要修改的id和参数UpdateQuery updateQuery = UpdateQuery.builder(String.valueOf(id)).withDocument(document).build();UpdateResponse response = elasticsearchRestTemplate.update(updateQuery, IndexCoordinates.of("books"));//批量修改部分字段List<UpdateQuery> updateQueryList = new ArrayList<>();for (int i = 1; i <= 3; i++) {Document bulkDocument = Document.create();bulkDocument.put("name", "三国演义"+i);bulkDocument.put("describe", "著名的小说"+i);//索引i数据不存在会报错UpdateQuery updateQueryBulk = UpdateQuery.builder(Long.toString(i)).withDocument(bulkDocument).build();updateQueryList.add(updateQueryBulk);}elasticsearchRestTemplate.bulkUpdate(updateQueryList, IndexCoordinates.of("books"));//查询Books books = elasticsearchRestTemplate.get(id.toString(), Books.class, IndexCoordinates.of("books"));log.info("查询数据:{}",books);//删除String delete = elasticsearchRestTemplate.delete(id.toString(), Books.class);log.info("删除数据id:{}",delete);//条件删除NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("name", "三国")).build();ByQueryResponse queryResponse = elasticsearchRestTemplate.delete(nativeSearchQuery, Books.class, IndexCoordinates.of("books"));log.info("删除queryResponse数据数量:{}",queryResponse.getDeleted());//全部删除//直接执行可能会删除时更新操作还未执行完,全部删除时会导致更新的数据还在ByQueryResponse queryResponse1 = elasticsearchRestTemplate.delete(new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery()).build(),Books.class, IndexCoordinates.of("books"));log.info("删除queryResponse1数据数量:{}",queryResponse1.getDeleted());return response;}
4.ElasticsearchRestTemplate数据检索
基础高亮查询
@GetMapping("/query")public void query() {// 构建查询条件(NativeSearchQueryBuilder更接近原生查询)NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();//查询queryBuilder.withQuery(QueryBuilders.matchQuery("username","lisi")).withSort(Sort.by("age").ascending());//高亮查询HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field("username");//多个匹配项高亮关闭highlightBuilder.requireFieldMatch(false);highlightBuilder.preTags("<span style='color:red'>");highlightBuilder.postTags("</span>");//设置高亮queryBuilder.withHighlightBuilder(highlightBuilder);SearchHits<News> searchHits = elasticsearchRestTemplate.search(queryBuilder.build(), News.class);searchHits.forEach(e -> {log.info("全文检索{}", e.getContent());Map<String, List<String>> highlightFields = e.getHighlightFields();List<String> list = highlightFields.get("username");if (list != null) {list.forEach(ex -> log.info("高亮{}", ex));}});}
NativeSearchQueryBuilder条件构建
// 构建查询条件(NativeSearchQueryBuilder更接近原生查询)NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); //查询全部 match_allqueryBuilder.withQuery(QueryBuilders.matchAllQuery()); //查询全部 过滤时间范围queryBuilder.withQuery(QueryBuilders.matchAllQuery()).withFilter(QueryBuilders.rangeQuery("time").timeZone("+08:00").format("yyyy-MM-dd HH:mm:ss").gt("2024-06-27 14:22:22").lt("2024-06-27 14:22:28"));//查询全部 match_all 并按age升序queryBuilder.withQuery(QueryBuilders.matchAllQuery()).withSort(Sort.by("age").ascending());//查询全部 match_all 并按age升序 并分页queryBuilder.withQuery(QueryBuilders.matchAllQuery()).withSort(Sort.by("age").ascending()).withPageable(PageRequest.of(2, 10));//term精确查询,整体匹配// 查询内容只是为数字时 推荐使用term 进行检索,但是当使用term中进行文本内容的全量检索时term不会检索任何内容queryBuilder.withQuery(QueryBuilders.termQuery("age", "2"));//多匹配值查询queryBuilder.withQuery(QueryBuilders.termsQuery("age", "2", "3"));//全文检索 match_queryqueryBuilder.withQuery(QueryBuilders.matchQuery("address", "Beijing"));//or 只要有一个词存在则就符合条件,and表示每个词都需存在queryBuilder.withQuery(QueryBuilders.matchQuery("address", "Beijing shanghai").operator((Operator.OR)));//短语匹配 match_phrasequeryBuilder.withQuery(QueryBuilders.matchPhraseQuery("address", "Beijing aaa"));//短语匹配 match_phrase_prefix 它允许文本中最后一项使用前缀匹配queryBuilder.withQuery(QueryBuilders.matchPhrasePrefixQuery("address", "Beijing a"));//多字段匹配 multi_matchqueryBuilder.withQuery(QueryBuilders.multiMatchQuery("Beijing aaa", "address", "username"));queryBuilder.withQuery(QueryBuilders.multiMatchQuery("Beijing aaa", "address", "username")//提升该字段查询匹配权重.field("address", 10));//match_bool_prefix 允许文本中最后一项使用前缀匹配 其它都是term query。queryBuilder.withQuery(QueryBuilders.matchBoolPrefixQuery("address", "b"));//id查询queryBuilder.withQuery(QueryBuilders.idsQuery().addIds("1", "2"));//区间查询queryBuilder.withQuery(QueryBuilders.rangeQuery("age").gte(2).lte(3));//bool复合查询//must必须 mustNot必须不//should 条件可以满足也可以不满足,在查询中如果有满足should的条件就会增加相关性得分QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("address", "Beijing")).must(QueryBuilders.matchQuery("username", "lisi")).mustNot(QueryBuilders.matchQuery("age", "1")).should(QueryBuilders.matchQuery("age", "2"))//minimumShouldMatch 最小匹配度,必须 匹配的should子句的数量或百分比。.minimumShouldMatch("50%");//filter结果过滤QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("age").gte(1).lte(2));//与上面结果相等QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("age").gte(1).lte(2));
完整测试代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.0</version></parent><groupId>com.example</groupId><artifactId>test</artifactId><version>0.0.1-SNAPSHOT</version><name>test</name><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
/*** @author moshangshang*/
@Data
@AllArgsConstructor
@Document(indexName ="news")
public class News implements Serializable {@Idprivate Integer id;@Field(type = FieldType.Text, analyzer = "ik_max_word")private String address;@Field(type = FieldType.Text, analyzer = "ik_max_word")private String username;@Field(type = FieldType.Long)private Integer age;
}
/*** @author moshangshang*/
@Data
@AllArgsConstructor
@Document(indexName ="books")
public class Books implements Serializable {@Idprivate Integer id;@Field(type = FieldType.Text, analyzer = "ik_max_word")private String name;@Field(type = FieldType.Text, analyzer = "ik_max_word")private String describe;}
/*** @author moshangshang*/
public interface BookMapper extends ElasticsearchRepository<Books,String> {
}
/*** @author moshangshang*/
@Slf4j
@RestController
@RequestMapping("/es")
public class BookController {@Autowiredprivate BookMapper bookMapper;@Autowiredprivate ElasticsearchRestTemplate elasticsearchRestTemplate;/*** 操作文档索引* 如果文档的createIndex设置为true自动创建,则可以直接创建映射会自动创建索引* 如果为false,直接创建映射会返回Document的json,但索引并未创建*/@GetMapping("/create/index")public Document createIndex() {// 获取操作的索引文档对象IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Books.class);//判断索引是否存在log.info("exists:{}", indexOperations.exists());//删除索引文档log.info("delete:{}", indexOperations.delete());//创建索引,如果存在则会抛异常log.info("create:{}", indexOperations.create());//创建映射,返回Documentlog.info("createMapping:{}", indexOperations.createMapping());indexOperations.delete();//创建索引同时创建映射,如果存在索引则会抛异常log.info("createWithMapping{}", indexOperations.createWithMapping());//更新映射mappinglog.info("putMapping:{}", indexOperations.putMapping());// 配置映射return indexOperations.createMapping();}/*** 发送保存数据,会自动创建索引文档并保存数据* 如果文档的createIndex设置为false,该方法会创建索引*/@GetMapping("/data/{id}")public String data(@PathVariable Integer id) {//新增数据bookMapper.save(new Books(id, "四大名著西游记111", "著名的神话小说"));//查所有数据bookMapper.findAll().forEach(System.out::println);bookMapper.findAllById(Arrays.asList("1", "10")).forEach(System.out::println);bookMapper.findById(id.toString()).ifPresent(System.out::println);//查所有数据按id排序bookMapper.findAll(Sort.by("id")).forEach(System.out::println);//删除指定id数据//bookMapper.deleteById(String.valueOf(id));//删除匹配的数据bookMapper.delete(new Books(null, "四大名著西游记111", "著名的神话小说"));//删除所有数据//bookMapper.deleteAll();//分页查询Page<Books> booksPage = bookMapper.findAll(Pageable.ofSize(2));//效果等同于booksPage2.getContent().forEach()log.info("总页数:{}", booksPage.getTotalPages());System.out.println("第1页");booksPage.forEach(System.out::println);int i = 1;//判断是否有下一页,hasPrevious方法判断是否有前一页while (booksPage.hasNext()) {i++;booksPage = bookMapper.findAll(booksPage.nextPageable());log.info("第{}页", i);booksPage.forEach(System.out::println);}return "success";}@GetMapping("/data1/{id}")public UpdateResponse data1(@PathVariable Integer id) {//添加elasticsearchRestTemplate.save(new Books(id, "四大名著西游记111", "著名的神话小说"));elasticsearchRestTemplate.save(new Books(1, "四大名著西游记111", "著名的神话小说"),new Books(2, "四大名著西游记111", "著名的神话小说"),new Books(3, "四大名著西游记111", "著名的神话小说"));/*** 修改 跟新增是相同。若id已存在,覆盖其他所有字段,* 若某个字段没有值,则为null。无法修改单个字段*/elasticsearchRestTemplate.save(new Books(3, "四大名著西游记222", null));//修改部分字段Document document = Document.create();document.put("name", "三国演义");document.put("describe", "著名的小说");//需要修改的id和参数UpdateQuery updateQuery = UpdateQuery.builder(String.valueOf(id)).withDocument(document).build();UpdateResponse response = elasticsearchRestTemplate.update(updateQuery, IndexCoordinates.of("books"));//批量修改部分字段List<UpdateQuery> updateQueryList = new ArrayList<>();for (int i = 1; i <= 3; i++) {Document bulkDocument = Document.create();bulkDocument.put("name", "三国演义" + i);bulkDocument.put("describe", "著名的小说" + i);//索引i数据不存在会报错UpdateQuery updateQueryBulk = UpdateQuery.builder(Long.toString(i)).withDocument(bulkDocument).build();updateQueryList.add(updateQueryBulk);}elasticsearchRestTemplate.bulkUpdate(updateQueryList, IndexCoordinates.of("books"));//查询Books books = elasticsearchRestTemplate.get(id.toString(), Books.class, IndexCoordinates.of("books"));log.info("查询数据:{}", books);//删除String delete = elasticsearchRestTemplate.delete(id.toString(), Books.class);log.info("删除数据id:{}", delete);//条件删除NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("name", "三国")).build();ByQueryResponse queryResponse = elasticsearchRestTemplate.delete(nativeSearchQuery, Books.class, IndexCoordinates.of("books"));log.info("删除queryResponse数据数量:{}", queryResponse.getDeleted());//全部删除//直接执行可能会删除时更新操作还未执行完,全部删除时会导致更新的数据还在ByQueryResponse queryResponse1 = elasticsearchRestTemplate.delete(new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery()).build(),Books.class, IndexCoordinates.of("books"));log.info("删除queryResponse1数据数量:{}", queryResponse1.getDeleted());return response;}@GetMapping("/query")public void query() {// 构建查询条件(NativeSearchQueryBuilder更接近原生查询)NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();//查询全部 match_allqueryBuilder.withQuery(QueryBuilders.matchAllQuery());//查询全部 过滤时间范围queryBuilder.withQuery(QueryBuilders.matchAllQuery()).withFilter(QueryBuilders.rangeQuery("time").timeZone("+08:00").format("yyyy-MM-dd HH:mm:ss").gt("2024-06-27 14:22:22").lt("2024-06-27 14:22:28"));//查询全部 match_all 并按age升序queryBuilder.withQuery(QueryBuilders.matchAllQuery()).withSort(Sort.by("age").ascending());//查询全部 match_all 并按age升序 并分页queryBuilder.withQuery(QueryBuilders.matchAllQuery()).withSort(Sort.by("age").ascending()).withPageable(PageRequest.of(2, 10));//term精确查询,整体匹配// 查询内容只是为数字时 推荐使用term 进行检索,但是当使用term中进行文本内容的全量检索时term不会检索任何内容queryBuilder.withQuery(QueryBuilders.termQuery("age", "2"));//多匹配值查询queryBuilder.withQuery(QueryBuilders.termsQuery("age", "2", "3"));//全文检索 match_queryqueryBuilder.withQuery(QueryBuilders.matchQuery("address", "Beijing"));//or 只要有一个词存在则就符合条件,and表示每个词都需存在queryBuilder.withQuery(QueryBuilders.matchQuery("address", "Beijing shanghai").operator((Operator.OR)));//短语匹配 match_phrasequeryBuilder.withQuery(QueryBuilders.matchPhraseQuery("address", "Beijing aaa"));//短语匹配 match_phrase_prefix 它允许文本中最后一项使用前缀匹配queryBuilder.withQuery(QueryBuilders.matchPhrasePrefixQuery("address", "Beijing a"));//多字段匹配 multi_matchqueryBuilder.withQuery(QueryBuilders.multiMatchQuery("Beijing aaa", "address", "username"));queryBuilder.withQuery(QueryBuilders.multiMatchQuery("Beijing aaa", "address", "username")//提升该字段查询匹配权重.field("address", 10));//match_bool_prefix 允许文本中最后一项使用前缀匹配 其它都是term query。queryBuilder.withQuery(QueryBuilders.matchBoolPrefixQuery("address", "b"));//id查询queryBuilder.withQuery(QueryBuilders.idsQuery().addIds("1", "2"));//区间查询queryBuilder.withQuery(QueryBuilders.rangeQuery("age").gte(2).lte(3));//bool复合查询//must必须 mustNot必须不//should 条件可以满足也可以不满足,在查询中如果有满足should的条件就会增加相关性得分QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("address", "Beijing")).must(QueryBuilders.matchQuery("username", "lisi")).mustNot(QueryBuilders.matchQuery("age", "1")).should(QueryBuilders.matchQuery("age", "2"))//minimumShouldMatch 最小匹配度,必须 匹配的should子句的数量或百分比。.minimumShouldMatch("50%");//filter结果过滤QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("age").gte(1).lte(2));//与上面结果相等QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("age").gte(1).lte(2));//查询queryBuilder.withQuery(QueryBuilders.matchQuery("username","lisi")).withSort(Sort.by("age").ascending());//高亮查询HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field("username");//多个匹配项高亮关闭highlightBuilder.requireFieldMatch(false);highlightBuilder.preTags("<span style='color:red'>");highlightBuilder.postTags("</span>");//设置高亮queryBuilder.withHighlightBuilder(highlightBuilder);SearchHits<News> searchHits = elasticsearchRestTemplate.search(queryBuilder.build(), News.class);searchHits.forEach(e -> {log.info("全文检索{}", e.getContent());Map<String, List<String>> highlightFields = e.getHighlightFields();List<String> list = highlightFields.get("username");if (list != null) {list.forEach(ex -> log.info("高亮{}", ex));}});}}