Search in sources :

Example 91 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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)

Example 92 with SearchRequest

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

the class ProjectMeasuresIndexerTest method assertThatProjectHasTag.

private void assertThatProjectHasTag(ComponentDto project, String expectedTag) {
    SearchRequest request = prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(new SearchSourceBuilder().query(boolQuery().filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())).filter(termQuery(FIELD_TAGS, expectedTag))));
    assertThat(es.client().search(request).getHits().getHits()).extracting(SearchHit::getId).contains(project.uuid());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 93 with SearchRequest

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

the class ProjectMeasuresIndexerTest method assertThatQualifierIs.

private void assertThatQualifierIs(String qualifier, String... componentsUuid) {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQuery().filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())).filter(termQuery(FIELD_QUALIFIER, qualifier)).filter(termsQuery(FIELD_UUID, componentsUuid)));
    SearchRequest request = prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder);
    assertThat(es.client().search(request).getHits().getHits()).extracting(SearchHit::getId).containsExactlyInAnyOrder(componentsUuid);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 94 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project metron by apache.

the class ElasticsearchRequestSubmitterTest method searchShouldHandleShardFailure.

@Test
public void searchShouldHandleShardFailure() throws InvalidSearchException, IOException {
    // mocks
    SearchResponse response = mock(SearchResponse.class);
    SearchRequest request = new SearchRequest();
    ShardSearchFailure fail = mock(ShardSearchFailure.class);
    SearchShardTarget target = new SearchShardTarget("node1", mock(Index.class), 1, "metron");
    // response will have status of OK
    when(response.status()).thenReturn(RestStatus.OK);
    // response will indicate 1 search hit
    SearchHits hits = mock(SearchHits.class);
    when(hits.getTotalHits()).thenReturn(1L);
    // the response will report shard failures
    when(response.getFailedShards()).thenReturn(1);
    when(response.getTotalShards()).thenReturn(2);
    when(response.getHits()).thenReturn(hits);
    // the response will return the failures
    ShardSearchFailure[] failures = { fail };
    when(response.getShardFailures()).thenReturn(failures);
    // shard failure needs to report the node
    when(fail.shard()).thenReturn(target);
    // shard failure needs to report details of failure
    when(fail.index()).thenReturn("bro_index_2017-10-11");
    when(fail.shardId()).thenReturn(1);
    // search should succeed, even with failed shards
    ElasticsearchRequestSubmitter submitter = setup(response);
    SearchResponse actual = submitter.submitSearch(request);
    assertNotNull(actual);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.jupiter.api.Test)

Example 95 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project metron by apache.

the class ElasticsearchRetrieveLatestDao method searchByGuids.

/**
 * Return the search hit based on the UUID and sensor type.
 * A callback can be specified to transform the hit into a type T.
 * If more than one hit happens, the first one will be returned.
 */
<T> List<T> searchByGuids(Collection<String> guids, Collection<String> sensorTypes, Function<SearchHit, Optional<T>> callback) throws IOException {
    if (guids == null || guids.isEmpty()) {
        return Collections.emptyList();
    }
    // should match any of the guids
    // the 'guid' field must be of type 'keyword' or this term query will not match
    BoolQueryBuilder guidQuery = boolQuery().must(termsQuery(Constants.GUID, guids));
    // should match any of the sensor types
    BoolQueryBuilder sensorQuery = boolQuery();
    sensorTypes.forEach(sensorType -> sensorQuery.should(typeQuery(sensorType + "_doc")));
    // must have a match for both guid and sensor
    BoolQueryBuilder query = boolQuery().must(guidQuery).must(sensorQuery);
    // submit the search
    SearchResponse response;
    try {
        SearchSourceBuilder source = new SearchSourceBuilder().query(query).size(guids.size());
        SearchRequest request = new SearchRequest().source(source);
        response = submitter.submitSearch(request);
    } catch (InvalidSearchException e) {
        throw new IOException(e);
    }
    // transform the search hits to results using the callback
    List<T> results = new ArrayList<>();
    for (SearchHit hit : response.getHits()) {
        Optional<T> result = callback.apply(hit);
        result.ifPresent(r -> results.add(r));
    }
    return results;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) InvalidSearchException(org.apache.metron.indexing.dao.search.InvalidSearchException) SearchHit(org.elasticsearch.search.SearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)156 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)81 SearchResponse (org.elasticsearch.action.search.SearchResponse)69 Test (org.junit.Test)37 IOException (java.io.IOException)31 SearchHit (org.elasticsearch.search.SearchHit)25 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)22 ArrayList (java.util.ArrayList)21 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)21 HashMap (java.util.HashMap)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 List (java.util.List)16 SearchHits (org.elasticsearch.search.SearchHits)15 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 Map (java.util.Map)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)11 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)10