-
篇二,我们对比SQL,在可视化工具下,了解了dsl的相关操作,现在我们在idea里面用Java代码实现相应的功能
- 我们新建一个搜索测试类HotelSearchTest
- 配置好客户端的连接
-
private RestHighLevelClient client; @BeforeEach //1建立连接 void setUp() { //创建client,连接服务器 this.client = new RestHighLevelClient( RestClient.builder(HttpHost.create("http://192.168.8.171:9200")) ); } @AfterEach //3关闭 void tearDown() throws IOException { //关闭 this.client.close(); }
- 测试匹配搜索所有,默认分配10条
-
@Test void testMatchAll() throws IOException { //1.request SearchRequest request = new SearchRequest("hotel"); //dsl的准备 request.source().query(QueryBuilders.matchAllQuery()); //开始请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //打印搜索结果 System.out.println(response); //解析结果打印 :searchHits:代表着hit下面的所有结果,包括了total和hits SearchHits searchHits = response.getHits(); //获取总记录数 long total = searchHits.getTotalHits().value; System.out.println("总计数为:"+total); //获取结果数据hits(这个hits包括的是所有的文档数据,json格式的hoteldoc) SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { //得到其中一个hoteldoc的json串 String hotelJsonStr = hit.getSourceAsString(); System.out.println(hotelJsonStr); //将json转为Java对象hoteldoc JSON.parseObject(hotelJsonStr, HotelDoc.class); } }
-
- 抽取出专门处理搜索结果的方法
-
//专门处理搜索结果的方法 private void resHandler(SearchResponse response) { //解析结果打印 :searchHits:代表着hit下面的所有结果,包括了total和hits SearchHits searchHits = response.getHits(); //获取总记录数 long total = searchHits.getTotalHits().value; System.out.println("总计数为:"+total); //获取结果数据hits(这个hits包括的是所有的文档数据,json格式的hoteldoc) SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { //得到其中一个hoteldoc的json串 String hotelJsonStr = hit.getSourceAsString(); System.out.println(hotelJsonStr); //将json转为Java对象hoteldoc HotelDoc hotelDoc = JSON.parseObject(hotelJsonStr, HotelDoc.class); System.out.println(hotelDoc); } }
-
- 在查所有的方法中调用
- 复合查询
- 要构建查询条件,要记住QueryBuilders这个类
@Test//复合查询,要构建查询条件,要记住QueryBuilders这个类 void testBool() throws IOException { SearchRequest request = new SearchRequest("hotel"); //编写dsl语句(就理解为SQL语句) BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); boolQuery.must(QueryBuilders.termQuery("city","上海")); boolQuery.filter(QueryBuilders.rangeQuery("price").lte(255)); request.source().query(boolQuery);//配置好查询请求的条件 //发送请求进行搜索得到数据响应 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //处理结果的函数 resHandler(response); }
- 要构建查询条件,要记住QueryBuilders这个类
- 分页查询
-
//分页查询 @Test void testPageOrSort() throws IOException { int page=1,size=5; SearchRequest request = new SearchRequest("hotel"); //准备dsl request.source().query(QueryBuilders.matchAllQuery()); //准备排序 request.source().sort("price", SortOrder.ASC); //分页 request.source().from((page-1)*size).size(size);//从第几页开始,一页分几行 //发送请求:用上面准备好的dsl去执行 SearchResponse response = client.search(request, RequestOptions.DEFAULT); // resHandler(response); }
-
- 高亮处理
- 修改一下通用方法
-
//高亮查询 @Test void testHightLight() throws IOException { SearchRequest request = new SearchRequest("hotel"); //准备dsl request.source().query(QueryBuilders.matchQuery("all","如家")); //设置高亮 request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false)); //发送请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //调用查询搜索结果的方法 resHandler(response); }
//分析高亮 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); if (highlightFields != null){ HighlightField name = highlightFields.get("name"); if (name != null){ String highName = name.getFragments()[0].string(); //将高亮 hotelDoc.setName(highName); } } System.out.println("高亮处理完毕:"+hotelDoc);
es--elasticsearch---篇三:Java快速搜索,查找,复合查找,分页,高亮(1027)
于 2022-11-01 12:48:43 首次发布