org.elasticsearch.search.aggregations.bucket.range.DateRangeAggregationBuilder Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.range.DateRangeAggregationBuilder. 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: DateRangeAggregationMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        DateRangeAggregationBuilder dateRangeAggregationBuilder = AggregationBuilders.dateRange("date_range");
        dateRangeAggregationBuilder.field("ctm");
        dateRangeAggregationBuilder.format("yyyy-MM-dd HH:mm:ss");
        dateRangeAggregationBuilder.addRange("2018-07-10 00:00:00","2018-07-11 00:00:00");
        dateRangeAggregationBuilder.timeZone(DateTimeZone.forOffsetHours(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(dateRangeAggregationBuilder);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);

    }finally{
        HighLevelClient.close();
    }
}
 
Example #2
Source File: ElasticQueryBuilder.java    From vind with Apache License 2.0 6 votes vote down vote up
private static void intervalToRange(Interval.UtilDateInterval<?> interval, DateRangeAggregationBuilder rangeAggregation) {

        final Date start = interval.getStart();
        final Date end =  interval.getEnd();

        if (Objects.nonNull(start) && Objects.nonNull(end)) {
            rangeAggregation.addRange(
                    interval.getName(),
                    ZonedDateTime.ofInstant(start.toInstant(), ZoneId.of("UTC")),
                    ZonedDateTime.ofInstant(end.toInstant(), ZoneId.of("UTC")));
        } else {
            Optional.ofNullable(start).ifPresent(n -> rangeAggregation.addUnboundedFrom(
                    interval.getName(),
                    ZonedDateTime.ofInstant(n.toInstant(), ZoneId.of("UTC"))));
            Optional.ofNullable(end).ifPresent(n -> rangeAggregation.addUnboundedTo(
                    interval.getName(),
                    ZonedDateTime.ofInstant(n.toInstant(), ZoneId.of("UTC"))));
        }
    }
 
Example #3
Source File: ParseUtils.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static SearchSourceBuilder generatePreviewQuery(
    AnomalyDetector detector,
    List<Entry<Long, Long>> ranges,
    NamedXContentRegistry xContentRegistry
) throws IOException {

    DateRangeAggregationBuilder dateRangeBuilder = dateRange("date_range").field(detector.getTimeField()).format("epoch_millis");
    for (Entry<Long, Long> range : ranges) {
        dateRangeBuilder.addRange(range.getKey(), range.getValue());
    }

    if (detector.getFeatureAttributes() != null) {
        for (Feature feature : detector.getFeatureAttributes()) {
            AggregatorFactories.Builder internalAgg = parseAggregators(
                feature.getAggregation().toString(),
                xContentRegistry,
                feature.getId()
            );
            dateRangeBuilder.subAggregation(internalAgg.getAggregatorFactories().iterator().next());
        }
    }

    return new SearchSourceBuilder().query(detector.getFilterQuery()).size(0).aggregation(dateRangeBuilder);
}
 
