Springboot整合Elasticsearch
Springboot使用ES的基操
一、使用NativeSearchQuery查询:
以下代码罗列了几种使用方式,可以通过组合实现其他复杂查询。
package com.chx.util; import com.alibaba.fastjson.JSONObject; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import java.util.ArrayList; import java.util.List; public class EsTest { @Autowired private ElasticsearchOperations elasticsearchOperations; public void test() { //设置查询的表名 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().withIndices("tableName"); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //filter相当于and,和must的主要区别在于不进行打分(_score) boolQuery.filter(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("columns", "QL"))); boolQuery.filter(QueryBuilders.termQuery("column", "00")); //查询时间区间的方式 boolQuery.filter(QueryBuilders.rangeQuery("time") .from("2020-11-10") .to("2020-12-20")); BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery(); //有时候会有需求,前端传过来list进行查询,所以要注意层级关系。用新的BoolQueryBuilder封装条件,最后放到最外层里,这样就组合成了where (xxx) and (xxx) or (xxx)这种形式 String[] flags = new String[]{"1", "2", "3", "4"}; for (String twoLabel : flags) { boolQuery2.should(QueryBuilders.termsQuery("twoLabel", twoLabel)); } //要查询出的列名,如果不设置则默认全部。 String[] columns = new String[]{"column1", "column2", "column3"}; //设置查询层级关系。组合查询条件 boolQuery.filter(boolQuery2); queryBuilder.withQuery(boolQuery); queryBuilder.withFields(columns); //设置排序条件 queryBuilder.withSort(SortBuilders.fieldSort("column_sort").order(SortOrder.ASC)); NativeSearchQuery build = queryBuilder.build(); //获取查询结果 SearchHits resultEs = elasticsearchOperations.query(build, response -> { return response.getHits(); }); SearchHit[] hits = resultEs.getHits(); List mktCustomStructList = new ArrayList<>(); for (SearchHit hit : hits) { //查询结果转换 String sourceAsString = JSONObject.parseObject(hit.getSourceAsString(), String.class); //如果要从hit里取,可以用hit.getSourceAsMap0.get("columns").toString()方法 } } }
二、ES条件查询的条数。
public void testCount(){ //设置查询的表名 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().withIndices("tableName"); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //filter相当于and boolQuery.filter(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("columns", "QL"))); boolQuery.filter(QueryBuilders.termQuery("column", "00")); //要查询出的列名,如果不设置则默认全部。 String[] columns = new String[]{"column1", "column2", "column3"}; //设置查询层级关系。组合查询条件 queryBuilder.withQuery(boolQuery); queryBuilder.withFields(columns); //设置排序条件 queryBuilder.withSort(SortBuilders.fieldSort("column_sort").order(SortOrder.ASC)); NativeSearchQuery build = queryBuilder.build(); long count = elasticsearchOperations.count(build); System.out.println(count); }
三、注意点:
1.我maven引入的es版本如下,三个包。如果不用count函数,elasticsearch-rest-high-level-client和elasticsearch不用引用。
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.8.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.8.0</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.8.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
2.一定要注意引入的版本对安装的ES客户端否支持,尽量引入的包要高于安装的ES版本。否则甚至会启动报错或执行错误(执行过程中报错或执行结果不正确)。
3.如果发生了exception is java.lang.NoSuchFieldError:IGNORE_DEPECATIONS的小伙伴,引入上述中的elasticsearch包即可解决。