Search in sources :

Example 6 with SimpleFieldFilterScope

use of org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope in project sonarqube by SonarSource.

the class IssueIndex method addNewCodeReferenceFilterByProjectsFilter.

private static void addNewCodeReferenceFilterByProjectsFilter(AllFilters allFilters, IssueQuery query) {
    Collection<String> newCodeOnReferenceByProjectUuids = query.newCodeOnReferenceByProjectUuids();
    BoolQueryBuilder boolQueryBuilder = boolQuery();
    newCodeOnReferenceByProjectUuids.forEach(projectOrProjectBranchUuid -> boolQueryBuilder.should(boolQuery().filter(termQuery(FIELD_ISSUE_BRANCH_UUID, projectOrProjectBranchUuid)).filter(termQuery(FIELD_ISSUE_NEW_CODE_REFERENCE, true))));
    allFilters.addFilter("__is_new_code_reference_by_project_uuids", new SimpleFieldFilterScope("newCodeReferenceByProjectUuids"), boolQueryBuilder);
}
Also used : SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 7 with SimpleFieldFilterScope

use of org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope in project sonarqube by SonarSource.

the class IssueIndex method createAllFilters.

private AllFilters createAllFilters(IssueQuery query) {
    AllFilters filters = RequestFiltersComputer.newAllFilters();
    filters.addFilter("__indexType", new SimpleFieldFilterScope(FIELD_INDEX_TYPE), termQuery(FIELD_INDEX_TYPE, TYPE_ISSUE.getName()));
    filters.addFilter("__authorization", new SimpleFieldFilterScope("parent"), createAuthorizationFilter());
    // Issue is assigned Filter
    if (Boolean.TRUE.equals(query.assigned())) {
        filters.addFilter(IS_ASSIGNED_FILTER, Facet.ASSIGNEES.getFilterScope(), existsQuery(FIELD_ISSUE_ASSIGNEE_UUID));
    } else if (Boolean.FALSE.equals(query.assigned())) {
        filters.addFilter(IS_ASSIGNED_FILTER, ASSIGNEES.getFilterScope(), boolQuery().mustNot(existsQuery(FIELD_ISSUE_ASSIGNEE_UUID)));
    }
    // Issue is Resolved Filter
    if (Boolean.TRUE.equals(query.resolved())) {
        filters.addFilter("__isResolved", Facet.RESOLUTIONS.getFilterScope(), existsQuery(FIELD_ISSUE_RESOLUTION));
    } else if (Boolean.FALSE.equals(query.resolved())) {
        filters.addFilter("__isResolved", Facet.RESOLUTIONS.getFilterScope(), boolQuery().mustNot(existsQuery(FIELD_ISSUE_RESOLUTION)));
    }
    // Field Filters
    filters.addFilter(FIELD_ISSUE_KEY, new SimpleFieldFilterScope(FIELD_ISSUE_KEY), createTermsFilter(FIELD_ISSUE_KEY, query.issueKeys()));
    filters.addFilter(FIELD_ISSUE_ASSIGNEE_UUID, ASSIGNEES.getFilterScope(), createTermsFilter(FIELD_ISSUE_ASSIGNEE_UUID, query.assignees()));
    filters.addFilter(FIELD_ISSUE_SCOPE, SCOPES.getFilterScope(), createTermsFilter(FIELD_ISSUE_SCOPE, query.scopes()));
    filters.addFilter(FIELD_ISSUE_LANGUAGE, LANGUAGES.getFilterScope(), createTermsFilter(FIELD_ISSUE_LANGUAGE, query.languages()));
    filters.addFilter(FIELD_ISSUE_TAGS, TAGS.getFilterScope(), createTermsFilter(FIELD_ISSUE_TAGS, query.tags()));
    filters.addFilter(FIELD_ISSUE_TYPE, TYPES.getFilterScope(), createTermsFilter(FIELD_ISSUE_TYPE, query.types()));
    filters.addFilter(FIELD_ISSUE_RESOLUTION, RESOLUTIONS.getFilterScope(), createTermsFilter(FIELD_ISSUE_RESOLUTION, query.resolutions()));
    filters.addFilter(FIELD_ISSUE_AUTHOR_LOGIN, AUTHOR.getFilterScope(), createTermsFilter(FIELD_ISSUE_AUTHOR_LOGIN, query.authors()));
    filters.addFilter(FIELD_ISSUE_RULE_UUID, RULES.getFilterScope(), createTermsFilter(FIELD_ISSUE_RULE_UUID, query.ruleUuids()));
    filters.addFilter(FIELD_ISSUE_STATUS, STATUSES.getFilterScope(), createTermsFilter(FIELD_ISSUE_STATUS, query.statuses()));
    // security category
    addSecurityCategoryFilter(FIELD_ISSUE_OWASP_TOP_10, OWASP_TOP_10, query.owaspTop10(), filters);
    addSecurityCategoryFilter(FIELD_ISSUE_SANS_TOP_25, SANS_TOP_25, query.sansTop25(), filters);
    addSecurityCategoryFilter(FIELD_ISSUE_CWE, CWE, query.cwe(), filters);
    addSecurityCategoryFilter(FIELD_ISSUE_SQ_SECURITY_CATEGORY, SONARSOURCE_SECURITY, query.sonarsourceSecurity(), filters);
    addSeverityFilter(query, filters);
    addComponentRelatedFilters(query, filters);
    addDatesFilter(filters, query);
    addCreatedAfterByProjectsFilter(filters, query);
    addNewCodeReferenceFilter(filters, query);
    addNewCodeReferenceFilterByProjectsFilter(filters, query);
    return filters;
}
Also used : SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope) AllFilters(org.sonar.server.es.searchrequest.RequestFiltersComputer.AllFilters)