Example #4
Source File: BsClickLogCA.java    From fess with Apache License 2.0 5 votes vote down vote up
public void setQueryRequestedAt_DateRange(String name, ConditionOptionCall<DateRangeAggregationBuilder> opLambda,
        OperatorCall<BsClickLogCA> aggsLambda) {
    DateRangeAggregationBuilder builder = regDateRangeA(name, "queryRequestedAt");
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        ClickLogCA ca = new ClickLogCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
 
Example #5
Source File: BsSearchLogCA.java    From fess with Apache License 2.0 5 votes vote down vote up
public void setRequestedAt_DateRange(String name, ConditionOptionCall<DateRangeAggregationBuilder> opLambda,
        OperatorCall<BsSearchLogCA> aggsLambda) {
    DateRangeAggregationBuilder builder = regDateRangeA(name, "requestedAt");
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        SearchLogCA ca = new SearchLogCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
 
Example #6
Source File: BsFavoriteLogCA.java    From fess with Apache License 2.0 5 votes vote down vote up
public void setCreatedAt_DateRange(String name, ConditionOptionCall<DateRangeAggregationBuilder> opLambda,
        OperatorCall<BsFavoriteLogCA> aggsLambda) {
    DateRangeAggregationBuilder builder = regDateRangeA(name, "createdAt");
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        FavoriteLogCA ca = new FavoriteLogCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
 
Example #7
Source File: BsClickLogCA.java    From fess with Apache License 2.0 5 votes vote down vote up
public void setRequestedAt_DateRange(String name, ConditionOptionCall<DateRangeAggregationBuilder> opLambda,
        OperatorCall<BsClickLogCA> aggsLambda) {
    DateRangeAggregationBuilder builder = regDateRangeA(name, "requestedAt");
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        ClickLogCA ca = new ClickLogCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
 
Example #8
Source File: BsUserInfoCA.java    From fess with Apache License 2.0 5 votes vote down vote up
public void setUpdatedAt_DateRange(String name, ConditionOptionCall<DateRangeAggregationBuilder> opLambda,
        OperatorCall<BsUserInfoCA> aggsLambda) {
    DateRangeAggregationBuilder builder = regDateRangeA(name, "updatedAt");
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        UserInfoCA ca = new UserInfoCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
 
Example #9
Source File: BsUserInfoCA.java    From fess with Apache License 2.0 5 votes vote down vote up
public void setCreatedAt_DateRange(String name, ConditionOptionCall<DateRangeAggregationBuilder> opLambda,
        OperatorCall<BsUserInfoCA> aggsLambda) {
    DateRangeAggregationBuilder builder = regDateRangeA(name, "createdAt");
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        UserInfoCA ca = new UserInfoCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
 
Example #10
Source File: ElasticQueryBuilder.java    From vind with Apache License 2.0 5 votes vote down vote up
private static void intervalToRange(Interval.ZonedDateTimeInterval<?> interval, DateRangeAggregationBuilder rangeAggregation) {

        final ZonedDateTime start = interval.getStart();
        final ZonedDateTime end =  interval.getEnd();

        if (Objects.nonNull(start) && Objects.nonNull(end)) {
            rangeAggregation.addRange(interval.getName(), start, end);
        } else {
            Optional.ofNullable(start).ifPresent(n -> rangeAggregation.addUnboundedFrom(interval.getName(), n));
            Optional.ofNullable(end).ifPresent(n -> rangeAggregation.addUnboundedTo(interval.getName(), n));
        }
    }
 
Example #11
Source File: ElasticQueryBuilder.java    From vind with Apache License 2.0 5 votes vote down vote up
private static void intervalToRange(Interval.DateMathInterval<?> interval, DateRangeAggregationBuilder rangeAggregation) {

        final DateMathExpression start = interval.getStart();
        final DateMathExpression end =  interval.getEnd();

        if (Objects.nonNull(start) && Objects.nonNull(end)) {
            rangeAggregation.addRange(
                    interval.getName(),
                    start.toElasticString(),
                    end.toElasticString()
//                    ZonedDateTime.ofInstant(Instant.ofEpochSecond(start.getTimeStamp()), ZoneId.of("UTC")),
//                    ZonedDateTime.ofInstant(Instant.ofEpochSecond(end.getTimeStamp()), ZoneId.of("UTC"))
                    );
        } else {
            Optional.ofNullable(start).ifPresent(n ->
                    rangeAggregation.addUnboundedFrom(
                            interval.getName(),
                            n.toElasticString()
                            //ZonedDateTime.ofInstant(Instant.ofEpochSecond(n.getTimeStamp()), ZoneId.of("UTC"))
                    )
            );
            Optional.ofNullable(end).ifPresent(n ->
                    rangeAggregation.addUnboundedTo(
                            interval.getName(),
                            n.toElasticString()
                            //ZonedDateTime.ofInstant(Instant.ofEpochSecond(n.getTimeStamp()), ZoneId.of("UTC"))
                    )
            );
        }

    }
 
Example #12
Source File: BsFavoriteLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setCreatedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda) {
    setCreatedAt_DateRange("createdAt", opLambda, null);
}
 
Example #13
Source File: EsAbstractConditionAggregation.java    From fess with Apache License 2.0 4 votes vote down vote up
protected DateRangeAggregationBuilder regDateRangeA(String name, String field) {
    DateRangeAggregationBuilder builder = AggregationBuilders.dateRange(name).field(field);
    regA(builder);
    return builder;
}
 
Example #14
Source File: EsAbstractConditionAggregation.java    From fess with Apache License 2.0 4 votes vote down vote up
protected DateRangeAggregationBuilder regDateRangeA(String name, String field) {
    DateRangeAggregationBuilder builder = AggregationBuilders.dateRange(name).field(field);
    regA(builder);
    return builder;
}
 
Example #15
Source File: EsAbstractConditionAggregation.java    From fess with Apache License 2.0 4 votes vote down vote up
protected DateRangeAggregationBuilder regDateRangeA(String name, String field) {
    DateRangeAggregationBuilder builder = AggregationBuilders.dateRange(name).field(field);
    regA(builder);
    return builder;
}
 
Example #16
Source File: BsSearchLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setRequestedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda, OperatorCall<BsSearchLogCA> aggsLambda) {
    setRequestedAt_DateRange("requestedAt", opLambda, aggsLambda);
}
 
Example #17
Source File: BsSearchLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setRequestedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda) {
    setRequestedAt_DateRange("requestedAt", opLambda, null);
}
 
Example #18
Source File: BsFavoriteLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setCreatedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda, OperatorCall<BsFavoriteLogCA> aggsLambda) {
    setCreatedAt_DateRange("createdAt", opLambda, aggsLambda);
}
 
Example #19
Source File: BsUserInfoCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setUpdatedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda) {
    setUpdatedAt_DateRange("updatedAt", opLambda, null);
}
 
Example #20
Source File: BsUserInfoCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setCreatedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda) {
    setCreatedAt_DateRange("createdAt", opLambda, null);
}
 
Example #21
Source File: BsClickLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setRequestedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda, OperatorCall<BsClickLogCA> aggsLambda) {
    setRequestedAt_DateRange("requestedAt", opLambda, aggsLambda);
}
 
Example #22
Source File: BsClickLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setRequestedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda) {
    setRequestedAt_DateRange("requestedAt", opLambda, null);
}
 
Example #23
Source File: BsUserInfoCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setCreatedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda, OperatorCall<BsUserInfoCA> aggsLambda) {
    setCreatedAt_DateRange("createdAt", opLambda, aggsLambda);
}
 
Example #24
Source File: BsClickLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setQueryRequestedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda,
        OperatorCall<BsClickLogCA> aggsLambda) {
    setQueryRequestedAt_DateRange("queryRequestedAt", opLambda, aggsLambda);
}
 
Example #25
Source File: BsClickLogCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setQueryRequestedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda) {
    setQueryRequestedAt_DateRange("queryRequestedAt", opLambda, null);
}
 
Example #26
Source File: BsUserInfoCA.java    From fess with Apache License 2.0 4 votes vote down vote up
public void setUpdatedAt_DateRange(ConditionOptionCall<DateRangeAggregationBuilder> opLambda, OperatorCall<BsUserInfoCA> aggsLambda) {
    setUpdatedAt_DateRange("updatedAt", opLambda, aggsLambda);
}