Search in sources :

Example 31 with SearchRequestBuilder

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

the class SortingTest method descending_sort_on_single_field.

@Test
public void descending_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", false);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(1);
    expectField(fields.get(0), "updatedAt", "_last", SortOrder.DESC);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) Test(org.junit.Test)

Example 32 with SearchRequestBuilder

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

the class SortingTest method descending_sort_on_single_field_with_missing_in_last_position.

@Test
public void descending_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", false);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(1);
    expectField(fields.get(0), "updatedAt", "_first", SortOrder.DESC);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) Test(org.junit.Test)

Example 33 with SearchRequestBuilder

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

the class BulkIndexerTest method bulk_delete.

@Test
public void bulk_delete() throws Exception {
    int max = 500;
    int removeFrom = 200;
    FakeDoc[] docs = new FakeDoc[max];
    for (int i = 0; i < max; i++) {
        docs[i] = FakeIndexDefinition.newDoc(i);
    }
    esTester.putDocuments(INDEX_TYPE_FAKE, docs);
    assertThat(count()).isEqualTo(max);
    SearchRequestBuilder req = esTester.client().prepareSearch(INDEX_TYPE_FAKE).setQuery(QueryBuilders.rangeQuery(FakeIndexDefinition.INT_FIELD).gte(removeFrom));
    BulkIndexer.delete(esTester.client(), INDEX, req);
    assertThat(count()).isEqualTo(removeFrom);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Test(org.junit.Test)

Example 34 with SearchRequestBuilder

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

the class ProjectMeasuresIndexerTest method update_existing_document_when_indexing_one_project.

@Test
public void update_existing_document_when_indexing_one_project() throws Exception {
    String uuid = "PROJECT-UUID";
    esTester.putDocuments(INDEX_TYPE_PROJECT_MEASURES, new ProjectMeasuresDoc().setId(uuid).setKey("Old Key").setName("Old Name").setTags(singletonList("old tag")).setAnalysedAt(new Date(1_000_000L)));
    ComponentDto project = newProjectDto(dbTester.getDefaultOrganization(), uuid).setKey("New key").setName("New name").setTagsString("new tag");
    SnapshotDto analysis = componentDbTester.insertProjectAndSnapshot(project);
    underTest.indexProject(project.uuid(), ProjectIndexer.Cause.NEW_ANALYSIS);
    assertThat(esTester.getIds(INDEX_TYPE_PROJECT_MEASURES)).containsOnly(uuid);
    SearchRequestBuilder request = esTester.client().prepareSearch(INDEX_TYPE_PROJECT_MEASURES).setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery().must(termQuery("_id", uuid)).must(termQuery(FIELD_KEY, "New key")).must(termQuery(FIELD_NAME, "New name")).must(termQuery(FIELD_TAGS, "new tag")).must(termQuery(FIELD_ANALYSED_AT, new Date(analysis.getCreatedAt())))));
    assertThat(request.get().getHits()).hasSize(1);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Date(java.util.Date) Test(org.junit.Test)

Example 35 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project wonderdog by infochimps-labs.

the class ElasticSearchStreamingInputFormat method findNumHits.

private void findNumHits() {
    SearchRequestBuilder request = client.prepareSearch(indexName);
    if (mappingName != null && mappingName.length() > 0) {
        request.setTypes(mappingName);
    }
    request.setSearchType(SearchType.COUNT);
    if (queryJSON != null && queryJSON.length() > 0) {
        request.setQuery(queryJSON);
    }
    SearchResponse response = request.execute().actionGet();
    this.numHits = response.getHits().totalHits();
    LOG.info("Ran query: " + String.valueOf(numHits) + " hits");
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

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