Search in sources :

Example 1 with Operator

use of org.elasticsearch.index.query.Operator 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();
    BoolQueryBuilder textQuery = boolQuery();
    JavaTokenizer.split(queryText).stream().map(token -> boolQuery().should(matchQuery(SEARCH_GRAMS_ANALYZER.subField(FIELD_RULE_NAME), StringUtils.left(token, DefaultIndexSettings.MAXIMUM_NGRAM_LENGTH)).boost(20F)).should(matchPhraseQuery(ENGLISH_HTML_ANALYZER.subField(FIELD_RULE_HTML_DESCRIPTION), token).boost(3F))).forEach(textQuery::must);
    qb.should(textQuery.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), queryText).operator(Operator.AND).boost(30F));
    qb.should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_RULE_RULE_KEY), queryText).operator(Operator.AND).boost(15F));
    qb.should(termQuery(FIELD_RULE_LANGUAGE, queryText, 3F));
    return qb;
}
Also used : JavaTokenizer(org.sonar.server.es.textsearch.JavaTokenizer) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) SortBuilders(org.elasticsearch.search.sort.SortBuilders) SCROLL_TIME_IN_MINUTES(org.sonar.server.es.EsUtils.SCROLL_TIME_IN_MINUTES) FIELD_RULE_INTERNAL_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_INTERNAL_KEY) FIELD_RULE_CREATED_AT(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_CREATED_AT) TYPE_RULE(org.sonar.server.rule.index.RuleIndexDefinition.TYPE_RULE) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) FIELD_RULE_TAGS(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_TAGS) StickyFacetBuilder(org.sonar.server.es.StickyFacetBuilder) EsClient(org.sonar.server.es.EsClient) FACET_DEFAULT_SIZE(org.sonar.server.es.StickyFacetBuilder.FACET_DEFAULT_SIZE) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SEARCH_WORDS_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_WORDS_ANALYZER) Map(java.util.Map) FIELD_RULE_STATUS(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_STATUS) SearchResponse(org.elasticsearch.action.search.SearchResponse) BucketOrder(org.elasticsearch.search.aggregations.BucketOrder) FIELD_RULE_UPDATED_AT(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_UPDATED_AT) FIELD_ACTIVE_RULE_INHERITANCE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE) JoinAggregationBuilders(org.elasticsearch.join.aggregations.JoinAggregationBuilders) FIELD_RULE_OWASP_TOP_10(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_OWASP_TOP_10) TimeValue(org.elasticsearch.core.TimeValue) FIELD_RULE_NAME(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_NAME) QueryBuilders.matchPhraseQuery(org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) System2(org.sonar.api.utils.System2) SearchIdResult(org.sonar.server.es.SearchIdResult) Collections.emptyList(java.util.Collections.emptyList) VULNERABILITY(org.sonar.api.rules.RuleType.VULNERABILITY) Collection(java.util.Collection) FIELD_RULE_IS_EXTERNAL(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_IS_EXTERNAL) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SORTABLE_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER) ScoreMode(org.apache.lucene.search.join.ScoreMode) FIELD_RULE_REPOSITORY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_REPOSITORY) List(java.util.List) SearchOptions(org.sonar.server.es.SearchOptions) QueryBuilders.matchQuery(org.elasticsearch.index.query.QueryBuilders.matchQuery) HasParentQueryBuilder(org.elasticsearch.join.query.HasParentQueryBuilder) FIELD_RULE_IS_TEMPLATE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_IS_TEMPLATE) SortOrder(org.elasticsearch.search.sort.SortOrder) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) FIELD_RULE_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_KEY) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TRUE(java.lang.Boolean.TRUE) Joiner(com.google.common.base.Joiner) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) FIELD_RULE_SANS_TOP_25(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_SANS_TOP_25) JoinQueryBuilders(org.elasticsearch.join.query.JoinQueryBuilders) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) RuleStatus(org.sonar.api.rule.RuleStatus) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) Function(java.util.function.Function) FIELD_RULE_LANGUAGE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_LANGUAGE) EsUtils(org.sonar.server.es.EsUtils) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) EsUtils.scrollIds(org.sonar.server.es.EsUtils.scrollIds) ENGLISH_HTML_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.ENGLISH_HTML_ANALYZER) SEARCH_GRAMS_ANALYZER(org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER) Operator(org.elasticsearch.index.query.Operator) Severity(org.sonar.api.rule.Severity) FIELD_ACTIVE_RULE_SEVERITY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY) FIELD_RULE_CWE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_CWE) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) FIELD_ACTIVE_RULE_PROFILE_UUID(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_UUID) FIELD_INDEX_TYPE(org.sonar.server.es.IndexType.FIELD_INDEX_TYPE) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) QueryBuilders.termsQuery(org.elasticsearch.index.query.QueryBuilders.termsQuery) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FALSE(java.lang.Boolean.FALSE) FIELD_RULE_SEVERITY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_SEVERITY) FIELD_RULE_SONARSOURCE_SECURITY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_SONARSOURCE_SECURITY) QueryBuilders.matchAllQuery(org.elasticsearch.index.query.QueryBuilders.matchAllQuery) Iterator(java.util.Iterator) Optional.ofNullable(java.util.Optional.ofNullable) FIELD_RULE_HTML_DESCRIPTION(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_HTML_DESCRIPTION) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) FIELD_RULE_RULE_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_RULE_KEY) FIELD_RULE_TEMPLATE_KEY(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_TEMPLATE_KEY) FIELD_RULE_TYPE(org.sonar.server.rule.index.RuleIndexDefinition.FIELD_RULE_TYPE) EsUtils.optimizeScrollRequest(org.sonar.server.es.EsUtils.optimizeScrollRequest) TYPE_ACTIVE_RULE(org.sonar.server.rule.index.RuleIndexDefinition.TYPE_ACTIVE_RULE) DefaultIndexSettings(org.sonar.server.es.newindex.DefaultIndexSettings) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Aggregations

Joiner (com.google.common.base.Joiner)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 FALSE (java.lang.Boolean.FALSE)1 TRUE (java.lang.Boolean.TRUE)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Function (java.util.function.Function)1 Nullable (javax.annotation.Nullable)1 StringUtils (org.apache.commons.lang.StringUtils)1 ScoreMode (org.apache.lucene.search.join.ScoreMode)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 TimeValue (org.elasticsearch.core.TimeValue)1