use of org.opensearch.search.aggregations.bucket.nested.ParsedNested in project kestra by kestra-io.
the class ElasticSearchExecutionRepository method dailyStatistics.
@Override
public List<DailyExecutionStatistics> dailyStatistics(@Nullable String query, @Nullable ZonedDateTime startDate, @Nullable ZonedDateTime endDate, boolean isTaskRun) {
if (startDate == null) {
startDate = ZonedDateTime.now().minusDays(30);
}
if (endDate == null) {
endDate = ZonedDateTime.now();
}
AggregationBuilder agg = dailyExecutionStatisticsFinalAgg(startDate, endDate, isTaskRun);
if (isTaskRun) {
agg = AggregationBuilders.nested(NESTED_AGG, "taskRunList").subAggregation(agg);
}
SearchSourceBuilder sourceBuilder = this.searchSource(this.dateFilters(query, startDate, endDate), Optional.of(Collections.singletonList(agg)), null);
try {
SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
ParsedDateHistogram groupAgg = isTaskRun ? ((ParsedNested) searchResponse.getAggregations().get(NESTED_AGG)).getAggregations().get(DATE_AGG) : searchResponse.getAggregations().get(DATE_AGG);
List<DailyExecutionStatistics> result = new ArrayList<>();
groupAgg.getBuckets().forEach(bucket -> {
ParsedStringTerms countAgg = bucket.getAggregations().get(COUNT_AGG);
ParsedStats durationAgg = bucket.getAggregations().get(DURATION_AGG);
final LocalDate currentStartDate = LocalDate.parse(bucket.getKeyAsString(), DateTimeFormatter.ISO_LOCAL_DATE);
result.add(dailyExecutionStatisticsMap(countAgg, durationAgg, currentStartDate));
});
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.opensearch.search.aggregations.bucket.nested.ParsedNested in project kestra by kestra-io.
the class ElasticSearchExecutionRepository method findTaskRun.
@Override
public ArrayListTotal<TaskRun> findTaskRun(String query, Pageable pageable, @Nullable List<State.Type> state) {
BoolQueryBuilder filterAggQuery = QueryBuilders.boolQuery().filter(QueryBuilders.queryStringQuery(query));
if (state != null) {
filterAggQuery = filterAggQuery.must(QueryBuilders.termsQuery("taskRunList.state.current", stateConvert(state)));
}
NestedAggregationBuilder nestedAgg = AggregationBuilders.nested("NESTED", "taskRunList").subAggregation(AggregationBuilders.filter("FILTER", filterAggQuery).subAggregation(AggregationBuilders.topHits("TOPHITS").size(pageable.getSize()).sorts(defaultSorts(pageable, true)).from(Math.toIntExact(pageable.getOffset() - pageable.getSize()))));
BoolQueryBuilder mainQuery = this.defaultFilter().filter(QueryBuilders.nestedQuery("taskRunList", QueryBuilders.queryStringQuery(query).field("*.fulltext"), ScoreMode.Total));
SearchSourceBuilder sourceBuilder = this.searchSource(mainQuery, Optional.of(List.of(nestedAgg)), null).fetchSource(false);
SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
ParsedNested pn = searchResponse.getAggregations().get("NESTED");
Filter fa = pn.getAggregations().get("FILTER");
long docCount = fa.getDocCount();
TopHits th = fa.getAggregations().get("TOPHITS");
List<TaskRun> collect = Arrays.stream(th.getHits().getHits()).map(documentFields -> {
try {
return MAPPER.readValue(documentFields.getSourceAsString(), TaskRun.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
return new ArrayListTotal<>(collect, docCount);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations