-
拼装搜索请求参数(SearchRequest)
-
创建参数存储对象(SearchSourceBuilder)
-
拼装搜索条件(QueryBuilders)
-
match_all : 查询全部
-
match
-
term
-
range
-
geodistance
-
bool
MatchAllQueryBuilder query = QueryBuilders.matchAllQuery(); sourceBuilder.query(query);// 把搜索条件存入参数对象中 // 拼装搜索条件-模糊全部 // 参数1:搜索的字段名称 // 参数2:搜索的关键字 MatchQueryBuilder query = QueryBuilders.matchQuery("name", "上海外滩"); sourceBuilder.query(query);// 把搜索条件存入参数对象中 // 参数2:搜索的字段名称 // 参数1:搜索的关键字 MultiMatchQueryBuilder query = QueryBuilders.multiMatchQuery("福华路广场","name","address"); // 参数1:搜索的字段名称 // 参数2:搜索的关键字 TermQueryBuilder query = QueryBuilders.termQuery("brand", "7天酒店"); RangeQueryBuilder query = QueryBuilders.rangeQuery("price").gte(10).lte(159); // bool查询 BoolQueryBuilder query = QueryBuilders.boolQuery(); MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", "如家"); query.must(matchQuery); RangeQueryBuilder priceQuery = QueryBuilders.rangeQuery("price").gte(400); query.mustNot(priceQuery); GeoDistanceQueryBuilder locationQuery = QueryBuilders.geoDistanceQuery("location") .distance(10, DistanceUnit.KILOMETERS) .point(31.21, 121.5); query.filter(locationQuery);
-
function_score
// ========================= 拼装广告置顶的搜索条件 FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[1]; TermQueryBuilder adQuery = QueryBuilders.termQuery("isAD", true); WeightBuilder weightBuilder = ScoreFunctionBuilders.weightFactorFunction(10);
-
// 参数1:过滤条件 adQuery // 参数2:分值策略
-
-
weightBuilder filterFunctionBuilders[0] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(adQuery,weightBuilder);
// 参数1:查询条件 query // 参数2:分值修改函数
-
FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(query,filterFunctionBuilders); functionScoreQuery.boostMode(CombineFunction.MULTIPLY);
sourceBuilder.query(functionScoreQuery);
- 拼装分页条件 // 分页参数 int page = 2; int size = 10; sourceBuilder.size(size); sourceBuilder.from((page-1)*size);
// 分页参数\ int page = 2; int size = 10; sourceBuilder.size(size); sourceBuilder.from((page-1)*size);
- 拼装排序条件 ```java sourceBuilder.sort("price", SortOrder.DESC); sourceBuilder.sort("score", SortOrder.DESC);
-
拼装高亮条件
// 高亮条件 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field("name"); highlightBuilder.preTags("<b>"); highlightBuilder.postTags("</b>"); highlightBuilder.requireFieldMatch(false);//搜索字段和高亮字段不进行一致性检查 sourceBuilder.highlighter(highlightBuilder);
-
拼装聚合条件(AggregationBuilders)
// 设置城市聚合条件 TermsAggregationBuilder city = AggregationBuilders.terms("city_name").field("city").size(10); sourceBuilder.aggregation(city); // 设置星级聚合条件 TermsAggregationBuilder starName = AggregationBuilders.terms("star_name").field("starName").size(20); sourceBuilder.aggregation(starName); // 设置品牌聚合条件 TermsAggregationBuilder brand = AggregationBuilders.terms("brand_name").field("brand").size(20); sourceBuilder.aggregation(brand); request.source(sourceBuilder);
-
-
-
执行请求
// 执行搜索请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT);
-
解析搜索结果
-
解析总条数
// 获取查询总条数 TotalHits totalHits = hits.getTotalHits(); long total = totalHits.value; System.out.println("总条数:"+total);
-
解析分页数据
// 获取当前页数据 SearchHit[] list = hits.getHits(); List<HotelDoc> results = new ArrayList<>(); for (SearchHit hit : list) { String doc = hit.getSourceAsString(); HotelDoc hotelDoc = JSON.parseObject(doc, HotelDoc.class); results.add(hotelDoc); } System.out.println(results.toString());
-
解析排序数据
// 获取排序结果 Object[] sortValues = hit.getSortValues(); if(sortValues!=null&&sortValues.length>0){ doc.setDistance(sortValues[0]); }
-
解析高亮数据
// 解析高亮结果 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); if(highlightFields!=null) { HighlightField highlightField = highlightFields.get("name"); if(highlightField!=null) { Text[] fragments = highlightField.fragments(); Text fragment = fragments[0]; String temp = fragment.string(); hotelDoc.setName(temp); } }
-
解析聚合数据
Terms terms = (Terms) aggregationMap.get(name); List<? extends Terms.Bucket> buckets = terms.getBuckets(); List<String> list = new ArrayList<>(); for (Terms.Bucket bucket : buckets) { String key = bucket.getKeyAsString(); list.add(key); }
-
Elasticsearch常见搜索方法的实现
最新推荐文章于 2024-05-15 17:25:07 发布