use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method searchTelemetryStatistics.
public ProjectMeasuresStatistics searchTelemetryStatistics() {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().fetchSource(false).size(0);
BoolQueryBuilder esFilter = boolQuery().filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())).filter(termQuery(FIELD_QUALIFIER, Qualifiers.PROJECT));
searchSourceBuilder.query(esFilter);
searchSourceBuilder.aggregation(AggregationBuilders.terms(FIELD_LANGUAGES).field(FIELD_LANGUAGES).size(MAX_PAGE_SIZE).minDocCount(1).order(BucketOrder.count(false)));
searchSourceBuilder.aggregation(AggregationBuilders.nested(FIELD_NCLOC_DISTRIBUTION, FIELD_NCLOC_DISTRIBUTION).subAggregation(AggregationBuilders.terms(FIELD_NCLOC_DISTRIBUTION + "_terms").field(FIELD_NCLOC_DISTRIBUTION_LANGUAGE).size(MAX_PAGE_SIZE).minDocCount(1).order(BucketOrder.count(false)).subAggregation(sum(FIELD_NCLOC_DISTRIBUTION_NCLOC).field(FIELD_NCLOC_DISTRIBUTION_NCLOC))));
searchSourceBuilder.aggregation(AggregationBuilders.nested(NCLOC_KEY, FIELD_MEASURES).subAggregation(AggregationBuilders.filter(NCLOC_KEY + "_filter", termQuery(FIELD_MEASURES_MEASURE_KEY, NCLOC_KEY)).subAggregation(sum(NCLOC_KEY + "_filter_sum").field(FIELD_MEASURES_MEASURE_VALUE))));
ProjectMeasuresStatistics.Builder statistics = ProjectMeasuresStatistics.builder();
SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder));
statistics.setProjectCount(getTotalHits(response.getHits().getTotalHits()).value);
statistics.setProjectCountByLanguage(termsToMap(response.getAggregations().get(FIELD_LANGUAGES)));
Function<Terms.Bucket, Long> bucketToNcloc = bucket -> Math.round(((Sum) bucket.getAggregations().get(FIELD_NCLOC_DISTRIBUTION_NCLOC)).getValue());
Map<String, Long> nclocByLanguage = Stream.of((Nested) response.getAggregations().get(FIELD_NCLOC_DISTRIBUTION)).map(nested -> (Terms) nested.getAggregations().get(nested.getName() + "_terms")).flatMap(terms -> terms.getBuckets().stream()).collect(MoreCollectors.uniqueIndex(Bucket::getKeyAsString, bucketToNcloc));
statistics.setNclocByLanguage(nclocByLanguage);
return statistics.build();
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method search.
public SearchIdResult<String> search(ProjectMeasuresQuery query, SearchOptions searchOptions) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().fetchSource(false).trackTotalHits(true).from(searchOptions.getOffset()).size(searchOptions.getLimit());
AllFilters allFilters = createFilters(query);
RequestFiltersComputer filtersComputer = createFiltersComputer(searchOptions, allFilters);
addFacets(searchSourceBuilder, searchOptions, filtersComputer, query);
addSort(query, searchSourceBuilder);
filtersComputer.getQueryFilters().ifPresent(searchSourceBuilder::query);
filtersComputer.getPostFilters().ifPresent(searchSourceBuilder::postFilter);
SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder));
return new SearchIdResult<>(response, id -> id, system2.getDefaultTimeZone().toZoneId());
}
use of org.graylog.shaded.elasticsearch6.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.elasticsearch6.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.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project herd by FINRAOS.
the class BusinessObjectDefinitionIndexSearchDaoImpl method findAllBusinessObjectDefinitions.
@Override
public ElasticsearchResponseDto findAllBusinessObjectDefinitions(String indexName, String documentType, Set<String> facetFieldsList) {
LOGGER.info("Elasticsearch get all business object definition documents from index, indexName={} and documentType={}.", indexName, documentType);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.fetchSource(new String[] { DATA_PROVIDER_NAME_SOURCE, DESCRIPTION_SOURCE, DISPLAY_NAME_SOURCE, NAME_SOURCE, NAMESPACE_CODE_SOURCE }, null);
ElasticsearchResponseDto elasticsearchResponseDto = new ElasticsearchResponseDto();
// Create a search request and set the scroll time and scroll size
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(new ElasticsearchClientImpl(), SearchAction.INSTANCE);
searchRequestBuilder.setIndices(indexName);
searchRequestBuilder.setTypes(documentType).setSource(searchSourceBuilder).addSort(SortBuilders.fieldSort(BUSINESS_OBJECT_DEFINITION_SORT_FIELD).order(SortOrder.ASC)).addSort(SortBuilders.fieldSort(NAMESPACE_CODE_SORT_FIELD).order(SortOrder.ASC));
// Add aggregation builder if facet fields are present
addFacetFieldAggregations(facetFieldsList, elasticsearchResponseDto, searchRequestBuilder, indexName);
elasticsearchResponseDto.setBusinessObjectDefinitionIndexSearchResponseDtos(scrollSearchResultsIntoBusinessObjectDefinitionDto(searchRequestBuilder, indexName));
return elasticsearchResponseDto;
}
Aggregations