赞
踩
目录
12.8.2 match, term,bool,range查询
@Test void testMatchALL() throws IOException { // 1. 准备request SearchRequest request = new SearchRequest("hotel"); // 2. 准备DSL request.source() .query(QueryBuilders.matchAllQuery()); // 3. 发送请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); System.out.println(response); }
结果解析:
@Test void testMatchALL() throws IOException { // 1. 准备request SearchRequest request = new SearchRequest("hotel"); // 2. 准备DSL request.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) { // 取文档source String json = hit.getSourceAsString(); // 反序列化 HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); System.out.println(hotelDoc); } }
把结果解析的代码抽取成方法
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) { // 取文档source String json = hit.getSourceAsString(); // 反序列化 HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); System.out.println(hotelDoc); } }
match查询
@Test void testMatch() throws IOException { // 1. 准备request SearchRequest request = new SearchRequest("hotel"); // 2. 准备DSL request.source() .query(QueryBuilders.matchQuery("all","如家")); // 3. 发送请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); HandleResponse(response); }
bool查询:
@Test void testBool() throws IOException { // 1. 准备request SearchRequest request = new SearchRequest("hotel"); // 2. 准备DSL // 2.1 准备BooleanQuery BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); // 2.2 添加term boolQuery.must(QueryBuilders.termQuery("city","上海")); // 2.3 添加range boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250)); request.source().query(boolQuery); // 3. 发送请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); HandleResponse(response); }
@Test void testPageAndSort() throws IOException { // 1. 准备request SearchRequest request = new SearchRequest("hotel"); // 2. 准备DSL // 2.1 query request.source().query(QueryBuilders.matchAllQuery()); // 2.2 排序sort request.source().sort("price", SortOrder.ASC); // 2.3 分页 from size request.source().from(56).size(10); // 第56条开始展示10条 // 3. 发送请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); HandleResponse(response); }
@Test void testHighLight() throws IOException { // 1. 准备request SearchRequest request = new SearchRequest("hotel"); // 2. 准备DSL // 2.1 query request.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) { // 取文档source String 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); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。