use of org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method createFilters.
private Map<String, QueryBuilder> createFilters(ProjectMeasuresQuery query) {
Map<String, QueryBuilder> filters = new HashMap<>();
filters.put("__authorization", authorizationTypeSupport.createQueryFilter());
Multimap<String, MetricCriterion> metricCriterionMultimap = ArrayListMultimap.create();
query.getMetricCriteria().forEach(metricCriterion -> metricCriterionMultimap.put(metricCriterion.getMetricKey(), metricCriterion));
metricCriterionMultimap.asMap().entrySet().forEach(entry -> {
BoolQueryBuilder metricFilters = boolQuery();
entry.getValue().stream().map(criterion -> nestedQuery(FIELD_MEASURES, boolQuery().filter(termQuery(FIELD_MEASURES_KEY, criterion.getMetricKey())).filter(toValueQuery(criterion)))).forEach(metricFilters::must);
filters.put(entry.getKey(), metricFilters);
});
query.getQualityGateStatus().ifPresent(qualityGateStatus -> filters.put(ALERT_STATUS_KEY, termQuery(FIELD_QUALITY_GATE_STATUS, QUALITY_GATE_STATUS.get(qualityGateStatus.name()))));
query.getProjectUuids().ifPresent(projectUuids -> filters.put("ids", termsQuery("_id", projectUuids)));
query.getLanguages().ifPresent(languages -> filters.put(FILTER_LANGUAGES, termsQuery(FIELD_LANGUAGES, languages)));
query.getOrganizationUuid().ifPresent(organizationUuid -> filters.put(FIELD_ORGANIZATION_UUID, termQuery(FIELD_ORGANIZATION_UUID, organizationUuid)));
query.getTags().ifPresent(tags -> filters.put(FIELD_TAGS, termsQuery(FIELD_TAGS, tags)));
createTextQueryFilter(query).ifPresent(queryBuilder -> filters.put("textQuery", queryBuilder));
return filters;
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class AuthorizationTypeSupport method createQueryFilter.
/**
* Build a filter to restrict query to the documents on which
* user has read access.
*/
public QueryBuilder createQueryFilter() {
if (userSession.isRoot()) {
return QueryBuilders.matchAllQuery();
}
Integer userId = userSession.getUserId();
BoolQueryBuilder filter = boolQuery();
// anyone
filter.should(QueryBuilders.termQuery(FIELD_ALLOW_ANYONE, true));
// users
Optional.ofNullable(userId).map(Integer::longValue).ifPresent(id -> filter.should(termQuery(FIELD_USER_IDS, id)));
// groups
userSession.getGroups().stream().map(GroupDto::getId).forEach(groupId -> filter.should(termQuery(FIELD_GROUP_IDS, groupId)));
return QueryBuilders.hasParentQuery(TYPE_AUTHORIZATION, QueryBuilders.boolQuery().filter(filter));
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project gerrit by GerritCodeReview.
the class ElasticQueryBuilder method exactQuery.
private <T> QueryBuilder exactQuery(IndexPredicate<T> p) {
String name = p.getField().getName();
String value = p.getValue();
if (value.isEmpty()) {
return new BoolQueryBuilder().mustNot(QueryBuilders.existsQuery(name));
} else if (p instanceof RegexPredicate) {
if (value.startsWith("^")) {
value = value.substring(1);
}
if (value.endsWith("$") && !value.endsWith("\\$") && !value.endsWith("\\\\$")) {
value = value.substring(0, value.length() - 1);
}
return QueryBuilders.regexpQuery(name + ".key", value);
} else {
return QueryBuilders.termQuery(name + ".key", value);
}
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project fess by codelibs.
the class EsAbstractConditionQuery method getQuery.
public QueryBuilder getQuery() {
if (queryBuilderList == null) {
return null;
} else if (queryBuilderList.size() == 1) {
return queryBuilderList.get(0);
}
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
queryBuilderList.forEach(query -> {
boolQuery.must(query);
});
return boolQuery;
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project fess by codelibs.
the class EsAbstractConditionQuery method regBoolCQ.
protected BoolQueryBuilder regBoolCQ(List<QueryBuilder> mustList, List<QueryBuilder> shouldList, List<QueryBuilder> mustNotList, List<QueryBuilder> filterList) {
assertObjectNotNull("mustList", mustList);
assertObjectNotNull("shouldList", shouldList);
assertObjectNotNull("mustNotList", mustNotList);
assertObjectNotNull("filterList", filterList);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
mustList.forEach(query -> {
boolQuery.must(query);
});
shouldList.forEach(query -> {
boolQuery.should(query);
});
mustNotList.forEach(query -> {
boolQuery.mustNot(query);
});
filterList.forEach(query -> {
boolQuery.filter(query);
});
regQ(boolQuery);
return boolQuery;
}
Aggregations