use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method addFacets.
private static void addFacets(SearchSourceBuilder esRequest, SearchOptions options, RequestFiltersComputer filtersComputer, ProjectMeasuresQuery query) {
TopAggregationHelper topAggregationHelper = new TopAggregationHelper(filtersComputer, new SubAggregationHelper());
options.getFacets().stream().map(FACETS_BY_NAME::get).filter(Objects::nonNull).map(facet -> facet.getFacetBuilder().buildFacet(facet, query, topAggregationHelper)).forEach(esRequest::aggregation);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class CreateActionTest method create_user.
@Test
public void create_user() {
logInAsSystemAdministrator();
CreateWsResponse response = call(CreateRequest.builder().setLogin("john").setName("John").setEmail("john@email.com").setScmAccounts(singletonList("jn")).setPassword("1234").build());
assertThat(response.getUser()).extracting(User::getLogin, User::getName, User::getEmail, User::getScmAccountsList, User::getLocal).containsOnly("john", "John", "john@email.com", singletonList("jn"), true);
// exists in index
assertThat(es.client().search(EsClient.prepareSearch(UserIndexDefinition.TYPE_USER).source(new SearchSourceBuilder().query(boolQuery().must(termQuery(FIELD_LOGIN, "john")).must(termQuery(FIELD_NAME, "John")).must(termQuery(FIELD_EMAIL, "john@email.com")).must(termQuery(FIELD_SCM_ACCOUNTS, "jn"))))).getHits().getHits()).hasSize(1);
// exists in db
Optional<UserDto> dbUser = db.users().selectUserByLogin("john");
assertThat(dbUser).isPresent();
assertThat(dbUser.get().isRoot()).isFalse();
// member of default group
assertThat(db.users().selectGroupUuidsOfUser(dbUser.get())).containsOnly(defaultGroup.getUuid());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder 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;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project metron by apache.
the class ElasticsearchUtils method queryAllResults.
/**
* Elasticsearch queries default to 10 records returned. Some internal queries require that all
* results are returned. Rather than setting an arbitrarily high size, this method pages through results
* and returns them all in a single SearchResponse.
* @param qb A QueryBuilder that provides the query to be run.
* @return A SearchResponse containing the appropriate results.
*/
public static SearchResponse queryAllResults(RestHighLevelClient transportClient, QueryBuilder qb, String index, int pageSize) throws IOException {
org.elasticsearch.action.search.SearchRequest request = new org.elasticsearch.action.search.SearchRequest();
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(qb);
builder.size(pageSize);
builder.fetchSource(true);
builder.storedField("*");
request.source(builder);
request.indices(index);
org.elasticsearch.action.search.SearchResponse esResponse = transportClient.search(request);
List<SearchResult> allResults = getSearchResults(esResponse);
long total = esResponse.getHits().getTotalHits();
if (total > pageSize) {
int pages = (int) (total / pageSize) + 1;
for (int i = 1; i < pages; i++) {
int from = i * pageSize;
builder.from(from);
esResponse = transportClient.search(request);
allResults.addAll(getSearchResults(esResponse));
}
}
SearchResponse searchResponse = new SearchResponse();
searchResponse.setTotal(total);
searchResponse.setResults(allResults);
return searchResponse;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project dq-easy-cloud by dq-open-cloud.
the class TransportClient method count.
/**
* Count文档
*
* @throws Exception
*/
@Test
public void count() throws Exception {
String[] name = new String[] { "T:o\"m-", "Jerry" };
String from = "2016-09-01T00:00:00";
String to = "2016-10-01T00:00:00";
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("name", name)).must(QueryBuilders.rangeQuery("birth").gte(from).lte(to));
searchSourceBuilder.query(queryBuilder);
String query = searchSourceBuilder.toString();
System.out.println(query);
Count count = new Count.Builder().addIndex(indexName).addType(typeName).query(query).build();
CountResult results = jestClient.execute(count);
Double counts = results.getCount();
System.out.println("Count:" + counts);
}
Aggregations