org.elasticsearch.search.aggregations.AggregationBuilder Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.AggregationBuilder. 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: EsAggregationSearchTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
private static SearchResponse search(AggregationBuilder aggregation) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("student");
    searchRequest.types("_doc");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    //不需要解释
    searchSourceBuilder.explain(false);
    //不需要原始数据
    searchSourceBuilder.fetchSource(false);
    //不需要版本号
    searchSourceBuilder.version(false);
    searchSourceBuilder.aggregation(aggregation);
    logger.info("查询的语句:"+searchSourceBuilder.toString());
    searchRequest.source(searchSourceBuilder);
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    return  searchResponse;
}
 
Example #2
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
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 + Elasticsearch5SearchIndex.GEO_POINT_PROPERTY_NAME_SUFFIX);
        geoHashAgg.precision(agg.getPrecision());
        aggs.add(geoHashAgg);
    }
    return aggs;
}
 
Example #3
Source File: AggregationQueryAction.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private AggregationBuilder createNestedAggregation(Field field) {
    AggregationBuilder nestedBuilder;

    String nestedPath = field.getNestedPath();

    if (field.isReverseNested()) {
        if (nestedPath == null || !nestedPath.startsWith("~")) {
            ReverseNestedAggregationBuilder reverseNestedAggregationBuilder = AggregationBuilders.reverseNested(getNestedAggName(field));
            if(nestedPath!=null){
                reverseNestedAggregationBuilder.path(nestedPath);
            }
            return reverseNestedAggregationBuilder;
        }
        nestedPath = nestedPath.substring(1);
    }

    nestedBuilder = AggregationBuilders.nested(getNestedAggName(field),nestedPath);

    return nestedBuilder;
}
 
Example #4
Source File: EsHeatmapRequestBuilder.java    From occurrence with Apache License 2.0 6 votes vote down vote up
private static AggregationBuilder buildAggs(OccurrenceHeatmapRequest request) {
  GeoGridAggregationBuilder geoGridAggs =
      AggregationBuilders.geohashGrid(HEATMAP_AGGS)
          .field(OccurrenceEsField.COORDINATE_POINT.getFieldName())
          .precision(PRECISION_LOOKUP[Math.min(request.getZoom(), PRECISION_LOOKUP.length - 1)]);

  if (OccurrenceHeatmapRequest.Mode.GEO_CENTROID == request.getMode()) {
    GeoCentroidAggregationBuilder geoCentroidAggs = AggregationBuilders.geoCentroid(CELL_AGGS)
                                                      .field(OccurrenceEsField.COORDINATE_POINT.getFieldName());
    geoGridAggs.subAggregation(geoCentroidAggs);
  } else {
    GeoBoundsAggregationBuilder geoBoundsAggs = AggregationBuilders.geoBounds(CELL_AGGS)
                                                  .field(OccurrenceEsField.COORDINATE_POINT.getFieldName());
    geoGridAggs.subAggregation(geoBoundsAggs);
  }

  return geoGridAggs;
}
 
Example #5
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private AggregationBuilder geoBounds(MethodField field) throws SqlParseException {
    String aggName = gettAggNameFromParamsOrAlias(field);
    GeoBoundsAggregationBuilder boundsBuilder = AggregationBuilders.geoBounds(aggName);
    String value = null;
    for (KVValue kv : field.getParams()) {
        value = kv.value.toString();
        switch (kv.key.toLowerCase()) {
            case "field":
                boundsBuilder.field(value);
                break;
            case "wrap_longitude":
                boundsBuilder.wrapLongitude(Boolean.getBoolean(value));
                break;
            case "alias":
            case "nested":
            case "reverse_nested":
            case "children":
                break;
            default:
                throw new SqlParseException("geo_bounds err or not define field " + kv.toString());
        }
    }
    return boundsBuilder;
}
 
