aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-es/src
diff options
context:
space:
mode:
authorJacek <jacek.poreda@sonarsource.com>2021-12-21 12:04:34 +0100
committersonartech <sonartech@sonarsource.com>2021-12-21 20:03:11 +0000
commitc46b02c711f77fc57088f357fcfaf85f3a737cae (patch)
tree229594ecbcc48d5cb55eb3287070fa85141752aa /server/sonar-webserver-es/src
parent32a5fecb407c19060301d2364d12002135b2aefe (diff)
downloadsonarqube-c46b02c711f77fc57088f357fcfaf85f3a737cae.tar.gz
sonarqube-c46b02c711f77fc57088f357fcfaf85f3a737cae.zip
[NO-JIRA] Fix Bugs, Code Smells
Diffstat (limited to 'server/sonar-webserver-es/src')
-rw-r--r--server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java8
-rw-r--r--server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java8
2 files changed, 14 insertions, 2 deletions
diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java
index 6a72d0558a4..5d52b1e2237 100644
--- a/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java
+++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java
@@ -26,6 +26,7 @@ import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.annotation.Nullable;
+import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
@@ -53,6 +54,7 @@ import org.sonar.server.es.textsearch.ComponentTextSearchQueryFactory;
import org.sonar.server.es.textsearch.ComponentTextSearchQueryFactory.ComponentTextSearchQuery;
import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
+import static java.util.Optional.ofNullable;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;
@@ -194,7 +196,11 @@ public class ComponentIndex {
SearchHits hitList = docs.getHits();
SearchHit[] hits = hitList.getHits();
- return new ComponentHitsPerQualifier(bucket.getKey(), ComponentHit.fromSearchHits(hits), hitList.getTotalHits().value);
+ return new ComponentHitsPerQualifier(bucket.getKey(), ComponentHit.fromSearchHits(hits), getTotalHits(hitList.getTotalHits()).value);
+ }
+
+ private static TotalHits getTotalHits(@Nullable TotalHits totalHits) {
+ return ofNullable(totalHits).orElseThrow(() -> new IllegalStateException("Could not get total hits of search results"));
}
private static <T> void setNullable(@Nullable T parameter, Consumer<T> consumer) {
diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java
index 5b20ee0d1ef..6bcfd8eaa03 100644
--- a/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java
+++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java
@@ -32,6 +32,7 @@ import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.annotation.Nullable;
+import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
@@ -74,6 +75,7 @@ import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.emptyList;
+import static java.util.Optional.ofNullable;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
@@ -270,7 +272,7 @@ public class ProjectMeasuresIndex {
SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType())
.source(searchSourceBuilder));
- statistics.setProjectCount(response.getHits().getTotalHits().value);
+ 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))
@@ -282,6 +284,10 @@ public class ProjectMeasuresIndex {
return statistics.build();
}
+ private static TotalHits getTotalHits(@Nullable TotalHits totalHits) {
+ return ofNullable(totalHits).orElseThrow(() -> new IllegalStateException("Could not get total hits of search results"));
+ }
+
private static void addSort(ProjectMeasuresQuery query, SearchSourceBuilder requestBuilder) {
String sort = query.getSort();
if (SORT_BY_NAME.equals(sort)) {