Search in sources :

Example 61 with BoolQueryBuilder

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

the class RuleIndex method buildQuery.

/* Build main query (search based) */
private static QueryBuilder buildQuery(RuleQuery query) {
    // No contextual query case
    String queryText = query.getQueryText();
    if (StringUtils.isEmpty(queryText)) {
        return matchAllQuery();
    }
    // Build RuleBased contextual query
    BoolQueryBuilder qb = boolQuery();
    String queryString = query.getQueryText();
    // Human readable type of querying
    qb.should(simpleQueryStringQuery(query.getQueryText()).field(SEARCH_WORDS_ANALYZER.subField(FIELD_RULE_NAME), 20f).field(FIELD_RULE_HTML_DESCRIPTION, 3f).defaultOperator(SimpleQueryStringBuilder.Operator.AND)).boost(20f);
    // Match and partial Match queries
    // Search by key uses the "sortable" sub-field as it requires to be case-insensitive (lower-case filtering)
    qb.should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_RULE_KEY), queryString).operator(MatchQueryBuilder.Operator.AND).boost(30f));
    qb.should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_RULE_RULE_KEY), queryString).operator(MatchQueryBuilder.Operator.AND).boost(15f));
    qb.should(termQuery(FIELD_RULE_LANGUAGE, queryString, 3f));
    qb.should(termQuery(FIELD_RULE_ALL_TAGS, queryString, 10f));
    qb.should(termAnyQuery(FIELD_RULE_ALL_TAGS, queryString, 1f));
    return qb;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 62 with BoolQueryBuilder

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

the class RuleIndex method searchAll.

/**
   * Return all keys matching the search query, without pagination nor facets
   */
public Iterator<RuleKey> searchAll(RuleQuery query) {
    SearchRequestBuilder esSearch = getClient().prepareSearch(INDEX_TYPE_RULE).setSearchType(SearchType.SCAN).setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
    QueryBuilder qb = buildQuery(query);
    Map<String, QueryBuilder> filters = buildFilters(query);
    setSorting(query, esSearch);
    BoolQueryBuilder fb = boolQuery();
    for (QueryBuilder filterBuilder : filters.values()) {
        fb.must(filterBuilder);
    }
    esSearch.setQuery(boolQuery().must(qb).filter(fb));
    SearchResponse response = esSearch.get();
    return scrollIds(getClient(), response.getScrollId(), ToRuleKey.INSTANCE);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) 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) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 63 with BoolQueryBuilder

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

the class RuleIndex method addTermFilter.

private static BoolQueryBuilder addTermFilter(BoolQueryBuilder filter, String field, @Nullable Collection<String> values) {
    if (isNotEmpty(values)) {
        BoolQueryBuilder valuesFilter = boolQuery();
        for (String value : values) {
            QueryBuilder valueFilter = QueryBuilders.termQuery(field, value);
            valuesFilter.should(valueFilter);
        }
        filter.must(valuesFilter);
    }
    return filter;
}
Also used : 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)

Example 64 with BoolQueryBuilder

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

the class RuleIndex method addStatusFacetIfNeeded.

private static void addStatusFacetIfNeeded(SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
    if (options.getFacets().contains(FACET_STATUSES)) {
        BoolQueryBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(FIELD_RULE_STATUS);
        AbstractAggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter").filter(facetFilter).subAggregation(AggregationBuilders.terms(FACET_STATUSES).field(FIELD_RULE_STATUS).include(Joiner.on('|').join(ALL_STATUSES_EXCEPT_REMOVED)).exclude(RuleStatus.REMOVED.toString()).size(ALL_STATUSES_EXCEPT_REMOVED.size()));
        aggregations.put(FACET_STATUSES, AggregationBuilders.global(FACET_STATUSES).subAggregation(statuses));
    }
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 65 with BoolQueryBuilder

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

the class ComponentIndex method createQuery.

private QueryBuilder createQuery(ComponentIndexQuery query, ComponentTextSearchFeature... features) {
    BoolQueryBuilder esQuery = boolQuery();
    esQuery.filter(authorizationTypeSupport.createQueryFilter());
    ComponentTextSearchQuery componentTextSearchQuery = ComponentTextSearchQuery.builder().setQueryText(query.getQuery()).setFieldKey(FIELD_KEY).setFieldName(FIELD_NAME).build();
    return esQuery.must(ComponentTextSearchQueryFactory.createQuery(componentTextSearchQuery, features));
}
Also used : ComponentTextSearchQuery(org.sonar.server.es.textsearch.ComponentTextSearchQueryFactory.ComponentTextSearchQuery) 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