]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14224 return correct number of total issues in case index.max_result_window...
authorJacek <jacek.poreda@sonarsource.com>
Tue, 8 Dec 2020 07:49:35 +0000 (08:49 +0100)
committerMathieu Suen <mathieu.suen@sonarsource.com>
Thu, 10 Dec 2020 08:35:50 +0000 (09:35 +0100)
(cherry picked from commit 05958a5bee3282f681696ad311d0a0d98e7cc0bf)

server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java
server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueIndex.java
server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java
server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java
server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java
server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java

index 886f81583ec17771e61ef75e841aed05eb84aca4..ae755e5eee53ef61af6776279798bfad5606dad1 100644 (file)
@@ -83,6 +83,7 @@ public class ComponentIndex {
   public SearchIdResult<String> search(ComponentQuery query, SearchOptions searchOptions) {
     SearchSourceBuilder source = new SearchSourceBuilder()
       .fetchSource(false)
+      .trackTotalHits(true)
       .from(searchOptions.getOffset())
       .size(searchOptions.getLimit());
 
index 0f016ac6c263c1d8ff2ccfe2b796f616782ba1ed..8c35b98e40fef917a2d8d92f46d887c0e420efe2 100644 (file)
@@ -360,7 +360,8 @@ public class IssueIndex {
     configureQuery(sourceBuilder, filterComputer);
     configureTopFilters(sourceBuilder, filterComputer);
 
-    sourceBuilder.fetchSource(false);
+    sourceBuilder.fetchSource(false)
+      .trackTotalHits(true);
 
     return client.search(requestBuilder);
   }
index bf81c3a02d747cd5404e0b66ed72c792a44039e0..635037a7b4ee9556335387f4b55be80e459ffc8a 100644 (file)
@@ -214,6 +214,7 @@ public class ProjectMeasuresIndex {
   public SearchIdResult<String> search(ProjectMeasuresQuery query, SearchOptions searchOptions) {
     SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
       .fetchSource(false)
+      .trackTotalHits(true)
       .from(searchOptions.getOffset())
       .size(searchOptions.getLimit());
 
index d7af68df6e379a9701fff0f50619815df076511d..7b288e55076aad14581475e72f2d1f7233e220f4 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.SearchIdResult;
@@ -40,6 +41,7 @@ import org.sonar.server.tester.UserSessionRule;
 
 import static java.util.Collections.singleton;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT;
 
 public class ComponentIndexSearchTest {
   @Rule
@@ -128,6 +130,18 @@ public class ComponentIndexSearchTest {
     assertThat(result.getUuids()).containsExactlyInAnyOrder(projects.get(3).uuid(), projects.get(4).uuid(), projects.get(5).uuid());
   }
 
+  @Test
+  public void returns_correct_total_number_if_default_index_window_exceeded() {
+    userSession.logIn().setRoot();
+
+    index(IntStream.range(0, 12_000)
+      .mapToObj(i -> newDoc(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization())))
+      .toArray(ComponentDoc[]::new));
+
+    SearchIdResult<String> result = underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3));
+    assertThat(result.getTotal()).isEqualTo(12_000);
+  }
+
   @Test
   public void filter_unauthorized_components() {
     ComponentDto unauthorizedProject = db.components().insertPrivateProject();
@@ -147,4 +161,18 @@ public class ComponentIndexSearchTest {
     indexer.indexAll();
     Arrays.stream(components).forEach(c -> authorizationIndexerTester.allowOnlyAnyone(c));
   }
+
+  private void index(ComponentDoc... componentDocs) {
+    es.putDocuments(TYPE_COMPONENT.getMainType(), componentDocs);
+  }
+
+  private ComponentDoc newDoc(ComponentDto componentDoc) {
+    return new ComponentDoc()
+      .setId(componentDoc.uuid())
+      .setKey(componentDoc.getKey())
+      .setName(componentDoc.name())
+      .setProjectUuid(componentDoc.projectUuid())
+      .setOrganization(componentDoc.getOrganizationUuid())
+      .setQualifier(componentDoc.qualifier());
+  }
 }
index 8563d65c54f432e35c563559f6f469065f7e62a2..ef043c981d1b44d3e0eaaf22e5711f531b95f7d3 100644 (file)
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import org.apache.lucene.search.TotalHits;
+import org.apache.lucene.search.TotalHits.Relation;
 import org.assertj.core.groups.Tuple;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.search.SearchHit;
@@ -130,6 +131,25 @@ public class IssueIndexTest {
     assertThat(result.getHits().getHits()).hasSize(SearchOptions.MAX_PAGE_SIZE);
   }
 
+  // SONAR-14224
+  @Test
+  public void search_exceeding_default_index_max_window() {
+    ComponentDto project = newPrivateProjectDto(newOrganizationDto());
+    ComponentDto file = newFileDto(project, null);
+    List<IssueDoc> issues = new ArrayList<>();
+    for (int i = 0; i < 11_000; i++) {
+      String key = "I" + i;
+      issues.add(newDoc(key, file));
+    }
+    indexIssues(issues.toArray(new IssueDoc[] {}));
+
+    IssueQuery.Builder query = IssueQuery.builder();
+    SearchResponse result = underTest.search(query.build(), new SearchOptions().setLimit(500));
+    assertThat(result.getHits().getHits()).hasSize(SearchOptions.MAX_PAGE_SIZE);
+    assertThat(result.getHits().getTotalHits().value).isEqualTo(11_000L);
+    assertThat(result.getHits().getTotalHits().relation).isEqualTo(Relation.EQUAL_TO);
+  }
+
   @Test
   public void authorized_issues_on_groups() {
     OrganizationDto org = newOrganizationDto();
index be7cdf6be7d2587dd53980e99df23ace5d62e83b..4e6ae085da278432780b9591439080896727cc85 100644 (file)
@@ -513,6 +513,17 @@ public class ProjectMeasuresIndexTest {
       APP1, APP2, APP3);
   }
 
+  @Test
+  public void return_correct_number_of_total_if_exceeds_index_max_results() {
+    index(IntStream.range(0, 12_000)
+      .mapToObj(operand -> newDoc(ComponentTesting.newPrivateProjectDto(ORG)))
+      .toArray(ProjectMeasuresDoc[]::new));
+
+    ProjectMeasuresQuery query = new ProjectMeasuresQuery();
+    SearchIdResult<String> result = underTest.search(query, new SearchOptions());
+    assertThat(result.getTotal()).isEqualTo(12_000);
+  }
+
   @Test
   public void return_only_projects_and_applications_authorized_for_user() {
     indexForUser(USER1, newDoc(PROJECT1), newDoc(PROJECT2),