org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl Java Examples

The following examples show how to use org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl. 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 vote down vote up
@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: XiaoEUKResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: UKResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: HighlightResultHelper.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: TorrentDaoImpl.java    From Dodder with MIT License 6 votes vote down vote up
@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 #6
Source File: HighlightResultMapper.java    From code with Apache License 2.0 6 votes vote down vote up
@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 #7
Source File: ResultMapperExt.java    From roncoo-education with MIT License 6 votes vote down vote up
@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 #8
Source File: ElasticsearchTransactionRepository.java    From servicecomb-pack with Apache License 2.0 6 votes vote down vote up
@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 #9
Source File: ResultTruncatedContentMapper.java    From klask-io with GNU General Public License v3.0 5 votes vote down vote up
@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 #10
Source File: ElasticsearchServiceImpl.java    From MyCommunity with Apache License 2.0 4 votes vote down vote up
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 #11
Source File: ResultHighlightMapper.java    From klask-io with GNU General Public License v3.0 4 votes vote down vote up
@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());
}