Search in sources :

Example 71 with BoolQueryBuilder

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;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ComponentTextSearchFeature(org.sonar.server.es.textsearch.ComponentTextSearchFeature) QueryBuilders.nestedQuery(org.elasticsearch.index.query.QueryBuilders.nestedQuery) StickyFacetBuilder(org.sonar.server.es.StickyFacetBuilder) AuthorizationTypeSupport(org.sonar.server.permission.index.AuthorizationTypeSupport) EsClient(org.sonar.server.es.EsClient) DESC(org.elasticsearch.search.sort.SortOrder.DESC) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) Map(java.util.Map) FIELD_LANGUAGES(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_LANGUAGES) FILTER_LANGUAGES(org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_LANGUAGES) RELIABILITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY) FIELD_QUALITY_GATE_STATUS(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_QUALITY_GATE_STATUS) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) ImmutableMap(com.google.common.collect.ImmutableMap) SearchIdResult(org.sonar.server.es.SearchIdResult) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Collections.emptyList(java.util.Collections.emptyList) SORT_BY_NAME(org.sonar.server.measure.index.ProjectMeasuresQuery.SORT_BY_NAME) Set(java.util.Set) Collectors(org.sonar.core.util.stream.Collectors) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) List(java.util.List) SearchOptions(org.sonar.server.es.SearchOptions) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) SQALE_RATING_KEY(org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY) Optional(java.util.Optional) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IntStream(java.util.stream.IntStream) INDEX_TYPE_PROJECT_MEASURES(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.INDEX_TYPE_PROJECT_MEASURES) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) DefaultIndexSettingsElement(org.sonar.server.es.DefaultIndexSettingsElement) EsUtils.escapeSpecialRegexChars(org.sonar.server.es.EsUtils.escapeSpecialRegexChars) QueryBuilders.rangeQuery(org.elasticsearch.index.query.QueryBuilders.rangeQuery) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) ComponentTextSearchQueryFactory(org.sonar.server.es.textsearch.ComponentTextSearchQueryFactory) RangeBuilder(org.elasticsearch.search.aggregations.bucket.range.RangeBuilder) AggregationBuilders.filters(org.elasticsearch.search.aggregations.AggregationBuilders.filters) ImmutableList(com.google.common.collect.ImmutableList) FIELD_ORGANIZATION_UUID(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_ORGANIZATION_UUID) Nullable(javax.annotation.Nullable) QueryBuilders.termsQuery(org.elasticsearch.index.query.QueryBuilders.termsQuery) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) SECURITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY) QueryBuilders.matchAllQuery(org.elasticsearch.index.query.QueryBuilders.matchAllQuery) DUPLICATED_LINES_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY) FIELD_KEY(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_KEY) FIELD_NAME(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NAME) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) MetricCriterion(org.sonar.server.measure.index.ProjectMeasuresQuery.MetricCriterion) QUALITY_GATE_STATUS(org.sonar.server.measure.index.ProjectMeasuresDoc.QUALITY_GATE_STATUS) FIELD_MEASURES(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES) ASC(org.elasticsearch.search.sort.SortOrder.ASC) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) COVERAGE_KEY(org.sonar.api.measures.CoreMetrics.COVERAGE_KEY) BaseIndex(org.sonar.server.es.BaseIndex) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) NCLOC_KEY(org.sonar.api.measures.CoreMetrics.NCLOC_KEY) FILTER_TAGS(org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_TAGS) FIELD_TAGS(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_TAGS) MetricCriterion(org.sonar.server.measure.index.ProjectMeasuresQuery.MetricCriterion) HashMap(java.util.HashMap) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 72 with BoolQueryBuilder

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));
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 73 with BoolQueryBuilder

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);
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) RegexPredicate(com.google.gerrit.server.index.RegexPredicate)

Example 74 with BoolQueryBuilder

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;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 75 with BoolQueryBuilder

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;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)98 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)15 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)9 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)9 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)8 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)5 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)5 Map (java.util.Map)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)4 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)4 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 StickyFacetBuilder (org.sonar.server.es.StickyFacetBuilder)4 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)3 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)3