Search in sources :

Example 76 with SearchHit

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

the class ComponentIndex method bucketToQualifier.

private static ComponentHitsPerQualifier bucketToQualifier(ParsedFilters.ParsedBucket bucket) {
    ParsedTopHits docs = bucket.getAggregations().get(DOCS_AGGREGATION_NAME);
    SearchHits hitList = docs.getHits();
    SearchHit[] hits = hitList.getHits();
    return new ComponentHitsPerQualifier(bucket.getKey(), ComponentHit.fromSearchHits(hits), getTotalHits(hitList.getTotalHits()).value);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ParsedTopHits(org.elasticsearch.search.aggregations.metrics.ParsedTopHits) SearchHits(org.elasticsearch.search.SearchHits)

Example 77 with SearchHit

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

the class IssueIndexTest method search_nine_issues_with_same_creation_date_sorted_by_creation_date_order_is_sorted_also_by_key.

@Test
public void search_nine_issues_with_same_creation_date_sorted_by_creation_date_order_is_sorted_also_by_key() {
    ComponentDto project = newPrivateProjectDto();
    ComponentDto file = newFileDto(project, null);
    List<IssueDoc> issues = new ArrayList<>();
    // we are adding issues in reverse order to see if the sort is actually doing anything
    for (int i = 9; i >= 1; i--) {
        String key = "I" + i;
        issues.add(newDoc(key, file));
    }
    indexIssues(issues.toArray(new IssueDoc[] {}));
    IssueQuery.Builder query = IssueQuery.builder().asc(true);
    SearchResponse result = underTest.search(query.sort(IssueQuery.SORT_BY_CREATION_DATE).build(), new SearchOptions());
    SearchHit[] hits = result.getHits().getHits();
    for (int i = 1; i <= 9; i++) {
        assertThat(hits[i - 1].getId()).isEqualTo("I" + i);
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ComponentDto(org.sonar.db.component.ComponentDto) ArrayList(java.util.ArrayList) SearchOptions(org.sonar.server.es.SearchOptions) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 78 with SearchHit

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

the class BulkIndexer method addDeletion.

public void addDeletion(SearchRequest searchRequest) {
    // TODO to be replaced by delete_by_query that is back in ES5
    searchRequest.scroll(TimeValue.timeValueMinutes(5)).source().sort("_doc", SortOrder.ASC).size(100).fetchSource(false);
    // this search is synchronous. An optimization would be to be non-blocking,
    // but it requires to tracking pending requests in close().
    // Same semaphore can't be reused because of potential deadlock (requires to acquire
    // two locks)
    SearchResponse searchResponse = esClient.search(searchRequest);
    while (true) {
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            DocumentField routing = hit.field("_routing");
            DeleteRequest deleteRequest = new DeleteRequest(hit.getIndex(), hit.getType(), hit.getId());
            if (routing != null) {
                deleteRequest.routing(routing.getValue());
            }
            add(deleteRequest);
        }
        String scrollId = searchResponse.getScrollId();
        if (scrollId == null) {
            break;
        }
        searchResponse = esClient.scroll(new SearchScrollRequest(scrollId).scroll(TimeValue.timeValueMinutes(5)));
        if (hits.length == 0) {
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(scrollId);
            esClient.clearScroll(clearScrollRequest);
            break;
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) DocumentField(org.elasticsearch.common.document.DocumentField) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 79 with SearchHit

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

the class EsUtilsTest method convertToDocs.

@Test
public void convertToDocs() {
    SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(16) }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1);
    List<BaseDoc> docs = EsUtils.convertToDocs(hits, IssueDoc::new);
    assertThat(docs).hasSize(1);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) IssueDoc(org.sonar.server.issue.index.IssueDoc) SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits) Test(org.junit.Test)

Example 80 with SearchHit

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

the class UserUpdaterCreateTest method create_user.

@Test
public void create_user() {
    createDefaultGroup();
    UserDto dto = underTest.createAndCommit(db.getSession(), NewUser.builder().setLogin("user").setName("User").setEmail("user@mail.com").setPassword("PASSWORD").setScmAccounts(ImmutableList.of("u1", "u_1", "User 1")).build(), u -> {
    });
    assertThat(dto.getUuid()).isNotNull();
    assertThat(dto.getLogin()).isEqualTo("user");
    assertThat(dto.getName()).isEqualTo("User");
    assertThat(dto.getEmail()).isEqualTo("user@mail.com");
    assertThat(dto.getScmAccountsAsList()).containsOnly("u1", "u_1", "User 1");
    assertThat(dto.isActive()).isTrue();
    assertThat(dto.isLocal()).isTrue();
    assertThat(dto.getSalt()).isNotNull();
    assertThat(dto.getHashMethod()).isEqualTo(HashMethod.PBKDF2.name());
    assertThat(dto.getCryptedPassword()).isNotNull();
    assertThat(dto.getCreatedAt()).isPositive().isEqualTo(dto.getUpdatedAt());
    assertThat(dbClient.userDao().selectByLogin(session, "user").getUuid()).isEqualTo(dto.getUuid());
    List<SearchHit> indexUsers = es.getDocuments(UserIndexDefinition.TYPE_USER);
    assertThat(indexUsers).hasSize(1);
    assertThat(indexUsers.get(0).getSourceAsMap()).contains(entry("login", "user"), entry("name", "User"), entry("email", "user@mail.com"));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)318 SearchResponse (org.elasticsearch.action.search.SearchResponse)225 SearchHits (org.elasticsearch.search.SearchHits)87 ArrayList (java.util.ArrayList)83 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)64 HashMap (java.util.HashMap)37 Map (java.util.Map)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)37 IOException (java.io.IOException)35 Test (org.junit.Test)32 TimeValue (org.elasticsearch.common.unit.TimeValue)29 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)22 List (java.util.List)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)17 SearchHitField (org.elasticsearch.search.SearchHitField)16 HashSet (java.util.HashSet)15 Text (org.elasticsearch.common.text.Text)14