Search in sources :

Example 81 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.

the class RuleIndex method search.

public SearchIdResult<String> search(RuleQuery query, SearchOptions options) {
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    QueryBuilder qb = buildQuery(query);
    Map<String, QueryBuilder> filters = buildFilters(query);
    if (!options.getFacets().isEmpty()) {
        for (AggregationBuilder aggregation : getFacets(query, options, qb, filters).values()) {
            sourceBuilder.aggregation(aggregation);
        }
    }
    setSorting(query, sourceBuilder);
    setPagination(options, sourceBuilder);
    BoolQueryBuilder fb = boolQuery();
    for (QueryBuilder filterBuilder : filters.values()) {
        fb.must(filterBuilder);
    }
    sourceBuilder.query(boolQuery().must(qb).filter(fb));
    SearchRequest esSearch = EsClient.prepareSearch(TYPE_RULE).source(sourceBuilder);
    return new SearchIdResult<>(client.search(esSearch), input -> input, system2.getDefaultTimeZone().toZoneId());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.join.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchIdResult(org.sonar.server.es.SearchIdResult) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 82 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.

the class RuleIndex method listTags.

public List<String> listTags(@Nullable String query, int size) {
    int maxPageSize = 500;
    checkArgument(size <= maxPageSize, "Page size must be lower than or equals to " + maxPageSize);
    if (size <= 0) {
        return emptyList();
    }
    TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS).field(FIELD_RULE_TAGS).size(size).order(BucketOrder.key(true)).minDocCount(1);
    ofNullable(query).map(EsUtils::escapeSpecialRegexChars).map(queryString -> ".*" + queryString + ".*").map(s -> new IncludeExclude(s, null)).ifPresent(termsAggregation::includeExclude);
    SearchRequest request = EsClient.prepareSearch(TYPE_RULE.getMainType()).source(new SearchSourceBuilder().query(matchAllQuery()).size(0).aggregation(termsAggregation));
    SearchResponse esResponse = client.search(request);
    return EsUtils.termsKeys(esResponse.getAggregations().get(AGGREGATION_NAME_FOR_TAGS));
}
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) SearchRequest(org.elasticsearch.action.search.SearchRequest) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 83 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.

the class BulkIndexerTest method bulk_delete.

