use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project pancm_project by xuwujing.
the class EsAggregationSearchTest2 method test.
private static void test() throws IOException {
// TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("ecid").field("ecid").size(99999);
// List<FieldSortBuilder> fieldSorts=new ArrayList<>();
// fieldSorts.add(new FieldSortBuilder("sum_field").order(SortOrder.DESC));
// termsBuilder.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", fieldSorts).from(6000).size(10));
//
AggregationBuilder aggregation = AggregationBuilders.terms("ecid").field("ecid");
List<FieldSortBuilder> fieldSorts = new ArrayList<>();
fieldSorts.add(new FieldSortBuilder("id_").order(SortOrder.DESC));
aggregation.subAggregation(new BucketSortPipelineAggregationBuilder("id", fieldSorts).from(0).size(10));
SearchResponse searchResponse = search(aggregation);
// 获取聚合结果
Aggregations aggregations = searchResponse.getAggregations();
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> list = new ArrayList<>();
agg(map, list, aggregations);
logger.info("聚合查询结果:" + list);
logger.info("------------------------------------");
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project pancm_project by xuwujing.
the class EsAggregationSearchTest2 method test2.
private static void test2() throws IOException {
AggregationBuilder aggregation = AggregationBuilders.filters("ecid", QueryBuilders.termQuery("id", 1));
SearchResponse searchResponse = search(aggregation);
// 获取聚合结果
Aggregations aggregations = searchResponse.getAggregations();
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> list = new ArrayList<>();
agg(map, list, aggregations);
logger.info("test2聚合查询结果:" + list);
logger.info("------------------------------------");
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project pancm_project by xuwujing.
the class EsHighLevelRestSearchTest method search.
/**
* search查询使用示例
*
* @throws IOException
*/
private static void search() throws IOException {
/*
* 全文查询使用示例
*/
// 查询指定的索引库
SearchRequest searchRequest = new SearchRequest("user");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 搜索字段user为pancm的数据
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "pancm");
// 设置模糊查询
matchQueryBuilder.fuzziness(Fuzziness.AUTO);
// 设置前缀长度
matchQueryBuilder.prefixLength(3);
// 设置最大扩展选项来控制查询的模糊过程
matchQueryBuilder.maxExpansions(10);
/*
* QueryBuilder也可以
*/
// QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
// .fuzziness(Fuzziness.AUTO)
// .prefixLength(3)
// .maxExpansions(10);
SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
searchSourceBuilder2.query(matchQueryBuilder);
SearchRequest searchRequest2 = new SearchRequest();
searchRequest2.source(searchSourceBuilder2);
// 同步查询
SearchResponse searchResponse2 = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse2.getHits();
// 总条数和分值
long totalHits = hits.getTotalHits();
float maxScore = hits.getMaxScore();
hits.forEach(hit -> {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String string = hit.getSourceAsString();
System.out.println("Match查询的Map结果:" + sourceAsMap);
System.out.println("Match查询的String结果:" + string);
String documentTitle = (String) sourceAsMap.get("title");
// List<Object> users = (List<Object>) sourceAsMap.get("user");
Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
});
System.out.println("\n=================\n");
/*
* 高亮查询
*/
SearchSourceBuilder searchSourceBuilder3 = new SearchSourceBuilder();
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
// 设置字段高亮字体
highlightTitle.highlighterType("user");
highlightBuilder.field(highlightTitle);
HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
highlightBuilder.field(highlightUser);
searchSourceBuilder3.highlighter(highlightBuilder);
SearchRequest searchRequest3 = new SearchRequest();
searchRequest3.source(searchSourceBuilder3);
// 同步查询
SearchResponse searchResponse3 = client.search(searchRequest3, RequestOptions.DEFAULT);
searchResponse3.getHits().forEach(hit -> {
Map<String, Object> map = hit.getSourceAsMap();
String string = hit.getSourceAsString();
System.out.println("Highlight查询的Map结果:" + map);
System.out.println("Highlight查询的String结果:" + string);
});
System.out.println("\n=================\n");
/**
* 聚合查询
*/
SearchSourceBuilder searchSourceBuilder4 = new SearchSourceBuilder();
// terms 就是分组统计 根据user进行分组并创建一个新的聚合user_
TermsAggregationBuilder aggregation = AggregationBuilders.terms("user_").field("user");
aggregation.subAggregation(AggregationBuilders.avg("average_age").field("age"));
searchSourceBuilder4.aggregation(aggregation);
SearchRequest searchRequest4 = new SearchRequest();
searchRequest4.source(searchSourceBuilder4);
// 同步查询
SearchResponse searchResponse4 = client.search(searchRequest4, RequestOptions.DEFAULT);
// 聚合查询返回条件
Aggregations aggregations = searchResponse4.getAggregations();
System.out.println("聚合查询");
for (Aggregation agg : aggregations) {
String type = agg.getType();
String name = agg.getName();
Terms terms = (Terms) aggregations.get(name);
for (Terms.Bucket bucket : terms.getBuckets()) {
System.out.println("条数:" + bucket.getDocCount());
System.out.println("key:" + bucket.getKey());
System.out.println("num:" + bucket.getKeyAsNumber());
Avg avg = bucket.getAggregations().get("average_age");
System.out.println("value:" + avg.getValue());
}
if (type.equals(TermsAggregationBuilder.NAME)) {
Bucket elasticBucket = ((Terms) agg).getBucketByKey("average_age");
long numberOfDocs = elasticBucket.getDocCount();
System.out.println("条数:" + numberOfDocs);
}
}
/*
* 建议查询
*/
SearchSourceBuilder searchSourceBuilder5 = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("pancm");
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
searchSourceBuilder5.suggest(suggestBuilder);
SearchRequest searchRequest5 = new SearchRequest();
searchRequest5.source(searchSourceBuilder5);
// 同步查询
SearchResponse searchResponse5 = client.search(searchRequest5, RequestOptions.DEFAULT);
Suggest suggest = searchResponse5.getSuggest();
TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
for (TermSuggestion.Entry.Option option : entry) {
String suggestText = option.getText().string();
System.out.println("返回结果:" + suggestText);
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project pancm_project by xuwujing.
the class EsAggregationSearchTest method agg.
protected static void agg(AggregationBuilder aggregation, String buk) throws IOException {
SearchResponse searchResponse = search(aggregation);
if (RestStatus.OK.equals(searchResponse.status())) {
// 获取聚合结果
Aggregations aggregations = searchResponse.getAggregations();
if (buk.contains("avg")) {
// 取子聚合
Avg ba = aggregations.get(buk);
logger.info(buk + ":" + ba.getValue());
logger.info("------------------------------------");
} else if (buk.contains("max")) {
// 取子聚合
Max ba = aggregations.get(buk);
logger.info(buk + ":" + ba.getValue());
logger.info("------------------------------------");
} else if (buk.contains("min")) {
// 取子聚合
Min ba = aggregations.get(buk);
logger.info(buk + ":" + ba.getValue());
logger.info("------------------------------------");
} else if (buk.contains("sum")) {
// 取子聚合
Sum ba = aggregations.get(buk);
logger.info(buk + ":" + ba.getValue());
logger.info("------------------------------------");
} else if (buk.contains("top")) {
// 取子聚合TopHits
TopHits ba = aggregations.get(buk);
logger.info(buk + ":" + ba.getHits().totalHits);
logger.info("------------------------------------");
} else if (buk.contains("group")) {
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> list = new ArrayList<>();
agg(map, list, aggregations);
logger.info("聚合查询结果:" + list);
logger.info("------------------------------------");
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project snow-owl by b2ihealthcare.
the class EsDocumentSearcher method aggregate.
@Override
public <T> Aggregation<T> aggregate(AggregationBuilder<T> aggregation) throws IOException {
final String aggregationName = aggregation.getName();
final EsClient client = admin.client();
final DocumentMapping mapping = admin.mappings().getMapping(aggregation.getFrom());
final EsQueryBuilder esQueryBuilder = new EsQueryBuilder(mapping, admin.settings(), admin.log());
final QueryBuilder esQuery = esQueryBuilder.build(aggregation.getQuery());
final SearchRequest req = new SearchRequest(admin.getTypeIndex(mapping));
final SearchSourceBuilder reqSource = req.source().query(esQuery).size(0).trackScores(false).trackTotalHitsUpTo(Integer.MAX_VALUE);
// field selection
final boolean fetchSource = applySourceFiltering(aggregation.getFields(), mapping, reqSource);
reqSource.aggregation(toEsAggregation(mapping, aggregation, fetchSource));
SearchResponse response = null;
try {
response = client.search(req);
} catch (Exception e) {
admin.log().error("Couldn't execute aggregation", e);
throw new IndexException("Couldn't execute aggregation: " + e.getMessage(), null);
}
ImmutableMap.Builder<Object, Bucket<T>> buckets = ImmutableMap.builder();
Aggregations topLevelAggregations = response.getAggregations();
Nested nested = topLevelAggregations.get(nestedAggName(aggregation));
Terms aggregationResult;
if (nested != null) {
aggregationResult = nested.getAggregations().get(aggregationName);
} else {
aggregationResult = topLevelAggregations.get(aggregationName);
}
for (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket bucket : aggregationResult.getBuckets()) {
final TopHits topHits;
if (nested != null) {
final ReverseNested reverseNested = bucket.getAggregations().get(reverseNestedAggName(aggregation));
topHits = reverseNested.getAggregations().get(topHitsAggName(aggregation));
} else {
topHits = bucket.getAggregations().get(topHitsAggName(aggregation));
}
Hits<T> hits;
if (topHits != null) {
hits = toHits(aggregation.getSelect(), List.of(aggregation.getFrom()), aggregation.getFields(), fetchSource, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount(), null, topHits.getHits());
} else {
hits = new Hits<>(Collections.emptyList(), null, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount());
}
buckets.put(bucket.getKey(), new Bucket<>(bucket.getKey(), hits));
}
return new Aggregation<>(aggregationName, buckets.build());
}
Aggregations