Example #6
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
protected List<AggregationBuilder> getElasticsearchAggregations(Iterable<Aggregation> aggregations) {
    List<AggregationBuilder> aggs = new ArrayList<>();
    for (Aggregation agg : aggregations) {
        if (agg instanceof HistogramAggregation) {
            aggs.addAll(getElasticsearchHistogramAggregations((HistogramAggregation) agg));
        } else if (agg instanceof RangeAggregation) {
            aggs.addAll(getElasticsearchRangeAggregations((RangeAggregation) agg));
        } else if (agg instanceof PercentilesAggregation) {
            aggs.addAll(getElasticsearchPercentilesAggregations((PercentilesAggregation) agg));
        } else if (agg instanceof TermsAggregation) {
            aggs.addAll(getElasticsearchTermsAggregations((TermsAggregation) agg));
        } else if (agg instanceof GeohashAggregation) {
            aggs.addAll(getElasticsearchGeohashAggregations((GeohashAggregation) agg));
        } else if (agg instanceof StatisticsAggregation) {
            aggs.addAll(getElasticsearchStatisticsAggregations((StatisticsAggregation) agg));
        } else if (agg instanceof CalendarFieldAggregation) {
            aggs.addAll(getElasticsearchCalendarFieldAggregation((CalendarFieldAggregation) agg));
        } else if (agg instanceof CardinalityAggregation) {
            aggs.addAll(getElasticsearchCardinalityAggregations((CardinalityAggregation) agg));
        } else {
            throw new VertexiumException("Could not add aggregation of type: " + agg.getClass().getName());
        }
    }
    return aggs;
}
 
Example #7
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
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 #8
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
protected List<AggregationBuilder> getElasticsearchAggregations(Iterable<Aggregation> aggregations) {
    List<AggregationBuilder> aggs = new ArrayList<>();
    for (Aggregation agg : aggregations) {
        if (agg instanceof HistogramAggregation) {
            aggs.addAll(getElasticsearchHistogramAggregations((HistogramAggregation) agg));
        } else if (agg instanceof RangeAggregation) {
            aggs.addAll(getElasticsearchRangeAggregations((RangeAggregation) agg));
        } else if (agg instanceof PercentilesAggregation) {
            aggs.addAll(getElasticsearchPercentilesAggregations((PercentilesAggregation) agg));
        } else if (agg instanceof TermsAggregation) {
            aggs.addAll(getElasticsearchTermsAggregations((TermsAggregation) agg));
        } else if (agg instanceof GeohashAggregation) {
            aggs.addAll(getElasticsearchGeohashAggregations((GeohashAggregation) agg));
        } else if (agg instanceof StatisticsAggregation) {
            aggs.addAll(getElasticsearchStatisticsAggregations((StatisticsAggregation) agg));
        } else if (agg instanceof CalendarFieldAggregation) {
            aggs.addAll(getElasticsearchCalendarFieldAggregation((CalendarFieldAggregation) agg));
        } else if (agg instanceof CardinalityAggregation) {
            aggs.addAll(getElasticsearchCardinalityAggregations((CardinalityAggregation) agg));
        } else {
            throw new VertexiumException("Could not add aggregation of type: " + agg.getClass().getName());
        }
    }
    return aggs;
}
 
Example #9
Source File: SearchQueryServiceImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public SearchResponse search(final QueryBuilder query, final List<AggregationBuilder> aggregations) {
  if (!validateQuery(query)) {
    return EMPTY_SEARCH_RESPONSE;
  }

  checkNotNull(aggregations);

  RepositoryQueryBuilder repoQuery = repositoryQuery(query);
  final String[] searchableIndexes = getSearchableIndexes(repoQuery);
  if (searchableIndexes.length == 0) {
    return EMPTY_SEARCH_RESPONSE;
  }

  if (repoQuery.skipContentSelectors) {
    return executeSearch(repoQuery, searchableIndexes, aggregations, null);
  }

  try (SubjectRegistration registration = searchSubjectHelper.register(securityHelper.subject())) {
    QueryBuilder selectorFilter = scriptQuery(ContentAuthPluginScriptFactory.newScript(registration.getId()));
    return executeSearch(repoQuery, searchableIndexes, aggregations, selectorFilter);
  }
}
 