@Test
public void bulk_delete() {
    int max = 500;
    int removeFrom = 200;
    FakeDoc[] docs = new FakeDoc[max];
    for (int i = 0; i < max; i++) {
        docs[i] = FakeIndexDefinition.newDoc(i);
    }
    es.putDocuments(TYPE_FAKE, docs);
    assertThat(count()).isEqualTo(max);
    SearchRequest req = EsClient.prepareSearch(TYPE_FAKE).source(new SearchSourceBuilder().query(QueryBuilders.rangeQuery(FakeIndexDefinition.INT_FIELD).gte(removeFrom)));
    BulkIndexer.delete(es.client(), TYPE_FAKE, req);
    assertThat(count()).isEqualTo(removeFrom);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Test(org.junit.Test)

Example 84 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.

the class UserIndex method search.

public SearchResult<UserDoc> search(UserQuery userQuery, SearchOptions options) {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(options.getLimit()).from(options.getOffset()).sort(FIELD_NAME, SortOrder.ASC);
    BoolQueryBuilder filter = boolQuery().must(termQuery(FIELD_ACTIVE, true));
    QueryBuilder esQuery = matchAllQuery();
    Optional<String> textQuery = userQuery.getTextQuery();
    if (textQuery.isPresent()) {
        esQuery = QueryBuilders.multiMatchQuery(textQuery.get(), 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(Operator.AND);
    }
    SearchRequest request = EsClient.prepareSearch(UserIndexDefinition.TYPE_USER).source(searchSourceBuilder.query(boolQuery().must(esQuery).filter(filter)));
    return new SearchResult<>(esClient.search(request), UserDoc::new, system2.getDefaultTimeZone().toZoneId());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResult(org.sonar.server.es.SearchResult) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 85 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.

the class EsTester method after.

@Override
protected void after() {
    if (isCustom) {
        // delete non-core indices
        String[] existingIndices = getIndicesNames();
        Stream.of(existingIndices).filter(i -> !CORE_INDICES_NAMES.contains(i)).forEach(EsTester::deleteIndexIfExists);
    }
    BulkIndexer.delete(ES_REST_CLIENT, IndexType.main(ALL_INDICES, "dummy"), EsClient.prepareSearch(ALL_INDICES.getName()).source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())));
}
Also used : IndexRelationType(org.sonar.server.es.IndexType.IndexRelationType) BuiltIndex(org.sonar.server.es.newindex.BuiltIndex) ComponentIndexDefinition(org.sonar.server.component.index.ComponentIndexDefinition) Environment(org.elasticsearch.env.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ALL_INDICES(org.sonar.server.es.Index.ALL_INDICES) Random(java.util.Random) Collections2(com.google.common.collect.Collections2) BindHttpException(org.elasticsearch.http.BindHttpException) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ParentJoinPlugin(org.elasticsearch.join.ParentJoinPlugin) NodeValidationException(org.elasticsearch.node.NodeValidationException) ServerSocket(java.net.ServerSocket) RecoverySettings(org.elasticsearch.indices.recovery.RecoverySettings) Loggers(org.sonar.api.utils.log.Loggers) IndexRequest(org.elasticsearch.action.index.IndexRequest) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) Path(java.nio.file.Path) TimeValue(org.elasticsearch.core.TimeValue) SearchHit(org.elasticsearch.search.SearchHit) ImmutableMap(com.google.common.collect.ImmutableMap) ConstructorUtils(org.apache.commons.lang.reflect.ConstructorUtils) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) REFRESH_IMMEDIATE(org.sonar.server.es.newindex.DefaultIndexSettings.REFRESH_IMMEDIATE) Set(java.util.Set) ProjectMeasuresIndexDefinition(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition) Netty4Plugin(org.elasticsearch.transport.Netty4Plugin) IndexDefinitionContext(org.sonar.server.es.IndexDefinition.IndexDefinitionContext) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Stream(java.util.stream.Stream) ExternalResource(org.junit.rules.ExternalResource) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) SortOrder(org.elasticsearch.search.sort.SortOrder) RuleIndexDefinition(org.sonar.server.rule.index.RuleIndexDefinition) IssueIndexDefinition(org.sonar.server.issue.index.IssueIndexDefinition) ViewIndexDefinition(org.sonar.server.view.index.ViewIndexDefinition) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) Iterables(com.google.common.collect.Iterables) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) DiskThresholdSettings(org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalSettingsPreparer(org.elasticsearch.node.InternalSettingsPreparer) SearchRequest(org.elasticsearch.action.search.SearchRequest) ArrayList(java.util.ArrayList) UpdateSettingsRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) Strings(org.elasticsearch.common.Strings) DatagramSocket(java.net.DatagramSocket) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) DiscoveryModule(org.elasticsearch.discovery.DiscoveryModule) CommonAnalysisPlugin(org.elasticsearch.analysis.common.CommonAnalysisPlugin) Node(org.elasticsearch.node.Node) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) FIELD_INDEX_TYPE(org.sonar.server.es.IndexType.FIELD_INDEX_TYPE) NoSuchElementException(java.util.NoSuchElementException) Logger(org.sonar.api.utils.log.Logger) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) QueryBuilders.matchAllQuery(org.elasticsearch.index.query.QueryBuilders.matchAllQuery) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Files(java.nio.file.Files) Throwables(com.google.common.base.Throwables) UserIndexDefinition(org.sonar.server.user.index.UserIndexDefinition) IOException(java.io.IOException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) NewIndex(org.sonar.server.es.newindex.NewIndex) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) Collections(java.util.Collections) HttpHost(org.apache.http.HttpHost) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)193 SearchResponse (org.elasticsearch.action.search.SearchResponse)101 SearchRequest (org.elasticsearch.action.search.SearchRequest)76 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)41 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)37 IOException (java.io.IOException)36 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)34 Search (io.searchbox.core.Search)32 Matchers.containsString (org.hamcrest.Matchers.containsString)32 Test (org.junit.Test)29 List (java.util.List)26 Map (java.util.Map)25 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)24 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)24 ArrayList (java.util.ArrayList)20 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)20 Set (java.util.Set)19 Collectors (java.util.stream.Collectors)18 SearchHit (org.elasticsearch.search.SearchHit)18 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)16