org.elasticsearch.search.aggregations.AggregationBuilders Java Examples
The following examples show how to use
org.elasticsearch.search.aggregations.AggregationBuilders.
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: GroupConditionBuilder.java From search-spring-boot-starter with Apache License 2.0 | 7 votes |
public AbstractAggregationBuilder getAggregationFunction(FunctionCondition functionCondition) { final String field = functionCondition.getField(); final SqlFunctionEnum sqlFunction = functionCondition.getFunction(); final String functionName = functionCondition.getFunctionName(); AbstractAggregationBuilder aggregation; switch (sqlFunction) { case AVG: aggregation = AggregationBuilders.avg(functionName).field(field); break; case MAX: aggregation = AggregationBuilders.max(functionName).field(field); break; case MIN: aggregation = AggregationBuilders.min(functionName).field(field); break; case SUM: aggregation = AggregationBuilders.sum(functionName).field(field); break; case COUNT: aggregation = AggregationBuilders.count(functionName).field(field); break; default: throw new RuntimeException("不支持的聚合类型"); } return aggregation; }
Example #2
Source File: CommonWebpageDAO.java From spider with GNU General Public License v3.0 | 7 votes |
/** * 获取query的关联信息 * * @param query 查询queryString * @param size 结果集数量 * @return 相关信息 */ public Pair<Map<String, List<Terms.Bucket>>, List<Webpage>> relatedInfo(String query, int size) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME) .setTypes(TYPE_NAME) .setQuery(QueryBuilders.queryStringQuery(query)) .addSort("gatherTime", SortOrder.DESC) .addAggregation(AggregationBuilders.terms("relatedPeople").field("namedEntity.nr")) .addAggregation(AggregationBuilders.terms("relatedLocation").field("namedEntity.ns")) .addAggregation(AggregationBuilders.terms("relatedInstitution").field("namedEntity.nt")) .addAggregation(AggregationBuilders.terms("relatedKeywords").field("keywords")) .setSize(size); SearchResponse response = searchRequestBuilder.execute().actionGet(); Map<String, List<Terms.Bucket>> info = Maps.newHashMap(); info.put("relatedPeople", ((Terms) response.getAggregations().get("relatedPeople")).getBuckets()); info.put("relatedLocation", ((Terms) response.getAggregations().get("relatedLocation")).getBuckets()); info.put("relatedInstitution", ((Terms) response.getAggregations().get("relatedInstitution")).getBuckets()); info.put("relatedKeywords", ((Terms) response.getAggregations().get("relatedKeywords")).getBuckets()); return Pair.of(info, warpHits2List(response.getHits())); }
Example #3
Source File: CommonWebpageDAO.java From Gather-Platform with GNU General Public License v3.0 | 6 votes |
/** * 统计指定网站每天抓取数量 * * @param domain 网站域名 * @return */ public Map<Date, Long> countDomainByGatherTime(String domain) { AggregationBuilder aggregation = AggregationBuilders .dateHistogram("agg") .field("gatherTime") .dateHistogramInterval(DateHistogramInterval.DAY).order(Histogram.Order.KEY_DESC); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME) .setTypes(TYPE_NAME) .setQuery(QueryBuilders.matchQuery("domain", domain)) .addAggregation(aggregation); SearchResponse response = searchRequestBuilder.execute().actionGet(); Histogram agg = response.getAggregations().get("agg"); Map<Date, Long> result = Maps.newHashMap(); for (Histogram.Bucket entry : agg.getBuckets()) { DateTime key = (DateTime) entry.getKey(); // Key long docCount = entry.getDocCount(); // Doc count result.put(key.toDate(), docCount); } return result; }
Example #4
Source File: EsSearchRequestBuilder.java From occurrence with Apache License 2.0 | 6 votes |
private static TermsAggregationBuilder buildTermsAggs( String aggsName, OccurrenceEsField esField, int facetOffset, int facetLimit, Integer minCount) { // build aggs for the field TermsAggregationBuilder termsAggsBuilder = AggregationBuilders.terms(aggsName).field(esField.getFieldName()); // min count Optional.ofNullable(minCount).ifPresent(termsAggsBuilder::minDocCount); // aggs size int size = calculateAggsSize(esField, facetOffset, facetLimit); termsAggsBuilder.size(size); // aggs shard size termsAggsBuilder.shardSize( CARDINALITIES.getOrDefault(esField, DEFAULT_SHARD_SIZE.applyAsInt(size))); return termsAggsBuilder; }
Example #5
Source File: SalesTrendsQueryAdapter.java From micronaut-microservices-poc with Apache License 2.0 | 6 votes |
@Override SearchRequest buildQuery() { SearchRequest searchRequest = new SearchRequest("policy_stats") .types("policy_type"); BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery(); if (query.getFilterByProductCode()!=null) { filterBuilder.must(QueryBuilders.termQuery("productCode.keyword", query.getFilterByProductCode())); } if (query.getFilterBySalesDate()!=null){ RangeQueryBuilder datesRange = QueryBuilders .rangeQuery("from") .gte(query.getFilterBySalesDate().getFrom().toString()) .lt(query.getFilterBySalesDate().getTo().toString()); filterBuilder.must(datesRange); } AggregationBuilder aggBuilder = AggregationBuilders.filter("agg_filter",filterBuilder); DateHistogramAggregationBuilder histBuilder = AggregationBuilders .dateHistogram("sales") .field("from") .dateHistogramInterval(query.getAggregationUnit().toDateHistogramInterval()) .subAggregation(AggregationBuilders.sum("total_premium").field("totalPremium")); aggBuilder.subAggregation(histBuilder); SearchSourceBuilder srcBuilder = new SearchSourceBuilder() .aggregation(aggBuilder) .size(0); searchRequest.source(srcBuilder); return searchRequest; }
Example #6
Source File: SearchFeatureDaoTests.java From anomaly-detection with Apache License 2.0 | 6 votes |
@Test public void test_getLatestDataTime_returnEmpty_givenNoData() { // pre-conditions SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() .aggregation(AggregationBuilders.max(SearchFeatureDao.AGG_NAME_MAX).field(detector.getTimeField())) .size(0); searchRequest.source(searchSourceBuilder); when(searchResponse.getAggregations()).thenReturn(null); // test Optional<Long> result = searchFeatureDao.getLatestDataTime(detector); // verify assertFalse(result.isPresent()); }
Example #7
Source File: ElasticsearchSearchDao.java From metron with Apache License 2.0 | 6 votes |
private TermsAggregationBuilder getGroupsTermBuilder(GroupRequest groupRequest, int index) { List<Group> groups = groupRequest.getGroups(); Group group = groups.get(index); String aggregationName = getGroupByAggregationName(group.getField()); TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(aggregationName); termsBuilder .field(group.getField()) .size(accessConfig.getMaxSearchGroups()) .order(getElasticsearchGroupOrder(group.getOrder())); if (index < groups.size() - 1) { termsBuilder.subAggregation(getGroupsTermBuilder(groupRequest, index + 1)); } Optional<String> scoreField = groupRequest.getScoreField(); if (scoreField.isPresent()) { SumAggregationBuilder scoreSumAggregationBuilder = AggregationBuilders.sum(getSumAggregationName(scoreField.get())).field(scoreField.get()).missing(0); termsBuilder.subAggregation(scoreSumAggregationBuilder); } return termsBuilder; }
Example #8
Source File: ElasticsearchSearchQueryBase.java From vertexium with Apache License 2.0 | 6 votes |
protected List<AggregationBuilder> getElasticsearchGeohashAggregations(GeohashAggregation agg) { List<AggregationBuilder> aggs = new ArrayList<>(); PropertyDefinition propertyDefinition = getPropertyDefinition(agg.getFieldName()); if (propertyDefinition == null) { throw new VertexiumException("Unknown property " + agg.getFieldName() + " for geohash aggregation."); } if (propertyDefinition.getDataType() != GeoPoint.class) { throw new VertexiumNotSupportedException("Only GeoPoint properties are valid for Geohash aggregation. Invalid property " + agg.getFieldName()); } for (String propertyName : getPropertyNames(agg.getFieldName())) { String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName); String aggName = createAggregationName(agg.getAggregationName(), visibilityHash); GeoGridAggregationBuilder geoHashAgg = AggregationBuilders.geohashGrid(aggName); geoHashAgg.field(propertyName + Elasticsearch7SearchIndex.GEO_POINT_PROPERTY_NAME_SUFFIX); geoHashAgg.precision(agg.getPrecision()); aggs.add(geoHashAgg); } return aggs; }
Example #9
Source File: Search.java From elasticsearch-rest-command with The Unlicense | 6 votes |
public void executeTimeline(final ActionListener<SearchResponse> listener, String interval, String timelineField) { timelineSearch.setSearchType(SearchType.COUNT); DateHistogramBuilder timeline = AggregationBuilders .dateHistogram("data_over_time"); if (timelineField == null) timeline.field("_timestamp"); else timeline.field(timelineField); try { long intervalAtMilli = Long.parseLong(interval); timeline.interval(intervalAtMilli); } catch (NumberFormatException e) { timeline.interval(new Interval(interval)); } timelineSearch.addAggregation(timeline); dumpSearchScript(timelineSearch, logger); timelineSearch.execute(listener); }
Example #10
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 #11
Source File: ElasticsearchTransactionRepository.java From servicecomb-pack with Apache License 2.0 | 6 votes |
public Map<String, Long> getTransactionStatistics() { TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders .terms("count_group_by_state").field("state.keyword"); SearchQuery searchQuery = new NativeSearchQueryBuilder() .withIndices(INDEX_NAME) .addAggregation(termsAggregationBuilder) .build(); return this.template.query(searchQuery, response -> { Map<String, Long> statistics = new HashMap<>(); if (response.getHits().totalHits > 0) { final StringTerms groupState = response.getAggregations().get("count_group_by_state"); statistics = groupState.getBuckets() .stream() .collect(Collectors.toMap(MultiBucketsAggregation.Bucket::getKeyAsString, MultiBucketsAggregation.Bucket::getDocCount)); } return statistics; }); }
Example #12
Source File: NpmSearchFacetHosted.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public Content searchV1(final Parameters parameters) throws IOException { String text = npmSearchParameterExtractor.extractText(parameters); int size = npmSearchParameterExtractor.extractSize(parameters); int from = npmSearchParameterExtractor.extractFrom(parameters); // npm search V1 endpoint currently returns an empty result set if no text is provided in the request NpmSearchResponse response; if (text.isEmpty()) { response = npmSearchResponseFactory.buildEmptyResponse(); } else { QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(text) .allowLeadingWildcard(true) .analyzeWildcard(true); TermsBuilder terms = AggregationBuilders.terms("name") .field("assets.attributes.npm.name") .size(v1SearchMaxResults) .subAggregation(AggregationBuilders.topHits("versions") .addSort(SortBuilders.fieldSort("assets.attributes.npm.search_normalized_version") .order(SortOrder.DESC)) .setTrackScores(true) .setSize(1)); SearchResponse searchResponse = searchQueryService.search( repositoryQuery(query).inRepositories(getRepository()), singletonList(terms)); Aggregations aggregations = searchResponse.getAggregations(); Terms nameTerms = aggregations.get("name"); response = npmSearchResponseFactory.buildResponseForResults(nameTerms.getBuckets(), size, from); } String content = npmSearchResponseMapper.writeString(response); return new Content(new StringPayload(content, ContentTypes.APPLICATION_JSON)); }
Example #13
Source File: MetricsQueryEs7DAO.java From skywalking with Apache License 2.0 | 5 votes |
@Override public int readMetricsValue(final MetricsCondition condition, final String valueColumnName, final Duration duration) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); buildQuery(sourceBuilder, condition, duration); TermsAggregationBuilder entityIdAggregation = AggregationBuilders.terms(Metrics.ENTITY_ID) .field(Metrics.ENTITY_ID) .size(1); final Function function = ValueColumnMetadata.INSTANCE.getValueFunction(condition.getName()); functionAggregation(function, entityIdAggregation, valueColumnName); sourceBuilder.aggregation(entityIdAggregation); SearchResponse response = getClient().search(condition.getName(), sourceBuilder); Terms idTerms = response.getAggregations().get(Metrics.ENTITY_ID); for (Terms.Bucket idBucket : idTerms.getBuckets()) { switch (function) { case Sum: Sum sum = idBucket.getAggregations().get(valueColumnName); return (int) sum.getValue(); case Avg: Avg avg = idBucket.getAggregations().get(valueColumnName); return (int) avg.getValue(); default: avg = idBucket.getAggregations().get(valueColumnName); return (int) avg.getValue(); } } return ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName()); }
Example #14
Source File: AvgAggregationParser.java From elasticsearch-sql with MIT License | 5 votes |
@Override public void parseAggregateItemClauseContext(AggregateQuery aggregateQuery, ElasticsearchParser.AggregateItemClauseContext aggregateItemClauseContext) { if (StringUtils.equalsIgnoreCase(aggregateItemClauseContext.ID().getText(), AGG_AVG_METHOD)) { try { String field = aggregateItemClauseContext.collection().identity(0).getText(); aggregateQuery.setAggregationBuilder(AggregationBuilders.avg(field + "_" + AGG_AVG_METHOD).field(field)); } catch (IllegalArgumentException e) { throw new ElasticSql2DslException(e); } } }
Example #15
Source File: CommonWebpageDAO.java From spider with GNU General Public License v3.0 | 5 votes |
/** * 统计制度域名的网站的文章的词频 * * @param domain 网站域名 * @return 词-词频 */ public Map<String, Long> countWordByDomain(String domain) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME) .setTypes(TYPE_NAME) .setQuery(QueryBuilders.matchQuery("domain", domain)) .addAggregation(AggregationBuilders.terms("content").field("content").size(200)); SearchResponse response = searchRequestBuilder.execute().actionGet(); Terms termsAgg = response.getAggregations().get("content"); List<Terms.Bucket> list = termsAgg.getBuckets(); Map<String, Long> count = new HashMap<>(); list.stream().filter(bucket -> ((String) bucket.getKey()).length() > 1).forEach(bucket -> { count.put((String) bucket.getKey(), bucket.getDocCount()); }); return count; }
Example #16
Source File: ElasticsearchSearchQueryBase.java From vertexium with Apache License 2.0 | 5 votes |
protected List<AggregationBuilder> getElasticsearchCardinalityAggregations(CardinalityAggregation agg) { List<AggregationBuilder> cardinalityAggs = new ArrayList<>(); String fieldName = agg.getPropertyName(); if (Element.ID_PROPERTY_NAME.equals(fieldName) || Edge.LABEL_PROPERTY_NAME.equals(fieldName) || Edge.OUT_VERTEX_ID_PROPERTY_NAME.equals(fieldName) || Edge.IN_VERTEX_ID_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.TABLE_NAME.equals(fieldName) || ExtendedDataRow.ROW_ID.equals(fieldName) || ExtendedDataRow.ELEMENT_ID.equals(fieldName) || ExtendedDataRow.ELEMENT_TYPE.equals(fieldName)) { Map<String, Object> metadata = new HashMap<>(); metadata.put(AGGREGATION_METADATA_FIELD_NAME_KEY, fieldName); if (Element.ID_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.ELEMENT_ID.equals(fieldName)) { fieldName = ELEMENT_ID_FIELD_NAME; } else if (ExtendedDataRow.ELEMENT_TYPE.equals(fieldName)) { fieldName = ELEMENT_TYPE_FIELD_NAME; } String aggregationName = createAggregationName(agg.getAggregationName(), "0"); CardinalityAggregationBuilder cardinalityAgg = AggregationBuilders.cardinality(aggregationName); cardinalityAgg.setMetaData(metadata); cardinalityAgg.field(fieldName); cardinalityAggs.add(cardinalityAgg); } else { throw new VertexiumException("Cannot use cardinality aggregation on properties with visibility: " + fieldName); } return cardinalityAggs; }
Example #17
Source File: CommonWebpageDAO.java From Gather-Platform with GNU General Public License v3.0 | 5 votes |
/** * 统计制度域名的网站的文章的词频 * * @param domain 网站域名 * @return 词-词频 */ public Map<String, Long> countWordByDomain(String domain) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME) .setTypes(TYPE_NAME) .setQuery(QueryBuilders.matchQuery("domain", domain)) .addAggregation(AggregationBuilders.terms("content").field("content").size(200)); SearchResponse response = searchRequestBuilder.execute().actionGet(); Terms termsAgg = response.getAggregations().get("content"); List<Terms.Bucket> list = termsAgg.getBuckets(); Map<String, Long> count = new HashMap<>(); list.stream().filter(bucket -> ((String) bucket.getKey()).length() > 1).forEach(bucket -> { count.put((String) bucket.getKey(), bucket.getDocCount()); }); return count; }
Example #18
Source File: AgentSalesQueryAdapter.java From micronaut-microservices-poc with Apache License 2.0 | 5 votes |
@Override SearchRequest buildQuery() { SearchRequest searchRequest = new SearchRequest("policy_stats") .types("policy_type"); BoolQueryBuilder filterBuilder = QueryBuilders.boolQuery(); if (query.getFilterByAgentLogin() != null) { filterBuilder.must(QueryBuilders.termQuery("agentLogin.keyword", query.getFilterByAgentLogin())); } if (query.getFilterByProductCode() != null) { filterBuilder.must(QueryBuilders.termQuery("productCode.keyword", query.getFilterByProductCode())); } if (query.getFilterBySalesDate() != null) { RangeQueryBuilder datesRange = QueryBuilders .rangeQuery("from") .gte(query.getFilterBySalesDate().getFrom().toString()) .lt(query.getFilterBySalesDate().getTo().toString()); filterBuilder.must(datesRange); } AggregationBuilder aggBuilder = AggregationBuilders.filter("agg_filter", filterBuilder); TermsAggregationBuilder sumAggBuilder = AggregationBuilders .terms("count_by_agent") .field("agentLogin.keyword") .subAggregation(AggregationBuilders.sum("total_premium").field("totalPremium")); aggBuilder.subAggregation(sumAggBuilder); SearchSourceBuilder srcBuilder = new SearchSourceBuilder() .aggregation(aggBuilder) .size(0); searchRequest.source(srcBuilder); return searchRequest; }
Example #19
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 #20
Source File: ElasticsearchHelperService.java From xmfcn-spring-cloud with Apache License 2.0 | 5 votes |
/** * 按天统计 各个日志级别的数量 查询条件 * * @param EsModel es * keywords * highlights * @return */ public Search statisticsDayCondition(EsModel es) { Search search = null; if (es == null) { return search; } String indexName = es.getIndex(); String type = es.getType(); String startTime = es.getStartTime(); String endTime = es.getEndTime(); JSONObject keywords = es.getKeyWord(); if (StringUtil.isBlank(indexName)) { return search; } if (StringUtil.isBlank(type)) { return search; } SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); if (StringUtil.isNotBlank(startTime) && StringUtil.isNotBlank(endTime)) { RangeQueryBuilder rangequerybuilder = QueryBuilders .rangeQuery("createTime") .from(startTime).to(endTime); searchSourceBuilder.query(rangequerybuilder); } AddKeyWords(keywords, searchSourceBuilder); ExtendedBounds extendedBounds = new ExtendedBounds(startTime, endTime); AggregationBuilder levelAgg = AggregationBuilders.terms("level_count").field("level").minDocCount(0); AggregationBuilder dateAgg = AggregationBuilders.dateHistogram("day_count") .field("createTime") .dateHistogramInterval(DateHistogramInterval.DAY) .format("yyyy-MM-dd") .extendedBounds(extendedBounds) .minDocCount(0L)//为空0补充 .timeZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+8"))); AggregationBuilder builder = dateAgg.subAggregation(levelAgg); searchSourceBuilder.aggregation(builder).size(0); search = new Search.Builder(searchSourceBuilder.toString()) .addIndex(indexName).addType(type).build(); return search; }
Example #21
Source File: ElasticsearchSearchQueryBase.java From vertexium with Apache License 2.0 | 5 votes |
protected List<AbstractAggregationBuilder> getElasticsearchStatisticsAggregations(StatisticsAggregation agg) { List<AbstractAggregationBuilder> aggs = new ArrayList<>(); for (String propertyName : getPropertyNames(agg.getFieldName())) { String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName); String aggName = createAggregationName(agg.getAggregationName(), visibilityHash); ExtendedStatsAggregationBuilder statsAgg = AggregationBuilders.extendedStats(aggName); statsAgg.field(propertyName); aggs.add(statsAgg); } return aggs; }
Example #22
Source File: AnalyticsServiceElasticsearch.java From hawkular-apm with Apache License 2.0 | 5 votes |
@Override public List<NodeTimeseriesStatistics> getNodeTimeseriesStatistics(String tenantId, Criteria criteria, long interval) { String index = client.getIndex(tenantId); if (!refresh(index)) { return null; } AvgBuilder avgBuilder = AggregationBuilders .avg("avg") .field(ElasticsearchUtil.ACTUAL_FIELD); TermsBuilder componentsBuilder = AggregationBuilders .terms("components") .field("componentType") .size(criteria.getMaxResponseSize()) .subAggregation(avgBuilder); DateHistogramBuilder histogramBuilder = AggregationBuilders .dateHistogram("histogram") .interval(interval) .field(ElasticsearchUtil.TIMESTAMP_FIELD) .subAggregation(componentsBuilder); BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, NodeDetails.class); SearchRequestBuilder request = getNodeDetailsRequest(index, criteria, query, 0) .addAggregation(histogramBuilder); SearchResponse response = getSearchResponse(request); DateHistogram histogram = response.getAggregations().get("histogram"); return histogram.getBuckets().stream() .map(AnalyticsServiceElasticsearch::toNodeTimeseriesStatistics) .collect(Collectors.toList()); }
Example #23
Source File: GroupParser.java From sql4es with Apache License 2.0 | 5 votes |
/** * Adds aggregations recursively * All metric columns are added to last aggregation * @param aggs * @param index * @param state * @return */ private TermsAggregationBuilder buildAggregationQuery(List<Column> aggs, int index, QueryState state){ Column agg = aggs.get(index); TermsAggregationBuilder result = null; if(aggs.get(index).getOp() == Operation.NONE){ result = AggregationBuilders.terms(agg.getAggName()).field(agg.getColumn()); result.size(state.getIntProp(Utils.PROP_FETCH_SIZE, 10000)); } if(index < aggs.size() - 1) result.subAggregation(buildAggregationQuery(aggs, index+1, state)); else addMetrics(result, state.getHeading(), true); return result; }
Example #24
Source File: CardinalityEstimationTask.java From siren-join with GNU Affero General Public License v3.0 | 5 votes |
protected SearchRequest getCardinalityRequest(FilterJoinNode node, ActionRequest parentRequest) { String[] lookupIndices = node.getLookupIndices(); String[] lookupTypes = node.getLookupTypes(); String lookupPath = node.getLookupPath(); // Build the search source with the aggregate definition SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0).aggregation(AggregationBuilders.cardinality(lookupPath).field(lookupPath)); // Build search request with reference to the parent request SearchRequest searchRequest = new SearchRequest(parentRequest); searchRequest.indices(lookupIndices).types(lookupTypes).source(sourceBuilder); return searchRequest; }
Example #25
Source File: AvgAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ AvgAggregationBuilder aggregationBuilder = AggregationBuilders.avg("utm").field("utm").missing(0); SearchRequest searchRequest = new SearchRequest("serverlog_20180715");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.aggregation(aggregationBuilder); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); //统计结果 Aggregations aggregations = searchResponse.getAggregations(); Map<String, Aggregation> aggregationMap = aggregations.asMap(); for(Map.Entry<String,Aggregation> each: aggregationMap.entrySet()){ System.out.println(((ParsedAvg)(each.getValue())).getValue()); } }finally{ HighLevelClient.close(); } }
Example #26
Source File: ElasticQueryBuilder.java From vind with Apache License 2.0 | 5 votes |
private static List<AggregationBuilder> getStatsAggregationBuilders(String searchContext, String contextualizedFacetName, Facet.StatsFacet statsFacet) { final List<AggregationBuilder> statsAggs = new ArrayList<>(); final ExtendedStatsAggregationBuilder statsAgg = AggregationBuilders .extendedStats(contextualizedFacetName) .field(FieldUtil.getFieldName(statsFacet.getField(), searchContext)); if (ArrayUtils.isNotEmpty(statsFacet.getPercentiles())) { statsAggs.add(AggregationBuilders .percentileRanks(contextualizedFacetName + "_percentiles", ArrayUtils.toPrimitive(statsFacet.getPercentiles())) .field(FieldUtil.getFieldName(statsFacet.getField(), searchContext)) ); } if (statsFacet.getCardinality()) { statsAggs.add(AggregationBuilders .cardinality(contextualizedFacetName + "_cardinality") .field(FieldUtil.getFieldName(statsFacet.getField(), searchContext)) ); } if (statsFacet.getCountDistinct() || statsFacet.getDistinctValues()) { statsAggs.add(AggregationBuilders .terms(contextualizedFacetName + "_values") .field(FieldUtil.getFieldName(statsFacet.getField(), searchContext)) ); } if (statsFacet.getMissing()) { statsAggs.add(AggregationBuilders .missing(contextualizedFacetName + "_missing") .field(FieldUtil.getFieldName(statsFacet.getField(), searchContext)) ); } statsAggs.add(statsAgg); return statsAggs; }
Example #27
Source File: DateHistogramAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ DateHistogramAggregationBuilder dateHistogramAggregationBuilder = AggregationBuilders.dateHistogram("ctm_date_histogram"); dateHistogramAggregationBuilder.field("ctm");//设置直方图针对的字段 dateHistogramAggregationBuilder.dateHistogramInterval(DateHistogramInterval.hours(6));//直方图每个分组对应的范围 dateHistogramAggregationBuilder.timeZone(DateTimeZone.forOffsetHours(8));//时区偏移 dateHistogramAggregationBuilder.keyed(true);//是否需要key名 dateHistogramAggregationBuilder.format("yyyy-MM-dd HH:mm");//key名格式 // dateHistogramAggregationBuilder.order(BucketOrder.aggregation("_key",true));//分组key的排序 // dateHistogramAggregationBuilder.minDocCount(0);//对于每个分组最少具有多少条数据,少于这个设置,则该分组不显示 // dateHistogramAggregationBuilder.extendedBounds(0,8000);//设置分组区间的下线和上线,只有当min_doc_count为0时有效 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("cmd","weather_info"); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.aggregation(dateHistogramAggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example #28
Source File: CustomSearchRepositoryImpl.java From klask-io with GNU General Public License v3.0 | 5 votes |
@Override public Map<String, Long> aggregateByRawField(String field, String filtre) { TermsBuilder aggregation = AggregationBuilders.terms("top_" + field) .field(field + ".raw") .size(0)// le résultat n'est pas complet si on ne précise pas la taille, 0 : infini // (voir : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_size) .order(Terms.Order.aggregation("_count", false)); SearchResponse response = createResponse(filtre, aggregation); Map<String, Aggregation> results = response.getAggregations().asMap(); StringTerms topField = (StringTerms) results.get("top_" + field); //sur l'ensemble des buckets, triés par ordre décroissant sur le nombre de documents // on retourne une Map (LinkedHashMap) pour conserver l'ordre avec la clé, le nom du champ (exemple version), et la valeur, le nombre de docs //exemple : // "trunk" -> 34012 // "branche_1" -> 35800 return topField.getBuckets() .stream() .sorted(Comparator.comparing(Terms.Bucket::getDocCount).reversed()) .collect( Collectors.toMap(bucket -> bucket.getKeyAsString(), bucket -> bucket.getDocCount(), (v1, v2) -> v1, LinkedHashMap::new )); }
Example #29
Source File: AST_Stats.java From elasticsearch-rest-command with The Unlicense | 5 votes |
public static AbstractAggregationBuilder newCount(Function func) { ValueCountBuilder count; count = AggregationBuilders.count(Function.genStatField(func)) .field(func.field); return count; }
Example #30
Source File: AggregationHelper.java From fast-elasticsearch-query-builder with Apache License 2.0 | 5 votes |
private static void setCardinalityAggregation(SearchSourceBuilder searchSource, CardinalityAggregation aggregation, Boolean value) { if (!value) { return; } CardinalityAggregationBuilder cardinalityAggregation = AggregationBuilders.cardinality(aggregation.name()).field(aggregation.field()); long precisionThreshold = aggregation.precisionThreshold(); if (precisionThreshold > 0) { cardinalityAggregation.precisionThreshold(precisionThreshold); } searchSource.aggregation(cardinalityAggregation); }