Example #10
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 统计指定网站每天抓取数量
 *
 * @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 #11
Source File: AggregateByQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private AggregationBuilder parseNestedAggregationClauseContext(ElasticsearchParser.NestedAggregationClauseContext nestedAggregationClauseContext) {
    String nestedPath = nestedAggregationClauseContext.nestedPath.getText();
    String nestedName = "nested_" + nestedPath;
    AggregationBuilder aggregationBuilder=null;
    if (nestedAggregationClauseContext.aggregationClause(0).nestedAggregationClause() != null) {
        AggregationBuilder nestedAggregationBuilder = parseNestedAggregationClauseContext(nestedAggregationClauseContext.aggregationClause(0).nestedAggregationClause());
        aggregationBuilder = AggregationBuilders.nested(nestedName, nestedPath).subAggregation(nestedAggregationBuilder);
    } else if (nestedAggregationClauseContext.aggregationClause(0).aggregateItemClause() != null) {
        List<AggregationBuilder> aggregationBuilders = parseAggregateItemClauseContext(nestedAggregationClauseContext.aggregationClause(0).aggregateItemClause());
        AggregationBuilder nestAggregationBuilder = AggregationBuilders.nested(nestedName, nestedPath);
        for (AggregationBuilder agg : aggregationBuilders) {
            nestAggregationBuilder.subAggregation(agg);
        }
        aggregationBuilder = nestAggregationBuilder;
    }
    if(aggregationBuilder!=null){
        if(nestedAggregationClauseContext.subAggregationClause().size()>0){
            parseSubAggregationClauseContext(aggregationBuilder,nestedAggregationClauseContext.subAggregationClause());
        }
        return aggregationBuilder;
    }else {
        throw new ElasticSql2DslException("[syntax error] nested aggregation not support this query format of the sql");
    }
}
 
Example #12
Source File: AggregateByQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private AggregationBuilder parseSubAggregationClauseContext(AggregationBuilder aggregationBuilder, List<ElasticsearchParser.SubAggregationClauseContext> subAggregationClauseContexts) {
    for (ElasticsearchParser.SubAggregationClauseContext subAggregationClauseContext : subAggregationClauseContexts) {
        if (subAggregationClauseContext.aggregationClause().aggregateItemClause() != null) {
            List<AggregationBuilder> aggregationItems = parseAggregateItemClauseContext(subAggregationClauseContext.aggregationClause().aggregateItemClause());
            for (AggregationBuilder aggItem : aggregationItems) {
                aggregationBuilder.subAggregation(aggItem);
            }
            return aggregationBuilder;
        } else if (subAggregationClauseContext.aggregationClause().nestedAggregationClause() != null) {
            AggregationBuilder nestedAggregationBuilder = parseNestedAggregationClauseContext(subAggregationClauseContext.aggregationClause().nestedAggregationClause());
            aggregationBuilder.subAggregation(nestedAggregationBuilder);
            for(int i=1;i<subAggregationClauseContext.aggregationClause().nestedAggregationClause().aggregationClause().size();i++){
                for(AggregationBuilder agg:parseAggregationClauseContext(subAggregationClauseContext.aggregationClause().nestedAggregationClause().aggregationClause(i))){
                    aggregationBuilder.subAggregation(agg);
                }
            }
            return aggregationBuilder;

        }
    }
    throw new ElasticSql2DslException("not support yet");
}
 
