Search in sources :

Example 26 with SearchRequestBuilder

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

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

the class UserIndex method selectUsersForBatch.

public Iterator<UserDoc> selectUsersForBatch(List<String> logins) {
    BoolQueryBuilder filter = boolQuery().filter(termsQuery(FIELD_LOGIN, logins));
    SearchRequestBuilder requestBuilder = esClient.prepareSearch(UserIndexDefinition.INDEX_TYPE_USER).setSearchType(SearchType.SCAN).addSort(SortBuilders.fieldSort(FIELD_LOGIN).order(SortOrder.ASC)).setScroll(TimeValue.timeValueMinutes(EsUtils.SCROLL_TIME_IN_MINUTES)).setSize(10_000).setFetchSource(new String[] { FIELD_LOGIN, FIELD_NAME }, null).setQuery(filter);
    SearchResponse response = requestBuilder.get();
    return EsUtils.scroll(esClient, response.getScrollId(), DOC_CONVERTER);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 28 with SearchRequestBuilder

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

the class TestIndexer method deleteByFile.

public void deleteByFile(String fileUuid) {
    SearchRequestBuilder searchRequest = esClient.prepareSearch(INDEX_TYPE_TEST).setQuery(QueryBuilders.termsQuery(FIELD_FILE_UUID, fileUuid));
    BulkIndexer.delete(esClient, INDEX_TYPE_TEST.getIndex(), searchRequest);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder)

Example 29 with SearchRequestBuilder

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

the class ViewIndex method delete.

public void delete(Collection<String> viewUuids) {
    SearchRequestBuilder searchRequest = esClient.prepareSearch(ViewIndexDefinition.INDEX_TYPE_VIEW).setQuery(boolQuery().must(matchAllQuery()).filter(termsQuery(ViewIndexDefinition.FIELD_UUID, viewUuids)));
    BulkIndexer.delete(esClient, ViewIndexDefinition.INDEX_TYPE_VIEW.getIndex(), searchRequest);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder)

Example 30 with SearchRequestBuilder

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

the class SortingTest method fail_if_unknown_field.

@Test
public void fail_if_unknown_field() {
    Sorting sorting = new Sorting();
    sorting.add("file");
    try {
        sorting.fill(new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE), "unknown", true);
        fail();
    } catch (BadRequestException e) {
        assertThat(e.getMessage()).isEqualTo("Bad sort field: unknown");
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BadRequestException(org.sonar.server.exceptions.BadRequestException) Test(org.junit.Test)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)110 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)23 SearchHits (org.elasticsearch.search.SearchHits)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 ArrayList (java.util.ArrayList)14 Client (org.elasticsearch.client.Client)13 HashMap (java.util.HashMap)12 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)9 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 Map (java.util.Map)7 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)7 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)7