use of org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method search.
public SearchIdResult<String> search(ProjectMeasuresQuery query, SearchOptions searchOptions) {
SearchRequestBuilder requestBuilder = getClient().prepareSearch(INDEX_TYPE_PROJECT_MEASURES).setFetchSource(false).setFrom(searchOptions.getOffset()).setSize(searchOptions.getLimit());
BoolQueryBuilder esFilter = boolQuery();
Map<String, QueryBuilder> filters = createFilters(query);
filters.values().forEach(esFilter::must);
requestBuilder.setQuery(esFilter);
addFacets(requestBuilder, searchOptions, filters, query);
addSort(query, requestBuilder);
return new SearchIdResult<>(requestBuilder.get(), id -> id);
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class RuleIndex method addActiveSeverityFacetIfNeeded.
private static void addActiveSeverityFacetIfNeeded(RuleQuery query, SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
if (options.getFacets().contains(FACET_ACTIVE_SEVERITIES)) {
// We are building a children aggregation on active rules
// so the rule filter has to be used as parent filter for active rules
// from which we remove filters that concern active rules ("activation")
HasParentQueryBuilder ruleFilter = QueryBuilders.hasParentQuery(INDEX_TYPE_RULE.getType(), stickyFacetBuilder.getStickyFacetFilter("activation"));
// Rebuilding the active rule filter without severities
BoolQueryBuilder childrenFilter = boolQuery();
addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
RuleIndex.addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
QueryBuilder activeRuleFilter;
if (childrenFilter.hasClauses()) {
activeRuleFilter = childrenFilter.must(ruleFilter);
} else {
activeRuleFilter = ruleFilter;
}
AbstractAggregationBuilder activeSeverities = AggregationBuilders.children(FACET_ACTIVE_SEVERITIES + "_children").childType(INDEX_TYPE_ACTIVE_RULE.getType()).subAggregation(AggregationBuilders.filter(FACET_ACTIVE_SEVERITIES + "_filter").filter(activeRuleFilter).subAggregation(AggregationBuilders.terms(FACET_ACTIVE_SEVERITIES).field(FIELD_ACTIVE_RULE_SEVERITY).include(Joiner.on('|').join(Severity.ALL)).size(Severity.ALL.size())));
aggregations.put(FACET_ACTIVE_SEVERITIES, AggregationBuilders.global(FACET_ACTIVE_SEVERITIES).subAggregation(activeSeverities));
}
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class RuleIndex method buildFilters.
/* Build main filter (match based) */
private static Map<String, QueryBuilder> buildFilters(RuleQuery query) {
Map<String, QueryBuilder> filters = new HashMap<>();
/* Add enforced filter on rules that are REMOVED */
filters.put(FIELD_RULE_STATUS, boolQuery().mustNot(QueryBuilders.termQuery(FIELD_RULE_STATUS, RuleStatus.REMOVED.toString())));
if (StringUtils.isNotEmpty(query.getInternalKey())) {
filters.put(FIELD_RULE_INTERNAL_KEY, QueryBuilders.termQuery(FIELD_RULE_INTERNAL_KEY, query.getInternalKey()));
}
if (StringUtils.isNotEmpty(query.getRuleKey())) {
filters.put(FIELD_RULE_RULE_KEY, QueryBuilders.termQuery(FIELD_RULE_RULE_KEY, query.getRuleKey()));
}
if (isNotEmpty(query.getLanguages())) {
filters.put(FIELD_RULE_LANGUAGE, QueryBuilders.termsQuery(FIELD_RULE_LANGUAGE, query.getLanguages()));
}
if (isNotEmpty(query.getRepositories())) {
filters.put(FIELD_RULE_REPOSITORY, QueryBuilders.termsQuery(FIELD_RULE_REPOSITORY, query.getRepositories()));
}
if (isNotEmpty(query.getSeverities())) {
filters.put(FIELD_RULE_SEVERITY, QueryBuilders.termsQuery(FIELD_RULE_SEVERITY, query.getSeverities()));
}
if (StringUtils.isNotEmpty(query.getKey())) {
filters.put(FIELD_RULE_KEY, QueryBuilders.termQuery(FIELD_RULE_KEY, query.getKey()));
}
if (isNotEmpty(query.getTags())) {
filters.put(FIELD_RULE_ALL_TAGS, QueryBuilders.termsQuery(FIELD_RULE_ALL_TAGS, query.getTags()));
}
if (isNotEmpty(query.getTypes())) {
filters.put(FIELD_RULE_TYPE, QueryBuilders.termsQuery(FIELD_RULE_TYPE, query.getTypes()));
}
if (query.getAvailableSinceLong() != null) {
filters.put("availableSince", QueryBuilders.rangeQuery(FIELD_RULE_CREATED_AT).gte(query.getAvailableSinceLong()));
}
if (isNotEmpty(query.getStatuses())) {
Collection<String> stringStatus = new ArrayList<>();
for (RuleStatus status : query.getStatuses()) {
stringStatus.add(status.name());
}
filters.put(FIELD_RULE_STATUS, QueryBuilders.termsQuery(FIELD_RULE_STATUS, stringStatus));
}
Boolean isTemplate = query.isTemplate();
if (isTemplate != null) {
filters.put(FIELD_RULE_IS_TEMPLATE, QueryBuilders.termQuery(FIELD_RULE_IS_TEMPLATE, Boolean.toString(isTemplate)));
}
String template = query.templateKey();
if (template != null) {
filters.put(FIELD_RULE_TEMPLATE_KEY, QueryBuilders.termQuery(FIELD_RULE_TEMPLATE_KEY, template));
}
// ActiveRule Filter (profile and inheritance)
BoolQueryBuilder childrenFilter = boolQuery();
addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_SEVERITY, query.getActiveSeverities());
// ChildQuery
QueryBuilder childQuery;
if (childrenFilter.hasClauses()) {
childQuery = childrenFilter;
} else {
childQuery = matchAllQuery();
}
/** Implementation of activation query */
if (Boolean.TRUE.equals(query.getActivation())) {
filters.put("activation", QueryBuilders.hasChildQuery(INDEX_TYPE_ACTIVE_RULE.getType(), childQuery));
} else if (Boolean.FALSE.equals(query.getActivation())) {
filters.put("activation", boolQuery().mustNot(QueryBuilders.hasChildQuery(INDEX_TYPE_ACTIVE_RULE.getType(), childQuery)));
}
return filters;
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class RuleIndex method search.
public SearchIdResult<RuleKey> search(RuleQuery query, SearchOptions options) {
SearchRequestBuilder esSearch = getClient().prepareSearch(INDEX_TYPE_RULE);
QueryBuilder qb = buildQuery(query);
Map<String, QueryBuilder> filters = buildFilters(query);
if (!options.getFacets().isEmpty()) {
for (AbstractAggregationBuilder aggregation : getFacets(query, options, qb, filters).values()) {
esSearch.addAggregation(aggregation);
}
}
setSorting(query, esSearch);
setPagination(options, esSearch);
BoolQueryBuilder fb = boolQuery();
for (QueryBuilder filterBuilder : filters.values()) {
fb.must(filterBuilder);
}
esSearch.setQuery(boolQuery().must(qb).filter(fb));
return new SearchIdResult<>(esSearch.get(), ToRuleKey.INSTANCE);
}
use of org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class UserIndex method search.
public SearchResult<UserDoc> search(@Nullable String searchText, SearchOptions options) {
SearchRequestBuilder request = esClient.prepareSearch(UserIndexDefinition.INDEX_TYPE_USER).setSize(options.getLimit()).setFrom(options.getOffset()).addSort(FIELD_NAME, SortOrder.ASC);
BoolQueryBuilder filter = boolQuery().must(termQuery(FIELD_ACTIVE, true));
QueryBuilder query;
if (StringUtils.isEmpty(searchText)) {
query = matchAllQuery();
} else {
query = QueryBuilders.multiMatchQuery(searchText, FIELD_LOGIN, USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_LOGIN), FIELD_NAME, USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_NAME), FIELD_EMAIL, USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_EMAIL)).operator(MatchQueryBuilder.Operator.AND);
}
request.setQuery(boolQuery().must(query).filter(filter));
return new SearchResult<>(request.get(), DOC_CONVERTER);
}
Aggregations