Example #13
Source File: CommonWebpageDAO.java    From Gather-Platform with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 统计指定网站每天抓取数量
 *
 * @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 #14
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private SearchRequest toRequest() {
    SearchRequest searchRequest = new SearchRequest();
    List<String> indexList = indices.parallelStream().map(StringManager::removeStringSymbol).collect(Collectors.toList());
    if (CollectionUtils.isNotEmpty(indexList)) {
        searchRequest.indices(indexList.toArray(new String[0]));
    } else {
        throw new ElasticSql2DslException("[syntax error] indices name must be set");
    }
    //这里不会修改from的值
    searchSourceBuilder.from(Math.max(from, 0));
    searchSourceBuilder.size(Math.max(size, 0));
    searchSourceBuilder.trackTotalHits(this.trackTotalHits);
    if (CollectionUtils.isNotEmpty(highlighter)) {
        HighlightBuilder highlightBuilder = HighlightBuilders.highlighter(highlighter);
        searchSourceBuilder.highlighter(highlightBuilder);
    }
    searchSourceBuilder.query(whereCondition);
    if (StringUtils.isNotBlank(distinctName)) {
        searchSourceBuilder.collapse(new CollapseBuilder(distinctName));
    }
    if (CollectionUtils.isNotEmpty(orderBy)) {
        for (SortBuilder sortBuilder : orderBy) {
            searchSourceBuilder.sort(sortBuilder);
        }
    }
    searchSourceBuilder.fetchSource(includeFields.toArray(new String[0]), excludeFields.toArray(new String[0]));
    if (CollectionUtils.isNotEmpty(routingBy)) {
        searchRequest.routing(routingBy.toArray(new String[0]));
    }
    if (CollectionUtils.isNotEmpty(groupBy)) {
        for (AggregationBuilder aggItem : groupBy) {
            searchSourceBuilder.aggregation(aggItem);
        }
    }
    return searchRequest.source(searchSourceBuilder);
}
 
Example #15
Source File: SalesTrendsQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 6 votes vote down vote up
@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 #16
Source File: AggregationQueryAction.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void setSize (AggregationBuilder agg, Field field) {
    if (field instanceof MethodField) { //zhongshu-comment MethodField可以自定义聚合的size
        MethodField mf = ((MethodField) field);
        Object customSize = mf.getParamsAsMap().get("size");
        if (customSize == null) { //zhongshu-comment 假如用户没有在MethodField指定agg的size,就将默认的rowCount设置为agg的size
            if(select.getRowCount()>0) {
                if (agg instanceof TermsAggregationBuilder) {
                    ((TermsAggregationBuilder) agg).size(select.getRowCount());
                }
            }
        } else {
            //zhongshu-comment 不需要任何操作,因为之前步骤的代码已经将自定义的size设置到agg对象中了
        }
    } else {
        if(select.getRowCount()>0) {
            if (agg instanceof TermsAggregationBuilder) {
                ((TermsAggregationBuilder) agg).size(select.getRowCount());
            }
        }
    }
}
 
Example #17
Source File: GroupConditionBuilder.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 封装查询请求的聚合条件
 *
 * @param rootTermsBuilder     聚合根
 * @param termsBuilder         当前聚合
 * @param functions            聚合函数集
 * @param searchRequestBuilder 聚合功能查询请求
 */
private void aggregationFunction(AggregationBuilder rootTermsBuilder, AggregationBuilder termsBuilder,
                                 final List<FunctionCondition> functions,
                                 final SearchRequestBuilder searchRequestBuilder) {

    for (FunctionCondition functionCondition : functions) {

        AbstractAggregationBuilder aggregation = getAggregationFunction(functionCondition);

        if (termsBuilder != null) {
            termsBuilder.subAggregation(aggregation);
        } else {
            searchRequestBuilder.addAggregation(aggregation);
        }
    }
    if (termsBuilder != null) {
        searchRequestBuilder.addAggregation(rootTermsBuilder);
    }
}
 
