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());
}
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());
}
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);
}
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);
}
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;
}
Aggregations