本文整理汇总了Java中org.springframework.data.elasticsearch.core.aggregation.AggregatedPage类的典型用法代码示例。如果您正苦于以下问题:Java AggregatedPage类的具体用法?Java AggregatedPage怎么用?Java AggregatedPage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AggregatedPage类属于org.springframework.data.elasticsearch.core.aggregation包,在下文中一共展示了AggregatedPage类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mapResults
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; //导入依赖的package包/类
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
long totalHits = response.getHits().totalHits();
LOG.info("total hits in mapResults are :" + totalHits);
List<T> results = new ArrayList<>();
for (SearchHit hit : response.getHits()) {
if (hit != null) {
T result;
if (StringUtils.isNotBlank(hit.sourceAsString())) {
result = mapEntity(hit.sourceAsString(), clazz);
} else {
result = mapEntity(hit.getFields().values(), clazz);
}
setPersistentEntityId(result, hit.getId(), clazz);
results.add(result);
}
}
return new AggregatedPageImpl<>(results, pageable, totalHits);
}
示例2: mapResults
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; //导入依赖的package包/类
@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());
}
示例3: mapResults
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; //导入依赖的package包/类
@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());
}
示例4: queryPage
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; //导入依赖的package包/类
/**
* 条件查询
*
* @param filedContentMap 字段和查询内容
* @param heightFields
* @param sortField
* @param order
* @param basePage
* @return
*/
@Override
public BasePage<T> queryPage(Map<String, Object> filedContentMap, final List<String> heightFields, String sortField, SortOrder order, BasePage<T> basePage) {
Field[] hfields = new Field[0];
if (heightFields != null) {
hfields = new Field[heightFields.size()];
for (int i = 0; i < heightFields.size(); i++) {
hfields[i] = new Field(heightFields.get(i)).preTags("<em style='color:red'>").postTags("</em>").fragmentSize(250);
}
}
NativeSearchQueryBuilder nsb = new NativeSearchQueryBuilder().withHighlightFields(hfields);//高亮字段
if (sortField != null && order != null)//排序
nsb.withSort(new FieldSortBuilder(sortField).ignoreUnmapped(true).order(order));
if (basePage != null)//分页
nsb.withPageable(new PageRequest(basePage.getPageNo() - 1, basePage.getPageSize()));
BoolQueryBuilder qb = QueryBuilders.boolQuery();
for (String key : filedContentMap.keySet()) {//字段查询
qb.must(QueryBuilders.matchQuery(key, filedContentMap.get(key)));
}
nsb.withQuery(qb);
SearchQuery searchQuery = nsb.build();//查询建立
Page<T> page = null;
if (heightFields != null && heightFields.size() > 0) {//如果设置高亮
page = elasticsearchTemplate.queryForPage(searchQuery, entityClass, new SearchResultMapper() {
@SuppressWarnings("unchecked")
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<T> chunk = new ArrayList<T>();
for (SearchHit searchHit : response.getHits()) {
if (response.getHits().getHits().length <= 0) {
return null;
}
Map<String, Object> entityMap = searchHit.getSource();
for (String highName : heightFields) {
Text text[] = searchHit.getHighlightFields().get(highName).fragments();
if (text.length > 0) {
String highValue = searchHit.getHighlightFields().get(highName).fragments()[0].toString();
entityMap.put(highName, highValue);
}
}
chunk.add((T) PropertyHelper.getFansheObj(entityClass, entityMap));
}
if (chunk.size() > 0) {
return new AggregatedPageImpl<T>((List<T>) chunk);
}
return new AggregatedPageImpl<T>(new ArrayList<T>());
}
});
} else {//如果不设置高亮
logger.info("#################" + qb.toString());
page = elasticsearchTemplate.queryForPage(searchQuery, entityClass);
}
// List<T> ts = page.getContent();
basePage.setTotalRecord(page.getTotalElements());
basePage.setResults(page.getContent());
return basePage;
}
示例5: queryList
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; //导入依赖的package包/类
/**
* @param filedContentMap
* @param heightFields
* @param sortField
* @param order
* @return
*/
@Override
public List<T> queryList(Map<String, Object> filedContentMap, final List<String> heightFields, String sortField, SortOrder order) {
Field[] hfields = new Field[0];
if (heightFields != null) {
hfields = new Field[heightFields.size()];
for (int i = 0; i < heightFields.size(); i++) {
//String o="{\"abc\" : \"[abc]\"}";
hfields[i] = new Field(heightFields.get(i)).preTags("<em>").postTags("</em>").fragmentSize(250);
}
}
NativeSearchQueryBuilder nsb = new NativeSearchQueryBuilder().withHighlightFields(hfields);//高亮字段
if (sortField != null && order != null)//排序
nsb.withSort(new FieldSortBuilder(sortField).ignoreUnmapped(true).order(order));
BoolQueryBuilder qb = QueryBuilders.boolQuery();
for (String key : filedContentMap.keySet()) {//字段查询
qb.must(QueryBuilders.matchQuery(key, filedContentMap.get(key)));
}
nsb.withQuery(qb);
SearchQuery searchQuery = nsb.build();//查询建立
Page<T> page = null;
if (heightFields != null && heightFields.size() > 0) {//如果设置高亮
page = elasticsearchTemplate.queryForPage(searchQuery, entityClass, new SearchResultMapper() {
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<T> chunk = new ArrayList<T>();
for (SearchHit searchHit : response.getHits()) {
if (response.getHits().getHits().length <= 0) {
return null;
}
Map<String, Object> entityMap = searchHit.getSource();
for (String highName : heightFields) {
String highValue = searchHit.getHighlightFields().get(highName).fragments()[0].toString();
entityMap.put(highName, highValue);
}
chunk.add((T) PropertyHelper.getFansheObj(entityClass, entityMap));
}
if (chunk.size() > 0) {
return new AggregatedPageImpl<T>((List<T>) chunk);
}
return null;
}
});
} else//如果不设置高亮
page = elasticsearchTemplate.queryForPage(searchQuery, entityClass);
return page.getContent();
}
示例6: queryList
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; //导入依赖的package包/类
/**
* @param filedContentMap
* @param heightFields
* @param sortFields
* @param order
* @return
*/
public List<GoodsModel> queryList(Map<String, Object> filedContentMap, final List<String> heightFields, String[] sortFields, String order, String[] resultFields) {
HighlightBuilder.Field[] hfields = new HighlightBuilder.Field[0];
if (heightFields != null) {
hfields = new HighlightBuilder.Field[heightFields.size()];
for (int i = 0; i < heightFields.size(); i++) {
//String o="{\"abc\" : \"[abc]\"}";
hfields[i] = new HighlightBuilder.Field(heightFields.get(i)).preTags("<em>").postTags("</em>").fragmentSize(250);
}
}
NativeSearchQueryBuilder nsb = new NativeSearchQueryBuilder().withHighlightFields(hfields);//高亮字段
// if (sortField != null && order != null){//排序
// nsb.withSort(new FieldSortBuilder(sortField).ignoreUnmapped(true).order(order));
// }
BoolQueryBuilder qb = QueryBuilders.boolQuery();
for (String key : filedContentMap.keySet()) {//字段查询
qb.must(QueryBuilders.matchQuery(key, filedContentMap.get(key)));
}
nsb.withQuery(qb);
SearchQuery searchQuery = nsb.build();//查询建立
searchQuery.addFields(resultFields);
if (sortFields != null && sortFields.length > 0 && order != null) {//排序
if ("desc".equals(order)) {//降序
searchQuery.addSort(new Sort(Sort.Direction.DESC, sortFields));
}
if ("asc".equals(order)) {//升序
searchQuery.addSort(new Sort(Sort.Direction.ASC, sortFields));
}
}
Page<GoodsModel> page = null;
if (heightFields != null && heightFields.size() > 0) {//如果设置高亮
page = elasticsearchTemplate.queryForPage(searchQuery, GoodsModel.class, new SearchResultMapper() {
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<T> chunk = new ArrayList<T>();
for (SearchHit searchHit : response.getHits()) {
if (response.getHits().getHits().length <= 0) {
return null;
}
Map<String, Object> entityMap = searchHit.getSource();
for (String highName : heightFields) {
String highValue = searchHit.getHighlightFields().get(highName).fragments()[0].toString();
entityMap.put(highName, highValue);
}
chunk.add((T) PropertyHelper.getFansheObj(GoodsModel.class, entityMap));
}
if (chunk.size() > 0) {
return new AggregatedPageImpl<T>((List<T>) chunk);
}
return null;
}
});
} else//如果不设置高亮
page = elasticsearchTemplate.queryForPage(searchQuery, GoodsModel.class);
return page.getContent();
}