Example #18
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public MovFnPipelineAggregationBuilder makeMovingFieldAgg(MethodField field, AggregationBuilder parent) throws SqlParseException {
    //question 加到groupMap里是为了什么
    groupMap.put(field.getAlias(), new KVValue("FIELD", parent));

    String bucketPath = field.getParams().get(0).value.toString();
    int window = Integer.parseInt(field.getParams().get(1).value.toString());

    ValuesSourceAggregationBuilder builder;
    field.setAlias(fixAlias(field.getAlias()));
    switch (field.getName().toUpperCase()) {
        //added by xzb 增加 movingavg和rollingstd
        case "MOVINGAVG":
            MovFnPipelineAggregationBuilder mvAvg =
                    //PipelineAggregatorBuilders.movingFunction("movingAvgIncome", new Script("MovingFunctions.unweightedAvg(values)"), "incomeSum", 2);
                    PipelineAggregatorBuilders.movingFunction(field.getAlias(),  new Script("MovingFunctions.unweightedAvg(values)"), bucketPath, window);

            return mvAvg;

        case "ROLLINGSTD":
            MovFnPipelineAggregationBuilder stdDev =
                    //PipelineAggregatorBuilders.movingFunction("stdDevIncome", new Script("MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"), "incomeSum", 2);
                    PipelineAggregatorBuilders.movingFunction(field.getAlias() , new Script("MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"), bucketPath, window);

            return stdDev;
    }
    return  null;
}
 
Example #19
Source File: ParseResult.java    From sql4es with Apache License 2.0 5 votes vote down vote up
public ParseResult(Heading heading, List<QuerySource> sources, QueryBuilder query, AggregationBuilder aggregation,
		IComparison having, List<OrderBy> sorts, Integer limit, Boolean useCache, Boolean requestScore) {
	super();
	this.heading = heading;
	this.sources = sources;
	this.query = query;
	this.aggregation = aggregation;
	this.having = having;
	if(sorts != null) this.sorts = sorts;
	this.limit = limit;
	this.useCache = useCache;
	this.requestScore = requestScore;
}
 
Example #20
Source File: SearchServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public SearchResponse searchInReposWithAggregations(final QueryBuilder query,
                                                    final List<AggregationBuilder> aggregations,
                                                    final Collection<String> repoNames)
{
  RepositoryQueryBuilder repoQuery = repositoryQuery(query).inRepositories(repoNames);

  return searchQueryService.search(repoQuery, aggregations);
}
 
Example #21
Source File: SearchServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public SearchResponse searchUnrestrictedInReposWithAggregations(final QueryBuilder query,
                                                                final List<AggregationBuilder> aggregations,
                                                                @Nullable final List<SortBuilder> sort,
                                                                final Collection<String> repoNames)
{
  RepositoryQueryBuilder repoQuery = repositoryQuery(query).inRepositories(repoNames);

  if (sort != null) {
    repoQuery = repoQuery.sortBy(sort);
  }

  return searchQueryService.search(repoQuery.unrestricted(), aggregations);
}
 
Example #22
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private Collection<? extends AggregationBuilder> getElasticsearchCalendarFieldAggregation(CalendarFieldAggregation agg) {
    List<AggregationBuilder> aggs = new ArrayList<>();
    PropertyDefinition propertyDefinition = getPropertyDefinition(agg.getPropertyName());
    if (propertyDefinition == null) {
        throw new VertexiumException("Could not find mapping for property: " + agg.getPropertyName());
    }
    Class propertyDataType = propertyDefinition.getDataType();
    for (String propertyName : getPropertyNames(agg.getPropertyName())) {
        String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
        String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
        if (propertyDataType == Date.class) {
            HistogramAggregationBuilder histAgg = AggregationBuilders.histogram(aggName);
            histAgg.interval(1);
            if (agg.getMinDocumentCount() != null) {
                histAgg.minDocCount(agg.getMinDocumentCount());
            } else {
                histAgg.minDocCount(1L);
            }
            Script script = new Script(
                ScriptType.INLINE,
                "painless",
                getCalendarFieldAggregationScript(agg, propertyName),
                ImmutableMap.of(
                    "tzId", agg.getTimeZone().getID(),
                    "fieldName", propertyName,
                    "calendarField", agg.getCalendarField())
            );
            histAgg.script(script);

            for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) {
                histAgg.subAggregation(subAgg);
            }

            aggs.add(histAgg);
        } else {
            throw new VertexiumException("Only dates are supported for hour of day aggregations");
        }
    }
    return aggs;
}
 
