Search in sources :

Example 26 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder 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 27 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder 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;
}
Also used : HashMap(java.util.HashMap) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) RuleStatus(org.sonar.api.rule.RuleStatus) 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 28 with QueryBuilder

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

Example 29 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder 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);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResult(org.sonar.server.es.SearchResult) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 30 with QueryBuilder

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

the class AdjacencyMatrixIT method testEmptyFilterDeclarations.

// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
    QueryBuilder emptyFilter = new BoolQueryBuilder();
    SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("all", emptyFilter).add("tag1", termQuery("tag", "tag1")))).execute().actionGet();
    assertSearchResponse(response);
    AdjacencyMatrix filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    AdjacencyMatrix.Bucket allBucket = filters.getBucketByKey("all");
    assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
    AdjacencyMatrix.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix.Bucket) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) AdjacencyMatrix(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)344 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)233 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)52 QueryImpl (org.molgenis.data.support.QueryImpl)36 ArrayList (java.util.ArrayList)28 Map (java.util.Map)28 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)26 HashMap (java.util.HashMap)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)22 SearchHit (org.elasticsearch.search.SearchHit)20 IOException (java.io.IOException)19 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)17 Test (org.junit.Test)16 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 List (java.util.List)14 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)14