本文主要是介绍山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(三十二)- 微服务(12),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
12.8 RestClient查询文档
12.8.1 快速入门
12.8.2 match, term,bool,range查询
12.8.3 排序和分页
12.8.4 高亮
12.8 RestClient查询文档
12.8.1 快速入门
@Testvoid testMatchALL() throws IOException {// 1. 准备requestSearchRequest request = new SearchRequest("hotel");// 2. 准备DSLrequest.source().query(QueryBuilders.matchAllQuery());// 3. 发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);System.out.println(response);}
结果解析:
@Testvoid testMatchALL() throws IOException {// 1. 准备requestSearchRequest request = new SearchRequest("hotel");// 2. 准备DSLrequest.source().query(QueryBuilders.matchAllQuery());// 3. 发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4. 结果解析SearchHits searchHits = response.getHits();// 4.1 获取总条数long total = searchHits.getTotalHits().value;System.out.println(total);// 4.2 获取文档数组SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {// 取文档sourceString json = hit.getSourceAsString();// 反序列化HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);}}
12.8.2 match, term,bool,range查询
把结果解析的代码抽取成方法
private void HandleResponse(SearchResponse response){// 4. 结果解析SearchHits searchHits = response.getHits();// 4.1 获取总条数long total = searchHits.getTotalHits().value;System.out.println(total);// 4.2 文档数组SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {// 取文档sourceString json = hit.getSourceAsString();// 反序列化HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);}}
match查询
@Testvoid testMatch() throws IOException {// 1. 准备requestSearchRequest request = new SearchRequest("hotel");// 2. 准备DSLrequest.source().query(QueryBuilders.matchQuery("all","如家"));// 3. 发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);HandleResponse(response);}
bool查询:
@Testvoid testBool() throws IOException {// 1. 准备requestSearchRequest request = new SearchRequest("hotel");// 2. 准备DSL// 2.1 准备BooleanQueryBoolQueryBuilder boolQuery = QueryBuilders.boolQuery();// 2.2 添加termboolQuery.must(QueryBuilders.termQuery("city","上海"));// 2.3 添加rangeboolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));request.source().query(boolQuery);// 3. 发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);HandleResponse(response);}
12.8.3 排序和分页
@Testvoid testPageAndSort() throws IOException {// 1. 准备requestSearchRequest request = new SearchRequest("hotel");// 2. 准备DSL// 2.1 queryrequest.source().query(QueryBuilders.matchAllQuery());// 2.2 排序sortrequest.source().sort("price", SortOrder.ASC);// 2.3 分页 from sizerequest.source().from(56).size(10); // 第56条开始展示10条// 3. 发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);HandleResponse(response);}
12.8.4 高亮
@Testvoid testHighLight() throws IOException {// 1. 准备requestSearchRequest request = new SearchRequest("hotel");// 2. 准备DSL// 2.1 queryrequest.source().query(QueryBuilders.matchQuery("all","如家"));// 2.2 高亮request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));// 3. 发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);HandleHighLightResponse(response);}
高亮结果解析 :
private void HandleHighLightResponse(SearchResponse response){// 4. 结果解析SearchHits searchHits = response.getHits();// 4.1 获取总条数long total = searchHits.getTotalHits().value;System.out.println(total);// 4.2 文档数组SearchHit[] hits = searchHits.getHits();for (SearchHit hit : hits) {// 取文档sourceString json = hit.getSourceAsString();// 反序列化HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);// 获取高亮结果Map<String, HighlightField> highlightFields = hit.getHighlightFields();if(!CollectionUtils.isEmpty(highlightFields)){// 根据名字获取高亮结果HighlightField highlightField = highlightFields.get("name");if(highlightField != null){// 获取高亮值String name = highlightField.getFragments()[0].string();// 覆盖非高亮结果hotelDoc.setName(name);}}System.out.println(hotelDoc);}}
这篇关于山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(三十二)- 微服务(12)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!