Example #23
Source File: ElasticQueryBuilder.java    From vind with Apache License 2.0 5 votes vote down vote up
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 #24
Source File: ElasticSearchHelper.java    From testgrid with Apache License 2.0 5 votes vote down vote up
/**
 * Will return all indices given under a certain k8s namespace in the elastic search instance
 * @param nameSpace Namespace of which all available indices are required
 * @return ArrayList containing all indices which have the mentioned namespace
 */
public ArrayList<String> getAllIndexes(String nameSpace) {

    ArrayList<String> indexList = new ArrayList<>();
    open();
    if (esClient != null) {
        try {
            QueryBuilder namespaceMatchQuery = QueryBuilders.boolQuery()
                    .should(QueryBuilders.termQuery("namespace", nameSpace));
            AggregationBuilder uniqueIndexAggregator
                    = AggregationBuilders.terms("unique_ids").field("_index");

            SearchSourceBuilder querySourceBuilder = new SearchSourceBuilder();
            querySourceBuilder.aggregation(uniqueIndexAggregator).query(namespaceMatchQuery);

            SearchRequest searchRequest = new SearchRequest().source(querySourceBuilder);
            SearchResponse searchResponse = esClient.search(searchRequest);
            Terms aggregationOutput =  searchResponse.getAggregations().get("unique_ids");

            for (Terms.Bucket indexBucket : aggregationOutput.getBuckets()) {
                indexList.add(indexBucket.getKey().toString());
            }
            esClient.close();
        } catch (IOException e) {
            logger.error(e.getMessage());
            logger.error("Could not get index list");
        }
    } else {
        logger.error("Client not initialized Could not get index list");
    }

    return indexList;
}
 
Example #25
Source File: FactSearchManager.java    From act-platform with ISC License 5 votes vote down vote up
private AggregationBuilder buildObjectsAggregation(FactSearchCriteria criteria) {
  // 1. Reduce to Facts matching the search criteria.
  return filter(FILTER_FACTS_AGGREGATION_NAME, buildFactsQuery(criteria))
          // 2. Map to nested Object documents.
          .subAggregation(nested(NESTED_OBJECTS_AGGREGATION_NAME, "objects")
                  // 3. Reduce to Objects matching the search criteria.
                  .subAggregation(filter(FILTER_OBJECTS_AGGREGATION_NAME, buildObjectsQuery(criteria))
                          // 4. Calculate the number of unique Objects by id. This will give the 'count' value.
                          // If 'count' is smaller than MAX_RESULT_WINDOW a correct value is expected, thus,
                          // the precision threshold is set to MAX_RESULT_WINDOW.
                          .subAggregation(cardinality(OBJECTS_COUNT_AGGREGATION_NAME)
                                  .field("objects.id")
                                  .precisionThreshold(MAX_RESULT_WINDOW)
                          )
                          // 5. Reduce to buckets of unique Objects by id, restricted to the search criteria's limit.
                          // This will give the actual search results.
                          .subAggregation(terms(UNIQUE_OBJECTS_AGGREGATION_NAME)
                                  .field("objects.id")
                                  .size(calculateMaximumSize(criteria))
                                  // 6. Map to the unique Object's source. Set size to 1, because all Objects in one
                                  // bucket are the same (ignoring 'direction' which isn't relevant for Object search).
                                  .subAggregation(topHits(UNIQUE_OBJECTS_SOURCE_AGGREGATION_NAME)
                                          .size(1)
                                  )
                          )
                  )
          );
}
 
