org.springframework.data.elasticsearch.core.aggregation.AggregatedPage Java Examples
The following examples show how to use
org.springframework.data.elasticsearch.core.aggregation.AggregatedPage.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UKAggTopResultExtractor.java From youkefu with Apache License 2.0 | 7 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { Aggregations aggregations = response.getAggregations(); Terms agg = aggregations.get(term) ; long total = agg.getSumOfOtherDocCounts() ; List<T> results = new ArrayList<T>(); if(agg.getBuckets()!=null && agg.getBuckets().size()>0){ for (int i = pageable.getPageNumber()*pageable.getPageSize();i<agg.getBuckets().size() ; i++) { Terms.Bucket entry = agg.getBuckets().get(i) ; if(!StringUtils.isBlank(name) && entry.getAggregations().get(name)!=null){ TopHits topHits = entry.getAggregations().get(name); for (SearchHit hit : topHits.getHits().getHits()) { T data = mapEntity(hit.getSourceAsString() , hit , clazz) ; if(data instanceof UKAgg){ ((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits()); } results.add(data) ; } } } } return new AggregatedPageImpl<T>(results, pageable, total); }
Example #2
Source File: ElasticSearchTest.java From ChengFeng1.5 with MIT License | 6 votes |
@Test public void demo(){ NativeSearchQueryBuilder builder=new NativeSearchQueryBuilder(); // 不查询任何结果 builder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null)); builder.addAggregation( AggregationBuilders.terms("品牌").field("subtitle")); // 2、查询,需要把结果强转为AggregatedPage类型 AggregatedPage<PurchaseInfoDto> aggPage = (AggregatedPage<PurchaseInfoDto>) this.purchaseInfoRepository.search(builder.build()); StringTerms agg = (StringTerms) aggPage.getAggregation("subtitle"); // 3.2、获取桶 List<StringTerms.Bucket> buckets = agg.getBuckets(); // 3.3、遍历 for (StringTerms.Bucket bucket : buckets) { // 3.4、获取桶中的key,即品牌名称 System.out.println(bucket.getKeyAsString()); // 3.5、获取桶中的文档数量 System.out.println(bucket.getDocCount()); } }
Example #3
Source File: PersonRepositoryTest.java From spring-boot-demo with MIT License | 6 votes |
/** * 测试聚合,测试平均年龄 */ @Test public void agg() { // 构造查询条件 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 不查询任何结果 queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null)); // 平均年龄 queryBuilder.addAggregation(AggregationBuilders.avg("avg").field("age")); log.info("【queryBuilder】= {}", JSONUtil.toJsonStr(queryBuilder.build())); AggregatedPage<Person> people = (AggregatedPage<Person>) repo.search(queryBuilder.build()); double avgAge = ((InternalAvg) people.getAggregation("avg")).getValue(); log.info("【avgAge】= {}", avgAge); }
Example #4
Source File: ElasticsearchTransactionRepository.java From servicecomb-pack with Apache License 2.0 | 6 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) { List<GlobalTransactionDocument> result = new ArrayList<>(); for (SearchHit hit : response.getHits()) { if (response.getHits().getHits().length <= 0) { return new AggregatedPageImpl<T>(Collections.EMPTY_LIST, pageable, response.getHits().getTotalHits(), response.getScrollId()); } GlobalTransactionDocument globalTransactionDocument = null; try { globalTransactionDocument = mapper.readValue(hit.getSourceAsString(), GlobalTransactionDocument.class); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } result.add(globalTransactionDocument); } if (result.isEmpty()) { return new AggregatedPageImpl<T>(Collections.EMPTY_LIST, pageable, response.getHits().getTotalHits(), response.getScrollId()); } return new AggregatedPageImpl<T>((List<T>) result, pageable, response.getHits().getTotalHits(), response.getScrollId()); }
Example #5
Source File: PersonRepositoryTest.java From spring-boot-demo with MIT License | 6 votes |
/** * 测试聚合,测试平均年龄 */ @Test public void agg() { // 构造查询条件 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 不查询任何结果 queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null)); // 平均年龄 queryBuilder.addAggregation(AggregationBuilders.avg("avg").field("age")); log.info("【queryBuilder】= {}", JSONUtil.toJsonStr(queryBuilder.build())); AggregatedPage<Person> people = (AggregatedPage<Person>) repo.search(queryBuilder.build()); double avgAge = ((InternalAvg) people.getAggregation("avg")).getValue(); log.info("【avgAge】= {}", avgAge); }
Example #6
Source File: ResultMapperExt.java From roncoo-education with MIT License | 6 votes |
@SuppressWarnings("deprecation") @Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { long totalHits = response.getHits().totalHits(); List<T> results = new ArrayList<>(); for (SearchHit hit : response.getHits()) { if (hit != null) { T result = null; if (StringUtils.hasText(hit.sourceAsString())) { result = mapEntity(hit.sourceAsString(), clazz); } else { result = mapEntity(hit.getFields().values(), clazz); } setPersistentEntityId(result, hit.getId(), clazz); setPersistentEntityVersion(result, hit.getVersion(), clazz); populateScriptFields(result, hit); // 高亮查询 populateHighLightedFields(result, hit.getHighlightFields()); results.add(result); } } return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations(), response.getScrollId()); }
Example #7
Source File: PersonRepositoryTest.java From spring-boot-demo with MIT License | 6 votes |
/** * 测试聚合,测试平均年龄 */ @Test public void agg() { // 构造查询条件 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 不查询任何结果 queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null)); // 平均年龄 queryBuilder.addAggregation(AggregationBuilders.avg("avg").field("age")); log.info("【queryBuilder】= {}", JSONUtil.toJsonStr(queryBuilder.build())); AggregatedPage<Person> people = (AggregatedPage<Person>) repo.search(queryBuilder.build()); double avgAge = ((InternalAvg) people.getAggregation("avg")).getValue(); log.info("【avgAge】= {}", avgAge); }
Example #8
Source File: HighlightResultMapper.java From code with Apache License 2.0 | 6 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> clazz, Pageable pageable) { SearchHits hits = searchResponse.getHits(); List<T> result = new ArrayList<>((int) hits.getTotalHits()); for (SearchHit searchHit : hits) { // 高亮 K,V Map<String, HighlightField> highlightFields = searchHit.getHighlightFields(); // 使用反射比较通用一些,也可以直接 new 对象,把值set进去 try { T document = objectMapper.readValue(searchHit.getSourceAsString(), clazz); for (Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) { Field field = clazz.getField(entry.getKey()); field.setAccessible(true); field.set(document, entry.getValue().fragments()[0].toString()); } result.add(document); } catch (IllegalAccessException | IOException | NoSuchFieldException e) { e.printStackTrace(); } } return new AggregatedPageImpl<>(result, pageable, hits.getTotalHits()); }
Example #9
Source File: TorrentDaoImpl.java From Dodder with MIT License | 6 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { long totalHits = response.getHits().getTotalHits(); float maxScore = response.getHits().getMaxScore(); List<Torrent> results = new ArrayList<>(); for (SearchHit hit : response.getHits().getHits()) { if (hit == null) continue; Torrent result; result = JSONUtil.parseObject(hit.getSourceAsString(), Torrent.class); result.setInfoHash(hit.getId()); if (hit.getHighlightFields().containsKey("fileName")) result.setFileName(hit.getHighlightFields().get("fileName").fragments()[0].toString()); else result.setFileName((String) hit.getSourceAsMap().get("fileName")); results.add(result); } return new AggregatedPageImpl<>((List<T>) results, pageable, totalHits, response.getAggregations(), response.getScrollId(), maxScore); }
Example #10
Source File: HighlightResultHelper.java From mogu_blog_v2 with Apache License 2.0 | 6 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { List<T> results = new ArrayList<>(); for (SearchHit hit : response.getHits()) { if (hit != null) { T result = null; if (StringUtils.hasText(hit.getSourceAsString())) { result = JSONObject.parseObject(hit.getSourceAsString(), clazz); } // 高亮查询 for (HighlightField field : hit.getHighlightFields().values()) { try { PropertyUtils.setProperty(result, field.getName(), concat(field.fragments())); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { log.error("设置高亮字段异常:{}", e.getMessage(), e); } } results.add(result); } } return new AggregatedPageImpl<T>(results, pageable, response.getHits().getTotalHits(), response.getAggregations(), response.getScrollId()); }
Example #11
Source File: UKResultMapper.java From youkefu with Apache License 2.0 | 6 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { long totalHits = response.getHits().totalHits(); List<T> results = new ArrayList<T>(); for (SearchHit hit : response.getHits()) { if (hit != null) { T result = null; if (StringUtils.isNotBlank(hit.sourceAsString())) { result = mapEntity(hit.sourceAsString() , hit , clazz); } else { result = mapEntity(hit.getFields().values() , hit , clazz); } setPersistentEntityId(result, hit.getId(), clazz); populateScriptFields(result, hit); results.add(result); } } return new AggregatedPageImpl<T>(results, pageable, totalHits); }
Example #12
Source File: XiaoEUKResultMapper.java From youkefu with Apache License 2.0 | 6 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { long totalHits = response.getHits().totalHits(); List<T> results = new ArrayList<T>(); for (SearchHit hit : response.getHits()) { if (hit != null) { T result = null; if (StringUtils.isNotBlank(hit.sourceAsString())) { result = mapEntity(hit.sourceAsString() , hit , clazz); } else { result = mapEntity(hit.getFields().values() , hit , clazz); } setPersistentEntityId(result, hit.getId(), clazz); populateScriptFields(result, hit); results.add(result); } } return new AggregatedPageImpl<T>(results, pageable, totalHits); }
Example #13
Source File: PurchaseInfoServiceImpl.java From ChengFeng1.5 with MIT License | 5 votes |
@Override public List<PurchaseInfoVo> getPurchaseInfo() { NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); // 不查询任何结果 builder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null)); builder.addAggregation( AggregationBuilders.terms("subtitle").field("subtitle") .subAggregation(AggregationBuilders.avg("sales").field("sales"))); // 2、查询,需要把结果强转为AggregatedPage类型 AggregatedPage<PurchaseInfoDto> aggPage = (AggregatedPage<PurchaseInfoDto>) this.purchaseInfoRepository.search(builder.build()); StringTerms agg = (StringTerms) aggPage.getAggregation("subtitle"); // 3.2、获取桶 List<StringTerms.Bucket> buckets = agg.getBuckets(); List<PurchaseInfoVo> purchaseInfoVos= Lists.newArrayList(); // 3.3、遍历 for (StringTerms.Bucket bucket : buckets) { // 3.4、获取桶中的key,即品牌名称 System.out.println(bucket.getKeyAsString()); InternalAvg sales= (InternalAvg) bucket.getAggregations().asMap().get("sales"); purchaseInfoVos.add(new PurchaseInfoVo(bucket.getKeyAsString(),sales.getValue())); } return purchaseInfoVos; }
Example #14
Source File: ResultTruncatedContentMapper.java From klask-io with GNU General Public License v3.0 | 5 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { List<File> result = new ArrayList<>(); long totalHits = response.getHits().getTotalHits(); for (SearchHit searchHit : response.getHits()) { if (response.getHits().getHits().length <= 0) { return null; } String content = (String) searchHit.getSource().get("content"); File oneFile = new File( (String) searchHit.getSource().get("id"), (String) searchHit.getSource().get("name"), (String) searchHit.getSource().get("extension"), (String) searchHit.getSource().get("path"), (String) searchHit.getSource().get("project"), content == null ? null : content.substring(0, Math.min(Constants.TRUNCATED_CONTENT, content.length())), (String) searchHit.getSource().get("version"), //conversion en string puis en long, très bizarre, à l'origine, il était préférable de réaliser : //(Long) searchHit.getSource().get("size") //mais cela jette un classCastException Integer to Long Long.valueOf(searchHit.getSource().get("size").toString()) ); result.add(oneFile); } return new AggregatedPageImpl<>((List<T>) result, pageable, totalHits, response.getAggregations()); }
Example #15
Source File: ElasticSearchService.java From mogu_blog_v2 with Apache License 2.0 | 4 votes |
public Map<String, Object> search(String keywords, Integer currentPage, Integer pageSize) { currentPage = Math.max(currentPage - 1, 0); List<HighlightBuilder.Field> highlightFields = new ArrayList<>(); HighlightBuilder.Field titleField = new HighlightBuilder.Field(SysConf.TITLE).preTags("<span style='color:red'>").postTags("</span>"); HighlightBuilder.Field summaryField = new HighlightBuilder.Field(SysConf.SUMMARY).preTags("<span style='color:red'>").postTags("</span>"); highlightFields.add(titleField); highlightFields.add(summaryField); HighlightBuilder.Field[] highlightFieldsAry = highlightFields.toArray(new HighlightBuilder.Field[highlightFields.size()]); //创建查询构造器 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); queryBuilder.withPageable(PageRequest.of(currentPage, pageSize)); //过滤 QueryStringQueryBuilder queryStrBuilder = new QueryStringQueryBuilder(keywords); queryStrBuilder.field("title").field("summary"); queryBuilder.withQuery(queryStrBuilder); queryBuilder.withHighlightFields(highlightFieldsAry); log.error("查询语句:{}", queryBuilder.build().getQuery().toString()); //查询 AggregatedPage<ESBlogIndex> result = elasticsearchTemplate.queryForPage(queryBuilder.build(), ESBlogIndex.class, highlightResultHelper); //解析结果 long total = result.getTotalElements(); int totalPage = result.getTotalPages(); List<ESBlogIndex> blogList = result.getContent(); Map<String, Object> map = new HashMap<>(); map.put(SysConf.TOTAL, total); map.put(SysConf.TOTAL_PAGE, totalPage); map.put(SysConf.PAGE_SIZE, pageSize); map.put(SysConf.CURRENT_PAGE, currentPage + 1); map.put(SysConf.BLOG_LIST, blogList); return map; }
Example #16
Source File: SearchService.java From leyou with Apache License 2.0 | 4 votes |
/** * 搜索商品 * * @param request * @return */ public SearchResult search(SearchRequest request) { // 判断是否有搜索条件,如果没有,直接返回null。不允许搜索全部商品 if (StringUtils.isBlank(request.getKey())) { return null; } // 1、初始化自定义查询构建器 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); //QueryBuilder basicQuery = QueryBuilders.matchQuery("all", request.getKey()).operator(Operator.AND); BoolQueryBuilder boolQueryBuilder = buildBooleanQueryBuilder(request); // 1.1、基本查询 //queryBuilder.withQuery(basicQuery); queryBuilder.withQuery(boolQueryBuilder); // 通过sourceFilter设置返回的结果字段,我们只需要id、subTitle、skus queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{"id", "subTitle", "skus"}, null)); // 1.2.分页排序 // 获取分页参数 Integer page = request.getPage(); Integer size = request.getSize(); // 添加分页 queryBuilder.withPageable(PageRequest.of(page - 1, size)); // 1.3、聚合 // 聚合名称 String categoryAggName = "categories"; // 商品分类聚合名称 String brandAggName = "brands"; // 品牌聚合名称 // 对商品分类进行聚合 queryBuilder.addAggregation(AggregationBuilders.terms(categoryAggName).field("cid3")); // 对品牌进行聚合 queryBuilder.addAggregation(AggregationBuilders.terms(brandAggName).field("brandId")); // 2、查询,获取结果 AggregatedPage<Goods> goodsPage = (AggregatedPage<Goods>) this.goodsRepository.search(queryBuilder.build()); // 3、解析查询结果 // 3.1、分页信息 Long total = goodsPage.getTotalElements(); int totalPage = (total.intValue() + request.getSize() - 1) / request.getSize(); // 3.2、商品分类的聚合结果 List<Map<String, Object>> categories = getCategoryAggResult(goodsPage.getAggregation(categoryAggName)); // 3.3、品牌的聚合结果 List<Brand> brands = getBrandAggResult(goodsPage.getAggregation(brandAggName)); // 根据商品分类个数判断是否需要聚合 List<Map<String, Object>> specs = null; if (!CollectionUtils.isEmpty(categories) && categories.size() == 1) { // 如果商品分类只有一个才进行聚合,并根据分类与基本查询条件聚合 specs = getParamAggResult((Long) categories.get(0).get("id"), boolQueryBuilder); } // 返回结果 return new SearchResult(total, totalPage, goodsPage.getContent(), categories, brands, specs); }
Example #17
Source File: ElasticsearchServiceImpl.java From MyCommunity with Apache License 2.0 | 4 votes |
public Page<DiscussPost> searchDiscussPost(String keyword, int current, int limit) { SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.multiMatchQuery(keyword, "title", "content")) .withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) .withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC)) .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC)) .withPageable(PageRequest.of(current, limit)) .withHighlightFields( new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"), new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>") ).build(); return elasticTemplate.queryForPage(searchQuery, DiscussPost.class, new SearchResultMapper() { @Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) { SearchHits hits = response.getHits(); if (hits.getTotalHits() <= 0) { return null; } List<DiscussPost> list = new ArrayList<>(); for (SearchHit hit : hits) { DiscussPost post = new DiscussPost(); String id = hit.getSourceAsMap().get("id").toString(); post.setId(Integer.valueOf(id)); String userId = hit.getSourceAsMap().get("userId").toString(); post.setUserId(Integer.valueOf(userId)); String title = hit.getSourceAsMap().get("title").toString(); post.setTitle(title); String content = hit.getSourceAsMap().get("content").toString(); post.setContent(content); String status = hit.getSourceAsMap().get("status").toString(); post.setStatus(Integer.valueOf(status)); String createTime = hit.getSourceAsMap().get("createTime").toString(); post.setCreateTime(new Date(Long.valueOf(createTime))); String commentCount = hit.getSourceAsMap().get("commentCount").toString(); post.setCommentCount(Integer.valueOf(commentCount)); // 处理高亮显示的结果 HighlightField titleField = hit.getHighlightFields().get("title"); if (titleField != null) { post.setTitle(titleField.getFragments()[0].toString()); } HighlightField contentField = hit.getHighlightFields().get("content"); if (contentField != null) { post.setContent(contentField.getFragments()[0].toString()); } list.add(post); } return new AggregatedPageImpl(list, pageable, hits.getTotalHits(), response.getAggregations(), response.getScrollId()); } }); }
Example #18
Source File: ResultHighlightMapper.java From klask-io with GNU General Public License v3.0 | 4 votes |
@Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) { List<File> result = new ArrayList<>(); long totalHits = response.getHits().getTotalHits(); for (SearchHit searchHit : response.getHits()) { if (response.getHits().getHits().length <= 0) { return null; } //System.out.println(response.toString()); String summaryWithHighlight = null; String pathWithHighlight = null; HighlightField highlightFieldContent = searchHit.getHighlightFields().get("content"); HighlightField highlightFieldPath = searchHit.getHighlightFields().get("path"); if (highlightFieldContent != null) { summaryWithHighlight = Arrays.stream(highlightFieldContent.fragments()) .map(text -> EncodingUtil.convertToUTF8(text.toString())) .collect(Collectors.joining("\n[...]\n")); } if (highlightFieldPath != null && highlightFieldPath.fragments() != null) { pathWithHighlight = EncodingUtil.unEscapeString(highlightFieldPath.fragments()[0].toString()); } File oneFile = new File( (String) searchHit.getSource().get("id"), (String) searchHit.getSource().get("name"), (String) searchHit.getSource().get("extension"), pathWithHighlight != null ? pathWithHighlight : (String) searchHit.getSource().get("path"), (String) searchHit.getSource().get("project"), summaryWithHighlight, (String) searchHit.getSource().get("version"), //conversion en string puis en long, très bizarre, à l'origine, il était préférable de réaliser : //(Long) searchHit.getSource().get("size") //mais cela jette un classCastException Integer to Long Long.valueOf(searchHit.getSource().get("size").toString()) ); oneFile.setScore(searchHit.getScore()); result.add(oneFile); } return new AggregatedPageImpl<>((List<T>) result, pageable, totalHits, response.getAggregations()); }