use of io.kestra.core.repositories.ArrayListTotal in project kestra by kestra-io.
the class AbstractElasticSearchRepository method findDistinctNamespace.
protected List<String> findDistinctNamespace(String index) {
BoolQueryBuilder query = this.defaultFilter();
// We want to keep only "distinct" values of field "namespace"
// @TODO: use includeExclude(new IncludeExclude(0, 10)) to partition results
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("distinct_namespace").field("namespace").size(10000).order(BucketOrder.key(true));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query).aggregation(termsAggregationBuilder);
SearchRequest searchRequest = searchRequest(index, sourceBuilder, false);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Terms namespaces = searchResponse.getAggregations().get("distinct_namespace");
return new ArrayListTotal<>(namespaces.getBuckets().stream().map(o -> {
return o.getKey().toString();
}).collect(Collectors.toList()), namespaces.getBuckets().size());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of io.kestra.core.repositories.ArrayListTotal 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);
}
}
use of io.kestra.core.repositories.ArrayListTotal in project kestra by kestra-io.
the class ElasticSearchFlowRepository method findSourceCode.
@Override
public ArrayListTotal<SearchResult<Flow>> findSourceCode(String query, Pageable pageable) {
BoolQueryBuilder bool = this.defaultFilter().must(QueryBuilders.queryStringQuery(query).field("sourceCode"));
SearchSourceBuilder sourceBuilder = this.searchSource(bool, Optional.empty(), pageable);
sourceBuilder.fetchSource("*", "sourceCode");
sourceBuilder.highlighter(new HighlightBuilder().preTags("[mark]").postTags("[/mark]").field("sourceCode"));
SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return new ArrayListTotal<>(Arrays.stream(searchResponse.getHits().getHits()).map(documentFields -> {
try {
return new SearchResult<>(MAPPER.readValue(documentFields.getSourceAsString(), this.cls), documentFields.getHighlightFields().get("sourceCode") != null ? Arrays.stream(documentFields.getHighlightFields().get("sourceCode").getFragments()).map(Text::string).collect(Collectors.toList()) : Collections.emptyList());
} catch (IOException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList()), searchResponse.getHits().getTotalHits().value);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations