Search in sources :

Example 91 with SearchRequestBuilder

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

the class ViewIndex method findAllViewUuids.

public List<String> findAllViewUuids() {
    SearchRequestBuilder esSearch = esClient.prepareSearch(ViewIndexDefinition.INDEX_TYPE_VIEW).addSort("_doc", SortOrder.ASC).setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES)).setFetchSource(false).setSize(100).setQuery(matchAllQuery());
    SearchResponse response = esSearch.get();
    List<String> result = newArrayList();
    while (true) {
        List<SearchHit> hits = newArrayList(response.getHits());
        for (SearchHit hit : hits) {
            result.add(hit.getId());
        }
        String scrollId = response.getScrollId();
        response = esClient.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES)).get();
        // Break condition: No hits are returned
        if (response.getHits().getHits().length == 0) {
            esClient.nativeClient().prepareClearScroll().addScrollId(scrollId).get();
            break;
        }
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 92 with SearchRequestBuilder

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

the class EsTester method getDocuments.

/**
   * Get all the indexed documents (no paginated results). Results are not sorted.
   */
public List<SearchHit> getDocuments(IndexType indexType) {
    SearchRequestBuilder req = client.nativeClient().prepareSearch(indexType.getIndex()).setTypes(indexType.getType()).setQuery(QueryBuilders.matchAllQuery());
    req.setSearchType(SearchType.SCAN).setScroll(new TimeValue(60000)).setSize(100);
    SearchResponse response = req.get();
    List<SearchHit> result = newArrayList();
    while (true) {
        Iterables.addAll(result, response.getHits());
        response = client.nativeClient().prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
        // Break condition: No hits are returned
        if (response.getHits().getHits().length == 0) {
            break;
        }
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 93 with SearchRequestBuilder

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

the class SortingTest method ascending_sort_on_single_field_with_missing_in_last_position.

@Test
public void ascending_sort_on_single_field_with_missing_in_last_position() throws Exception {
    Sorting sorting = new Sorting();
    sorting.add("updatedAt").missingLast();
    SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
    sorting.fill(request, "updatedAt", true);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(1);
    expectField(fields.get(0), "updatedAt", "_last", SortOrder.ASC);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) Test(org.junit.Test)

Example 94 with SearchRequestBuilder

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

the class SortingTest method default_sorting.

@Test
public void default_sorting() throws Exception {
    Sorting sorting = new Sorting();
    sorting.addDefault("file");
    SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
    sorting.fillDefault(request);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(1);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) Test(org.junit.Test)

Example 95 with SearchRequestBuilder

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

the class SortingTest method ascending_sort_on_single_field.

@Test
public void ascending_sort_on_single_field() throws Exception {
    Sorting sorting = new Sorting();
    sorting.add("updatedAt");
    SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
    sorting.fill(request, "updatedAt", true);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(1);
    expectField(fields.get(0), "updatedAt", "_first", SortOrder.ASC);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) 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