use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.
the class IssueIndex method newSecurityReportSubAggregations.
private static AggregationBuilder newSecurityReportSubAggregations(AggregationBuilder categoriesAggs, boolean includeCwe, @Nullable Collection<String> cwesInCategory) {
AggregationBuilder aggregationBuilder = addSecurityReportIssueCountAggregations(categoriesAggs);
if (includeCwe) {
final TermsAggregationBuilder cwesAgg = AggregationBuilders.terms(AGG_CWES).field(FIELD_ISSUE_CWE).size(MAX_FACET_SIZE);
if (cwesInCategory != null) {
cwesAgg.includeExclude(new IncludeExclude(cwesInCategory.toArray(new String[0]), new String[0]));
}
categoriesAggs.subAggregation(addSecurityReportIssueCountAggregations(cwesAgg));
}
return aggregationBuilder;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project metron by apache.
the class ElasticsearchSearchDao method buildGroupRequest.
/**
* Builds a group search request.
* @param groupRequest The Metron group request.
* @param queryBuilder The search query.
* @return An Elasticsearch search request.
*/
private org.elasticsearch.action.search.SearchRequest buildGroupRequest(GroupRequest groupRequest, QueryBuilder queryBuilder) {
// handle groups
TermsAggregationBuilder groups = getGroupsTermBuilder(groupRequest, 0);
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(queryBuilder).aggregation(groups);
// return the search request
String[] indices = wildcardIndices(groupRequest.getIndices());
return new org.elasticsearch.action.search.SearchRequest().indices(indices).source(searchSourceBuilder);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project metron by apache.
the class ElasticsearchSearchDao method getGroupsTermBuilder.
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;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project metron by apache.
the class ElasticsearchSearchDao method buildSearchRequest.
/**
* Builds an Elasticsearch search request.
* @param searchRequest The Metron search request.
* @param queryBuilder
* @return An Elasticsearch search request.
*/
private org.elasticsearch.action.search.SearchRequest buildSearchRequest(SearchRequest searchRequest, QueryBuilder queryBuilder) throws InvalidSearchException {
if (LOG.isDebugEnabled()) {
LOG.debug("Got search request; request={}", ElasticsearchUtils.toJSON(searchRequest).orElse("???"));
}
SearchSourceBuilder searchBuilder = new SearchSourceBuilder().size(searchRequest.getSize()).from(searchRequest.getFrom()).query(queryBuilder).trackScores(true);
List<String> fields = searchRequest.getFields();
// column metadata needed to understand the type of each sort field
Map<String, FieldType> meta;
try {
meta = columnMetadataDao.getColumnMetadata(searchRequest.getIndices());
} catch (IOException e) {
throw new InvalidSearchException("Unable to get column metadata", e);
}
// handle sort fields
for (SortField sortField : searchRequest.getSort()) {
// what type is the sort field?
FieldType sortFieldType = meta.getOrDefault(sortField.getField(), FieldType.OTHER);
// sort order - if ascending missing values sorted last. otherwise, missing values sorted first
org.elasticsearch.search.sort.SortOrder sortOrder = getElasticsearchSortOrder(sortField.getSortOrder());
String missingSortOrder;
if (sortOrder == org.elasticsearch.search.sort.SortOrder.DESC) {
missingSortOrder = SORT_MISSING_LAST;
} else {
missingSortOrder = SORT_MISSING_FIRST;
}
// sort by the field - missing fields always last
FieldSortBuilder sortBy = new FieldSortBuilder(sortField.getField()).order(sortOrder).missing(missingSortOrder).unmappedType(sortFieldType.getFieldType());
searchBuilder.sort(sortBy);
}
// handle search fields
if (fields != null) {
searchBuilder.fetchSource("*", null);
} else {
searchBuilder.fetchSource(true);
}
List<String> facetFields = searchRequest.getFacetFields();
// handle facet fields
if (facetFields != null) {
// https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_bucket_aggregations.html
for (String field : facetFields) {
String name = getFacetAggregationName(field);
TermsAggregationBuilder terms = AggregationBuilders.terms(name).field(field);
// new TermsBuilder(name).field(field);
searchBuilder.aggregation(terms);
}
}
// return the search request
String[] indices = wildcardIndices(searchRequest.getIndices());
if (LOG.isDebugEnabled()) {
LOG.debug("Built Elasticsearch request; indices={}, request={}", indices, searchBuilder.toString());
}
return new org.elasticsearch.action.search.SearchRequest().indices(indices).source(searchBuilder);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project snow-owl by b2ihealthcare.
the class EsDocumentSearcher method toEsAggregation.
private org.elasticsearch.search.aggregations.AggregationBuilder toEsAggregation(DocumentMapping mapping, AggregationBuilder<?> aggregation, boolean fetchSource) {
final TermsAggregationBuilder termsAgg = AggregationBuilders.terms(aggregation.getName()).minDocCount(aggregation.getMinBucketSize()).size(Integer.MAX_VALUE);
boolean isFieldAgg = !Strings.isNullOrEmpty(aggregation.getGroupByField());
boolean isScriptAgg = !Strings.isNullOrEmpty(aggregation.getGroupByScript());
if (isFieldAgg) {
checkArgument(!isScriptAgg, "Specify either field or script parameter, not both");
termsAgg.field(aggregation.getGroupByField());
} else if (isScriptAgg) {
termsAgg.script(aggregation.toEsScript(mapping));
} else {
throw new IllegalArgumentException("Specify either field or script parameter");
}
boolean isNested = !Strings.isNullOrEmpty(aggregation.getPath());
// add top hits agg to get the top N items for each bucket
if (aggregation.getBucketHitsLimit() > 0) {
TopHitsAggregationBuilder topHitsAgg = AggregationBuilders.topHits(topHitsAggName(aggregation)).size(aggregation.getBucketHitsLimit());
if (fetchSource) {
topHitsAgg.storedFields(STORED_FIELDS_ID_ONLY).fetchSource(true);
} else {
topHitsAgg.storedFields(STORED_FIELDS_NONE).fetchSource(false);
aggregation.getFields().forEach(field -> topHitsAgg.docValueField(field));
}
if (isNested) {
termsAgg.subAggregation(AggregationBuilders.reverseNested(reverseNestedAggName(aggregation)).subAggregation(topHitsAgg));
} else {
termsAgg.subAggregation(topHitsAgg);
}
}
if (isNested) {
return AggregationBuilders.nested(nestedAggName(aggregation), aggregation.getPath()).subAggregation(termsAgg);
}
return termsAgg;
}
Aggregations