Search in sources :

Example 81 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project sonarqube by SonarSource.

the class EsRequestDetailsTest method should_format_SearchRequest.

@Test
public void should_format_SearchRequest() {
    SearchRequest searchRequest = Requests.searchRequest("index").types("type");
    assertThat(EsRequestDetails.computeDetailsAsString(searchRequest)).isEqualTo("ES search request 'SearchRequest{searchType=QUERY_THEN_FETCH, indices=[index]," + " indicesOptions=IndicesOptions[ignore_unavailable=false, allow_no_indices=true," + " expand_wildcards_open=true, expand_wildcards_closed=false, expand_wildcards_hidden=false," + " allow_aliases_to_multiple_indices=true, forbid_closed_indices=true, ignore_aliases=false," + " ignore_throttled=true], types=[type], routing='null', preference='null', requestCache=null," + " scroll=null, maxConcurrentShardRequests=0, batchedReduceSize=512, preFilterShardSize=null," + " allowPartialSearchResults=null, localClusterAlias=null, getOrCreateAbsoluteStartMillis=-1," + " ccsMinimizeRoundtrips=true, enableFieldsEmulation=false, source={}}' on indices '[index]' on types '[type]'");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Test(org.junit.Test)

Example 82 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest 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 83 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest 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 84 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project sonarqube by SonarSource.

the class IssueIndex method getMinCreatedAt.

private OptionalLong getMinCreatedAt(AllFilters filters) {
    String facetNameAndField = CREATED_AT.getFieldName();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(0);
    BoolQueryBuilder esFilter = boolQuery();
    filters.stream().filter(Objects::nonNull).forEach(esFilter::must);
    if (esFilter.hasClauses()) {
        sourceBuilder.query(QueryBuilders.boolQuery().filter(esFilter));
    }
    sourceBuilder.aggregation(AggregationBuilders.min(facetNameAndField).field(facetNameAndField));
    SearchRequest request = EsClient.prepareSearch(TYPE_ISSUE.getMainType()).source(sourceBuilder);
    Min minValue = client.search(request).getAggregations().get(facetNameAndField);
    double actualValue = minValue.getValue();
    if (Double.isInfinite(actualValue)) {
        return OptionalLong.empty();
    }
    return OptionalLong.of((long) actualValue);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Min(org.elasticsearch.search.aggregations.metrics.Min) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 85 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project sonarqube by SonarSource.

the class ComponentIndex method search.

public SearchIdResult<String> search(ComponentQuery query, SearchOptions searchOptions) {
    SearchSourceBuilder source = new SearchSourceBuilder().fetchSource(false).trackTotalHits(true).from(searchOptions.getOffset()).size(searchOptions.getLimit());
    BoolQueryBuilder esQuery = boolQuery();
    esQuery.filter(authorizationTypeSupport.createQueryFilter());
    setNullable(query.getQuery(), q -> {
        ComponentTextSearchQuery componentTextSearchQuery = ComponentTextSearchQuery.builder().setQueryText(q).setFieldKey(FIELD_KEY).setFieldName(FIELD_NAME).build();
        esQuery.must(ComponentTextSearchQueryFactory.createQuery(componentTextSearchQuery, ComponentTextSearchFeatureRepertoire.values()));
    });
    setEmptiable(query.getQualifiers(), q -> esQuery.must(termsQuery(FIELD_QUALIFIER, q)));
    source.sort(SORTABLE_ANALYZER.subField(FIELD_NAME), SortOrder.ASC);
    source.query(esQuery);
    SearchRequest request = EsClient.prepareSearch(TYPE_COMPONENT.getMainType()).source(source);
    return new SearchIdResult<>(client.search(request), id -> id, system2.getDefaultTimeZone().toZoneId());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ComponentTextSearchQuery(org.sonar.server.es.textsearch.ComponentTextSearchQueryFactory.ComponentTextSearchQuery) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchIdResult(org.sonar.server.es.SearchIdResult) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)607 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)418 SearchResponse (org.elasticsearch.action.search.SearchResponse)361 IOException (java.io.IOException)201 SearchHit (org.elasticsearch.search.SearchHit)113 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)99 ArrayList (java.util.ArrayList)68 Test (org.junit.Test)57 Test (org.junit.jupiter.api.Test)55 SearchHits (org.elasticsearch.search.SearchHits)51 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)49 TimeValue (org.elasticsearch.common.unit.TimeValue)43 HashMap (java.util.HashMap)42 List (java.util.List)38 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)37 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)35 IndexRequest (org.elasticsearch.action.index.IndexRequest)34 Map (java.util.Map)33 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)30 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)30