1、查询基本步骤
1、创建SearchRequest对象
2、准备Request.source(),也就是DSL。
QueryBuilders来构建查询条件
传入Request.source() 的 query() 方法
3、发送请求,得到结果
4、解析结果(参考JSON结果,从外到内,逐层解析)
RestAPI中其中构建DSL是通过HighLevelRestClient中的resource()返回的SearchSourceBuilder来实现的,其中包含了查询、排序、分页、高亮等所有功能
SearchSourceBuilder 进行查询、排序、分页、高亮
RestAPI中其中构建查询条件的核心部分是由一个名为QueryBuilders的工具类提供的,其中包含了各种查询方法。
QueryBuilders 进行match_all /match /multi_match /term /range/boolmatch等查询
查询所有
@Testpublic void testMatchAll() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("case_management");//构建查询条件,下边是所有查询MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();//将查询条件赋值给查询语句request.source().query(matchAllQueryBuilder);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);}
2、多种检索查询
match、multi_match查询
@Testpublic void testMatchQuery() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("case_management");//构建查询条件,下边是所有查询MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("fileId","赎吧");//将查询条件赋值给查询语句request.source().query(queryBuilder);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);}
@Testpublic void testMultiMatchQuery() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("case_management");//构建查询条件,下边是所有查询MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("B端赎吧","fileId","caseDes");//将查询条件赋值给查询语句request.source().query(queryBuilder);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);}
term 、range查询
@Testpublic void testTermQuery() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("case_management");//构建查询条件,下边是所有查询TermQueryBuilder queryBuilder = QueryBuilders.termQuery("oa","lilan04");//将查询条件赋值给查询语句request.source().query(queryBuilder);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);}@Testpublic void testRangeQuery() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("test_1");//构建查询条件,下边是所有查询RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age").gte(10).lte(20);//将查询条件赋值给查询语句request.source().query(queryBuilder);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);}
布尔查询
@Testpublic void testBooleanQuery() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("case_management");//构建查询条件,下边是所有查询BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));//将查询条件赋值给查询语句request.source().query(queryBuilder);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);}
分页、排序、高亮查询
@Testpublic void testSortQuery() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("case_management");//构建查询条件,下边是布尔查询BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));//将查询条件赋值给查询语句request.source().query(queryBuilder);//排序request.source().sort("editTime", SortOrder.ASC);//分页request.source().from(1).size(10);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);}@Testpublic void testHightLightQuery() throws IOException {//创建查询请求,指定要查询的索引库SearchRequest request = new SearchRequest("case_management");//构建查询条件,下边是布尔查询BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));//将查询条件赋值给查询语句request.source().query(queryBuilder);//自定义高亮 查找HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.preTags("<font color='red'>");highlightBuilder.postTags("</font>");highlightBuilder.field("oa");// highlightBuilder.requireFieldMatch(false); //多字段时,需要设置为false// request.source().highlighter(highlightBuilder);//排序request.source().sort("editTime", SortOrder.ASC);//分页request.source().from(1).size(10);//进行查询SearchResponse response = client.search(request, RequestOptions.DEFAULT);System.out.println("respon话费时间 " + response.getTook());//对结果进行处理SearchHits hits = response.getHits();//返回结果列表SearchHit[] hitsList = hits.getHits();//总数long totalHits = hits.getTotalHits();System.out.println(totalHits);SearchHit documentFields = hitsList[0];String json = documentFields.getSourceAsString();System.out.println(json);//获取到高亮字段System.out.println(documentFields.getHighlightFields());}
3、错误解决
场景
ES对text类型的字段进行聚合操作的时候, 报 Fielddata is disabled on text fields by default. Set fielddata=true on [make] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.
分析
官方文档
sort, aggregate, or access values from a script on a text field 这些行为会需要从内存中加载数据, 但是text类型的数据可能会消耗非常多内存, 另外可能会导致查询延迟, 所以默认不允许对text字段这样操作
所以最好对非text类型字段进行排序等操作