- ElasticsearchTemplate6.x + Springboot2.x
- Postman
目录
ElasticSearch集成Spring之ElasticsearchTemplate
spring-data-elasticsearch与elasticsearch的版本对应关系:
ElasticSearch集成Spring之ElasticsearchTemplate
maven依赖
<!-- elasticsearch的spring-data包 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>x.y.z.RELEASE</version> </dependency> <!-- elasticsearch客户端,version填入本地安装好的ES版本号 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>x.y.z</version> </dependency>
spring-data-elasticsearch与elasticsearch的版本对应关系:
spring data elasticsearch elasticsearch 3.2.x 6.5.0 3.1.x 6.2.2 3.0.x 5.5.0 2.1.x 2.4.0 2.0.x 2.2.0 1.3.x 1.5.2 补充:
:文档:
- 对照表:https://github.com/spring-projects/spring-data-elasticsearch/blob/master/src/main/asciidoc/preface.adoc
- http://xbib.org/elasticsearch/2.1.1/apidocs/org/elasticsearch/index/query/QueryBuilders.html
- https://docs.spring.io/spring-data/elasticsearch/docs/current/api/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.html
elasticsearchTemplate
引用
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
创建索引
// 1、直接用名称创建 elasticsearchTemplate.createIndex("book"); // 2、填入class对象 elasticsearchTemplate.createIndex(BookEntity.class);
插入单条数据
BookEntity bookEntity = new BookEntity(); bookEntity.setId("2"); bookEntity.setAuthor("老李"); bookEntity.setName("三年高考"); IndexQuery indexQuery = new IndexQueryBuilder() .withId(bookEntity.getId()) .withObject(bookEntity) .build(); elasticsearchTemplate.index(indexQuery);
插入多条数据
BookEntity bookEntity = new BookEntity(); bookEntity.setId("1"); bookEntity.setAuthor("测试1"); bookEntity.setName("测试1"); IndexQuery indexQuery1 = new IndexQueryBuilder() .withId(bookEntity.getId()) .withObject(bookEntity) .build(); // elasticsearchTemplate.index(indexQuery); indexQueries.add(indexQuery1); BookEntity bookEntity2 = new BookEntity(); bookEntity.setId("2"); bookEntity.setAuthor("测试2"); bookEntity.setName("测试2"); IndexQuery indexQuery2 = new IndexQueryBuilder() .withId(bookEntity2.getId()) .withObject(bookEntity) .build(); indexQueries.add(indexQuery2); elasticsearchTemplate.bulkIndex(indexQueries);
单字符串查询
SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.queryStringQuery("菜鸟")) .withPageable(new PageRequest(0, 20)) .build(); List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);
排序
Pageable pageable= new PageRequest(0, 20,new Sort(Sort.Direction.DESC, "name")); SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.queryStringQuery("菜鸟")) .withPageable(pageable) .build(); Page<BookEntity> list = elasticsearchTemplate.queryForPage(searchQuery, BookEntity.class);
模糊查询
Pageable pageable = new PageRequest(0, 10); SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.matchQuery("name", "菜鸟")) .withPageable(pageable) .build(); List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);
其余匹配
Pageable pageable = new PageRequest(0, 10); SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.matchPhraseQuery("name", "菜鸟")) .withPageable(pageable) .build(); List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);
Term全等查询
Pageable pageable = new PageRequest(0, 10); SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.termQuery("name", "菜鸟")) .withPageable(pageable) .build(); List<BookEntity> list = elasticsearchTemplate.queryForList(searchQuery, BookEntity.class);
GetQuery根据id查询
GetQuery getQuery = GetQuery.getById(String.valueOf(admin.getId())); Admin admin = template.queryForObject(getQuery, Admin.class);
RangeQuery范围查询
- 对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRangeQuery
- 范围查询接受以下参数:
- gte: 大于或等于
- gt: 大于
- lte: 小于或等于
- lt: 小于
- boost: 设置查询的提升值,默认为1.0
- format(String format) 对于日期字段,我们可以设置要使用的格式,而不是映射器格式
- timeZone 时区
- from 范围查询的from部分
RangeQueryBuilder createTime = QueryBuilders.rangeQuery("createTime").from(questions.getStartTime()).to(questions.getEndTime()); boolQueryBuilder.should(createTime);
组合查询
即boolQuery,可以设置多个条件的查询方式。它的作用是用来组合多个Query,有四种方式来组合,must,mustnot,filter,should。
must代表返回的文档必须满足must子句的条件,会参与计算分值;
filter代表返回的文档必须满足filter子句的条件,但不会参与计算分值;
should代表返回的文档可能满足should子句的条件,也可能不满足,有多个should时满足任何一个就可以,通过minimum_should_match设置至少满足几个。
mustnot代表必须不满足子句的条件。QueryBuilder filterQuery = QueryBuilders .boolQuery() .filter(QueryBuilders.termQuery("name", "菜鸟")) .filter(QueryBuilders.termQuery("author", "小菜")); List<BookEntity> list = elasticsearchTemplate.queryForList(filterQuery, BookEntity.class);
组合查询举例:should
NativeSearchQueryBuilder query = new NativeSearchQueryBuilder(); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id", "b31ea3a48f0f48738ecbc4a25534d6c6"); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "你好"); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.should(matchQueryBuilder); // or boolQueryBuilder.should(termQueryBuilder); query.withQuery(boolQueryBuilder);
ElasticSearch操作-Postman
创建索引:
PUT http://ip:port/indexname
查看索引下的数据:
POST http://ip:port/indexname/indextype/_search?pretty
查询所有索引:
GET http://ip:port/_cat/indices?v
删除索引:
DELETE http://ip:port/indexname
删除多个:/index_one,index_two /index_*
ik分词索引:
POST http://ip:port/user_index/_analyze
查看索引数据结构
GET http://ip:port/index_name/_mapping
插入数据
POST http://ip:port/index/type/id
http://192.168.0.148:9200/user_index/user/9ebcb330919b4baa9403a7564cfb1e4a
删除索引下所有数据
POST http://0.0.0.0:9200/索引/标签/_delete_by_query?pretty
{ "query": { "match_all": {} } }
其他用到再补充
Elasticsearch搜索引擎 - Springboot+ElasticSearchTemplate使用方法总结和用Postman操作ES
最新推荐文章于 2024-09-21 21:30:33 发布