Example 8 with SimpleFieldFilterScope

use of org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope in project sonarqube by SonarSource.

the class IssueIndex method addViewRelatedFilters.

private static void addViewRelatedFilters(IssueQuery query, AllFilters allFilters) {
    if (Boolean.TRUE.equals(query.onComponentOnly())) {
        return;
    }
    Collection<String> viewUuids = query.viewUuids();
    String branchUuid = query.branchUuid();
    boolean onApplicationBranch = branchUuid != null && !viewUuids.isEmpty();
    if (onApplicationBranch) {
        allFilters.addFilter("__view", new SimpleFieldFilterScope("view"), createViewFilter(singletonList(query.branchUuid())));
    } else {
        allFilters.addFilter("__view", new SimpleFieldFilterScope("view"), createViewFilter(viewUuids));
    }
}
Also used : SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope)

Example 9 with SimpleFieldFilterScope

use of org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope in project sonarqube by SonarSource.

the class ProjectMeasuresIndex method createFilters.

private AllFilters createFilters(ProjectMeasuresQuery query) {
    AllFilters filters = RequestFiltersComputer.newAllFilters();
    filters.addFilter("__indexType", new SimpleFieldFilterScope(FIELD_INDEX_TYPE), termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName()));
    if (!query.isIgnoreAuthorization()) {
        filters.addFilter("__authorization", new SimpleFieldFilterScope("parent"), authorizationTypeSupport.createQueryFilter());
    }
    Multimap<String, MetricCriterion> metricCriterionMultimap = ArrayListMultimap.create();
    query.getMetricCriteria().forEach(metricCriterion -> metricCriterionMultimap.put(metricCriterion.getMetricKey(), metricCriterion));
    metricCriterionMultimap.asMap().forEach((key, value) -> {
        BoolQueryBuilder metricFilters = boolQuery();
        value.stream().map(ProjectMeasuresIndex::toQuery).forEach(metricFilters::must);
        filters.addFilter(key, new NestedFieldFilterScope<>(FIELD_MEASURES, SUB_FIELD_MEASURES_KEY, key), metricFilters);
    });
    query.getQualityGateStatus().ifPresent(qualityGateStatus -> filters.addFilter(ALERT_STATUS_KEY, ALERT_STATUS.getFilterScope(), termQuery(FIELD_QUALITY_GATE_STATUS, QUALITY_GATE_STATUS.get(qualityGateStatus.name()))));
    query.getProjectUuids().ifPresent(projectUuids -> filters.addFilter("ids", new SimpleFieldFilterScope("_id"), termsQuery("_id", projectUuids)));
    query.getLanguages().ifPresent(languages -> filters.addFilter(FILTER_LANGUAGES, LANGUAGES.getFilterScope(), termsQuery(FIELD_LANGUAGES, languages)));
    query.getTags().ifPresent(tags -> filters.addFilter(FIELD_TAGS, TAGS.getFilterScope(), termsQuery(FIELD_TAGS, tags)));
    query.getQualifiers().ifPresent(qualifiers -> filters.addFilter(FIELD_QUALIFIER, new SimpleFieldFilterScope(FIELD_QUALIFIER), termsQuery(FIELD_QUALIFIER, qualifiers)));
    query.getQueryText().map(ProjectsTextSearchQueryFactory::createQuery).ifPresent(queryBuilder -> filters.addFilter("textQuery", new SimpleFieldFilterScope(FIELD_NAME), queryBuilder));
    return filters;
}
Also used : SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope) MetricCriterion(org.sonar.server.measure.index.ProjectMeasuresQuery.MetricCriterion) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) AllFilters(org.sonar.server.es.searchrequest.RequestFiltersComputer.AllFilters)

Example 10 with SimpleFieldFilterScope

use of org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope in project sonarqube by SonarSource.

the class RequestFiltersComputerTest method filters_on_field_of_sticky_TopAggregation_go_to_PostFilters_and_TopAgg_Filters_on_other_fields.

