企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**1. 查询一个索引的所有文档** ```java /** * 查询一个索引的所有文档 */ @Test public void matchAllQuery() throws IOException { //获取客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建SearchSourceBuilder SearchSourceBuilder builder = new SearchSourceBuilder(); //查询所有数据 builder.query(QueryBuilders.matchAllQuery()); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30} //{"name":"lisi","nickname":"lisi","sex":"男","age":20} //{"name":"wangwu","nickname":"wangwu","sex":"女","age":40} //{"name":"zhangsan01","nickname":"zhangsan01","sex":"女","age":50} //{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30} client.close(); //关闭连接 } ``` **2. 关键字精准查询** ```java /** * 关键字精准查询。 * term 查询,精确的关键词匹配查询,不对查询条件进行分词。 */ @Test public void termQuery() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //SearchSourceBuilder SearchSourceBuilder builder = new SearchSourceBuilder(); //查询条件:age=30 builder.query(QueryBuilders.termQuery("age", "30")); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30} //{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30} client.close(); //关闭连接 } ``` **3. 分页查询** ```java /** * 分页查询 */ @Test public void pageQuery() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建SearchSourceBuilder SearchSourceBuilder builder = new SearchSourceBuilder(); //查询所有文档 builder.query(QueryBuilders.matchAllQuery()); //构建分页查询 //从第1页开始(第一页默认从0开始), from=(pageNum−1)∗size, pageNum为当前第几页 builder.from(0); //每页显示2条数据 builder.size(2); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30} //{"name":"lisi","nickname":"lisi","sex":"男","age":20} client.close(); } ``` **4. 排序** ```java /** * 排序 */ @Test public void orderQuery() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //构建查询request SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建查询体 SearchSourceBuilder builder = new SearchSourceBuilder(); //查询所有 builder.query(QueryBuilders.matchAllQuery()); //排序,升序排序 builder.sort("age", SortOrder.ASC); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"lisi","nickname":"lisi","sex":"男","age":20} //{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30} //{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30} //{"name":"wangwu","nickname":"wangwu","sex":"女","age":40} //{"name":"zhangsan01","nickname":"zhangsan01","sex":"女","age":50} client.close(); } ``` **5. 过滤字段** ```java /** * 过滤字段 */ @Test public void filterQuery() throws IOException { //获取客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //构建查询request SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建查询体 SearchSourceBuilder builder = new SearchSourceBuilder(); //查询条件:查询所有 builder.query(QueryBuilders.matchAllQuery()); //查询过滤字段 String[] excludes = {}; //不查询的字段 String[] includes = {"name", "age"}; //只查询的字段 builder.fetchSource(includes, excludes); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"zhangsan","age":30} //{"name":"lisi","age":20} //{"name":"wangwu","age":40} //{"name":"zhangsan01","age":50} //{"name":"zhangsan02","age":30} client.close(); } ``` **6. 组合查询** ```java /** * 组合查询 * bool把各种其它查询通过must(必须 )、must_not(必须不)、should(应该)的方式进行组合。 */ @Test public void boolQuery() throws IOException { //获取客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //构建搜索请求对象 SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建查询体 SearchSourceBuilder builder = new SearchSourceBuilder(); //构建bool查询体 BoolQueryBuilder boolBuilder = new BoolQueryBuilder(); //条件1:必须age=30 boolBuilder.must(QueryBuilders.matchQuery("age", "30")); //条件2:必须name!=zhangsan boolBuilder.mustNot(QueryBuilders.matchQuery("name", "zhangsan")); //条件3:应该sex=男 boolBuilder.should(QueryBuilders.matchQuery("sex", "男")); builder.query(boolBuilder); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30} } ``` **7. 范围查询** ```java /** * 范围查询 * gt 大于(>) * gte 大于等于(>=) * lt 小于(<) * lte 小于等于(<=) */ @Test public void rangeQuery() throws IOException { //获取客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //构建搜索请求对象 SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建查询体 SearchSourceBuilder builder = new SearchSourceBuilder(); //构建范围查询体 RangeQueryBuilder rangeBuilder = QueryBuilders.rangeQuery("age"); rangeBuilder.gte("30"); //age大于等于30 rangeBuilder.lte("40"); //age小于等于40 builder.query(rangeBuilder); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30} //{"name":"wangwu","nickname":"wangwu","sex":"女","age":40} //{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30} } ``` **8. 模糊查询** ```java /** * 模糊查询:返回包含与搜索字词相似的字词的文档。 * 1)编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括: * * 1、更改字符(box → fox) * * 2、删除字符(black → lack) * * 3、插入字符(sic → sick) * * 4、转置两个相邻字符(act → cat) * 为了找到相似的术语,`fuzzy` 查询会在指定的编辑距离内创建一组搜索词的所有可能的变体或扩展。然后查询返回每个扩展的完全匹配。 * 通过 `fuzziness` 修改编辑距离。一般使用默认值 AUTO,根据术语的长度生成编辑距离。 */ @Test public void fuzzyQuery() throws IOException { //获取客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //构建搜索请求对象 SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建查询体 SearchSourceBuilder builder = new SearchSourceBuilder(); //模糊查询 builder.query(QueryBuilders .fuzzyQuery("name", "zhangsan") .fuzziness(Fuzziness.AUTO)); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); } //{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30} //{"name":"zhangsan01","nickname":"zhangsan01","sex":"女","age":50} //{"name":"zhangsan02","nickname":"zhangsan02","sex":"女","age":30} } ``` **9. 高亮查询** ```java /** * 高亮查询 */ @Test public void highlightQuery() throws IOException { //获取客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); //构建SearchSourceBuilder SearchSourceBuilder builder = new SearchSourceBuilder(); //查询name=zhangsan TermsQueryBuilder termsBuilder = QueryBuilders.termsQuery("name", "zhangsan"); builder.query(termsBuilder); //构建高亮字段 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.preTags("<font color='red'>"); //前置标签 highlightBuilder.postTags("</font>"); //后置标签 highlightBuilder.field("name"); //要高亮的字段 builder.highlighter(highlightBuilder); request.source(builder); //发送请求并获取响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); for (SearchHit hit : response.getHits()) { log.info(hit.getSourceAsString()); Map<String, HighlightField> highlightFieldMap = hit.getHighlightFields(); log.info("高亮结果:{}", highlightFieldMap); } //{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30} //高亮结果:{name=[name], fragments[[<font color='red'>zhangsan</font>]]} } ``` **10. 聚合查询** ```java /** * 聚合查询 */ @Test public void aggsQuery() throws IOException { //获取客户端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); SearchRequest request = new SearchRequest(); //设置要查询的索引 request.indices("student"); SearchSourceBuilder builder = new SearchSourceBuilder(); //求最大值 //searchSourceBuilder.aggregation(AggregationBuilders.max("max_age").field("age")); //最小值 //searchSourceBuilder.aggregation(AggregationBuilders.min("min_age").field("age")); //stats可以一次性将max/min/avg/count/sum求出 builder.aggregation(AggregationBuilders.stats("stats_age").field("age")); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); //...,"aggregations":{"stats#stats_age":{"count":5,"min":20.0,"max":50.0,"avg":34.0,"sum":170.0}}} log.info("{}", response); client.close(); } ``` **11. 分组统计** ```java /** * 分组统计 */ @Test public void groupbyQuery() throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http"))); SearchRequest request = new SearchRequest(); request.indices("student"); //设置要搜索的索引 SearchSourceBuilder builder = new SearchSourceBuilder(); builder.aggregation(AggregationBuilders.terms("age_groupby").field("age")); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); //... // {"key":30,"doc_count":2}, // {"key":20,"doc_count":1}, // {"key":40,"doc_count":1}, // {"key":50,"doc_count":1}]}}} log.info("{}", response); } ```