Search in sources :

Example 1 with HasParentQueryBuilder

use of org.elasticsearch.index.query.HasParentQueryBuilder 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));
    }
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 2 with HasParentQueryBuilder

use of org.elasticsearch.index.query.HasParentQueryBuilder in project sonarqube by SonarSource.

the class AuthorizationTypeSupportTest method createQueryFilter_sets_filter_on_anyone_and_user_id_and_group_ids_if_user_is_logged_in_and_has_groups.

@Test
public void createQueryFilter_sets_filter_on_anyone_and_user_id_and_group_ids_if_user_is_logged_in_and_has_groups() {
    GroupDto group1 = GroupTesting.newGroupDto().setId(10);
    GroupDto group2 = GroupTesting.newGroupDto().setId(11);
    userSession.logIn().setUserId(1234).setGroups(group1, group2);
    HasParentQueryBuilder filter = (HasParentQueryBuilder) underTest.createQueryFilter();
    assertJson(filter.toString()).isSimilarTo("{" + "  \"has_parent\": {" + "    \"query\": {" + "      \"bool\": {" + "        \"filter\": {" + "          \"bool\": {" + "            \"should\": [" + "              {" + "                \"term\": {" + "                  \"allowAnyone\": true" + "                }" + "              }," + "              {" + "                \"term\": {" + "                  \"userIds\": 1234" + "                }" + "              }," + "              {" + "                \"term\": {" + "                  \"groupIds\": 10" + "                }" + "              }," + "              {" + "                \"term\": {" + "                  \"groupIds\": 11" + "                }" + "              }" + "            ]" + "          }" + "        }" + "      }" + "    }," + "    \"parent_type\": \"authorization\"" + "  }" + "}");
}
Also used : GroupDto(org.sonar.db.user.GroupDto) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) Test(org.junit.Test)

Example 3 with HasParentQueryBuilder

use of org.elasticsearch.index.query.HasParentQueryBuilder in project sonarqube by SonarSource.

the class AuthorizationTypeSupportTest method createQueryFilter_sets_filter_on_anyone_group_if_user_is_anonymous.

@Test
public void createQueryFilter_sets_filter_on_anyone_group_if_user_is_anonymous() {
    userSession.anonymous();
    HasParentQueryBuilder filter = (HasParentQueryBuilder) underTest.createQueryFilter();
    assertJson(filter.toString()).isSimilarTo("{" + "  \"has_parent\" : {" + "    \"query\" : {" + "      \"bool\" : {" + "        \"filter\" : {" + "          \"bool\" : {" + "            \"should\" : {" + "              \"term\" : {" + "                \"allowAnyone\" : true" + "              }" + "            }" + "          }" + "        }" + "      }" + "    }," + "    \"parent_type\" : \"authorization\"" + "  }" + "}");
}
Also used : HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) Test(org.junit.Test)

Example 4 with HasParentQueryBuilder

use of org.elasticsearch.index.query.HasParentQueryBuilder in project elasticsearch by elastic.

the class PercolatorFieldMapper method verifyQuery.

/**
     * Fails if a percolator contains an unsupported query. The following queries are not supported:
     * 1) a range query with a date range based on current time
     * 2) a has_child query
     * 3) a has_parent query
     */
static void verifyQuery(QueryBuilder queryBuilder) {
    if (queryBuilder instanceof RangeQueryBuilder) {
        RangeQueryBuilder rangeQueryBuilder = (RangeQueryBuilder) queryBuilder;
        if (rangeQueryBuilder.from() instanceof String) {
            String from = (String) rangeQueryBuilder.from();
            if (from.contains("now")) {
                throw new IllegalArgumentException("percolator queries containing time range queries based on the " + "current time is unsupported");
            }
        }
        if (rangeQueryBuilder.to() instanceof String) {
            String to = (String) rangeQueryBuilder.to();
            if (to.contains("now")) {
                throw new IllegalArgumentException("percolator queries containing time range queries based on the " + "current time is unsupported");
            }
        }
    } else if (queryBuilder instanceof HasChildQueryBuilder) {
        throw new IllegalArgumentException("the [has_child] query is unsupported inside a percolator query");
    } else if (queryBuilder instanceof HasParentQueryBuilder) {
        throw new IllegalArgumentException("the [has_parent] query is unsupported inside a percolator query");
    } else if (queryBuilder instanceof BoolQueryBuilder) {
        BoolQueryBuilder boolQueryBuilder = (BoolQueryBuilder) queryBuilder;
        List<QueryBuilder> clauses = new ArrayList<>();
        clauses.addAll(boolQueryBuilder.filter());
        clauses.addAll(boolQueryBuilder.must());
        clauses.addAll(boolQueryBuilder.mustNot());
        clauses.addAll(boolQueryBuilder.should());
        for (QueryBuilder clause : clauses) {
            verifyQuery(clause);
        }
    } else if (queryBuilder instanceof ConstantScoreQueryBuilder) {
        verifyQuery(((ConstantScoreQueryBuilder) queryBuilder).innerQuery());
    } else if (queryBuilder instanceof FunctionScoreQueryBuilder) {
        verifyQuery(((FunctionScoreQueryBuilder) queryBuilder).query());
    } else if (queryBuilder instanceof BoostingQueryBuilder) {
        verifyQuery(((BoostingQueryBuilder) queryBuilder).negativeQuery());
        verifyQuery(((BoostingQueryBuilder) queryBuilder).positiveQuery());
    }
}
Also used : FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) List(java.util.List) ArrayList(java.util.ArrayList) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoostingQueryBuilder(org.elasticsearch.index.query.BoostingQueryBuilder) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder) BoostingQueryBuilder(org.elasticsearch.index.query.BoostingQueryBuilder)

Example 5 with HasParentQueryBuilder

use of org.elasticsearch.index.query.HasParentQueryBuilder in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testUnsupportedQueries.

public void testUnsupportedQueries() {
    RangeQueryBuilder rangeQuery1 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("2017-01-01||/D");
    RangeQueryBuilder rangeQuery2 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("now");
    PercolatorFieldMapper.verifyQuery(rangeQuery1);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(rangeQuery2));
    PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery1));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery2)));
    PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1)));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(rangeQuery2)));
    PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery2, new MatchAllQueryBuilder())));
    PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery2, new RandomScoreFunctionBuilder())));
    HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
    HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery)));
}
Also used : FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoostingQueryBuilder(org.elasticsearch.index.query.BoostingQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder)

Aggregations

HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)8 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)5 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)3 Test (org.junit.Test)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 BoostingQueryBuilder (org.elasticsearch.index.query.BoostingQueryBuilder)2 ConstantScoreQueryBuilder (org.elasticsearch.index.query.ConstantScoreQueryBuilder)2 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)2 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)2 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RandomScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder)1 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)1 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)1 Field (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)1 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1