@Test
public void filters_on_field_of_sticky_TopAggregation_go_to_PostFilters_and_TopAgg_Filters_on_other_fields() {
    AllFilters allFilters = newAllFilters();
    // has topAggs and two filters
    String field1 = "field1";
    SimpleFieldFilterScope filterScopeField1 = new SimpleFieldFilterScope(field1);
    SimpleFieldTopAggregationDefinition stickyTopAggField1 = new SimpleFieldTopAggregationDefinition(field1, STICKY);
    SimpleFieldTopAggregationDefinition nonStickyTopAggField1 = new SimpleFieldTopAggregationDefinition(field1, NON_STICKY);
    QueryBuilder filterField1_1 = newQuery();
    QueryBuilder filterField1_2 = newQuery();
    allFilters.addFilter("filter_field1_1", filterScopeField1, filterField1_1);
    allFilters.addFilter("filter_field1_2", filterScopeField1, filterField1_2);
    // has topAggs and one filter
    String field2 = "field2";
    SimpleFieldFilterScope filterScopeField2 = new SimpleFieldFilterScope(field2);
    SimpleFieldTopAggregationDefinition stickyTopAggField2 = new SimpleFieldTopAggregationDefinition(field2, STICKY);
    SimpleFieldTopAggregationDefinition nonStickyTopAggField2 = new SimpleFieldTopAggregationDefinition(field2, NON_STICKY);
    QueryBuilder filterField2 = newQuery();
    allFilters.addFilter("filter_field2", filterScopeField2, filterField2);
    // has only non-sticky top-agg and one filter
    String field3 = "field3";
    SimpleFieldFilterScope filterScopeField3 = new SimpleFieldFilterScope(field3);
    SimpleFieldTopAggregationDefinition nonStickyTopAggField3 = new SimpleFieldTopAggregationDefinition(field3, NON_STICKY);
    QueryBuilder filterField3 = newQuery();
    allFilters.addFilter("filter_field3", filterScopeField3, filterField3);
    // has one filter but no top agg
    String field4 = "field4";
    SimpleFieldFilterScope filterScopeField4 = new SimpleFieldFilterScope(field4);
    QueryBuilder filterField4 = newQuery();
    allFilters.addFilter("filter_field4", filterScopeField4, filterField4);
    // has top-aggs by no filter
    String field5 = "field5";
    SimpleFieldTopAggregationDefinition stickyTopAggField5 = new SimpleFieldTopAggregationDefinition(field5, STICKY);
    SimpleFieldTopAggregationDefinition nonStickyTopAggField5 = new SimpleFieldTopAggregationDefinition(field5, NON_STICKY);
    Set<TopAggregationDefinition<?>> declaredTopAggregations = ImmutableSet.of(stickyTopAggField1, nonStickyTopAggField1, stickyTopAggField2, nonStickyTopAggField2, nonStickyTopAggField3, stickyTopAggField5, nonStickyTopAggField5);
    RequestFiltersComputer underTest = new RequestFiltersComputer(allFilters, declaredTopAggregations);
    assertThat(underTest.getQueryFilters()).contains(toBoolQuery(filterField3, filterField4));
    QueryBuilder[] postFilters = { filterField1_1, filterField1_2, filterField2 };
    assertThat(underTest.getPostFilters()).contains(toBoolQuery(postFilters));
    assertTopAggregationFilter(underTest, stickyTopAggField1, filterField2);
    assertTopAggregationFilter(underTest, nonStickyTopAggField1, postFilters);
    assertTopAggregationFilter(underTest, stickyTopAggField2, filterField1_1, filterField1_2);
    assertTopAggregationFilter(underTest, nonStickyTopAggField2, postFilters);
    assertTopAggregationFilter(underTest, nonStickyTopAggField3, postFilters);
    assertTopAggregationFilter(underTest, stickyTopAggField5, postFilters);
    assertTopAggregationFilter(underTest, nonStickyTopAggField5, postFilters);
}
Also used : SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope) RequestFiltersComputer.newAllFilters(org.sonar.server.es.searchrequest.RequestFiltersComputer.newAllFilters) AllFilters(org.sonar.server.es.searchrequest.RequestFiltersComputer.AllFilters) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.Test)

Aggregations

SimpleFieldFilterScope (org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope)16 Test (org.junit.Test)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)7 AllFilters (org.sonar.server.es.searchrequest.RequestFiltersComputer.AllFilters)6 NestedFieldFilterScope (org.sonar.server.es.searchrequest.TopAggregationDefinition.NestedFieldFilterScope)5 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4 RequestFiltersComputer.newAllFilters (org.sonar.server.es.searchrequest.RequestFiltersComputer.newAllFilters)4 PeriodStart (org.sonar.server.issue.index.IssueQuery.PeriodStart)1 MetricCriterion (org.sonar.server.measure.index.ProjectMeasuresQuery.MetricCriterion)1