Example #26
Source File: GroupConditionBuilder.java    From search-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public SearchRequestBuilder build(final SearchRequestBuilder searchRequestBuilder, final QueryESObject esObject) {

        final GroupByCondition groupByCondition = esObject.getGroupByCondition();
        if (groupByCondition == null) {
            return searchRequestBuilder;
        }
        final List<String> groupFields = groupByCondition.getGroupFields();
        if (groupFields == null) {
            return searchRequestBuilder;
        }
        // 根聚合嵌套使用
        AggregationBuilder rootTermsBuilder = null;
        // 最底层嵌套聚合
        AggregationBuilder termTermsBuilder = null;
        for (int i = 0; i < groupFields.size(); i++) {
            if (rootTermsBuilder == null) {
                rootTermsBuilder = AggregationBuilders.terms(groupFields.get(i))
                        .field(groupFields.get(i));
            } else {
                if (null == termTermsBuilder || termTermsBuilder == rootTermsBuilder) {
                    termTermsBuilder = AggregationBuilders.terms(groupFields.get(i)).field(groupFields.get(i));
                    rootTermsBuilder.subAggregation(termTermsBuilder);
                } else {
                    final AggregationBuilder currentTermsBuilder = AggregationBuilders.terms(groupFields.get(i))
                            .field(groupFields.get(i))
                            .minDocCount(0);
                    termTermsBuilder.subAggregation(currentTermsBuilder);
                    termTermsBuilder = currentTermsBuilder;
                }
            }
        }

        aggregationFunction(rootTermsBuilder, termTermsBuilder, groupByCondition.getFunctionConditions(),
                searchRequestBuilder);

        return searchRequestBuilder;
    }
 
Example #27
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private AggregationBuilder geohashGrid(MethodField field) throws SqlParseException {
    String aggName = gettAggNameFromParamsOrAlias(field);
    GeoGridAggregationBuilder geoHashGrid = AggregationBuilders.geohashGrid(aggName);
    String value = null;
    for (KVValue kv : field.getParams()) {
        value = kv.value.toString();
        switch (kv.key.toLowerCase()) {
            case "precision":
                geoHashGrid.precision(Integer.parseInt(value));
                break;
            case "field":
                geoHashGrid.field(value);
                break;
            case "size":
                geoHashGrid.size(Integer.parseInt(value));
                break;
            case "shard_size":
                geoHashGrid.shardSize(Integer.parseInt(value));
                break;
            case "alias":
            case "nested":
            case "reverse_nested":
            case "children":
                break;
            default:
                throw new SqlParseException("geohash grid err or not define field " + kv.toString());
        }
    }
    return geoHashGrid;
}
 
Example #28
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * @Author pancm
 * @Description 平均聚合查询测试用例
 * @Date  2019/4/1
 * @Param []
 * @return void
 **/
private static  void avgSearch() throws IOException {

    String buk="t_grade_avg";
    //直接求平均数
    AggregationBuilder aggregation = AggregationBuilders.avg(buk).field("grade");
    logger.info("求班级的平均分数:");
    agg(aggregation,buk);

}
 
Example #29
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * @Author pancm
 * @Description 多个聚合条件测试
 * SQL: select age, name, count(*) as count1 from student group by age, name;
 * @Date  2019/7/3
 * @Param []
 * @return void
 **/
private static void groupbySearch() throws IOException{
    String buk="group";
    AggregationBuilder aggregation = AggregationBuilders.terms("age").field("age");
    AggregationBuilder aggregation2 = AggregationBuilders.terms("name").field("name");
    //根据创建时间按天分组
    AggregationBuilder aggregation3 = AggregationBuilders.dateHistogram("createtm")
            .field("createtm")
            .format("yyyy-MM-dd")
            .dateHistogramInterval(DateHistogramInterval.DAY);

    aggregation2.subAggregation(aggregation3);
    aggregation.subAggregation(aggregation2);
    agg(aggregation,buk);
}
 
Example #30
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseException {
    switch (field.getName().toLowerCase()) {
        case "range":
            return rangeBuilder(field);
        case "date_histogram":
        case "dhg":
            return dateHistogram(field);
        case "date_range":
            return dateRange(field);
        case "month":
            return dateRange(field);
        case "histogram":
        case "hg":
            return histogram(field);
        case "geohash_grid":
            return geohashGrid(field);
        case "geo_bounds":
            return geoBounds(field);
        case "terms":
            return termsAgg(field);
        case "significant_text":
            return significantTextAgg(field);
        default:
            throw new SqlParseException("can define this method " + field);
    }

}