]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9217 if two suggestions have the same relevancy, prefer A over Z
authorDaniel Schwarz <bartfastiel@users.noreply.github.com>
Thu, 11 May 2017 16:37:02 +0000 (18:37 +0200)
committerGitHub <noreply@github.com>
Thu, 11 May 2017 16:37:02 +0000 (18:37 +0200)
api/components/suggestions used to only use "relevance" (score) in sorting results. If two suggestions have exactly the same relevance (this is a rare case), then we will now also take alphabetical order into account.

server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java

index d03b2220fb37db31224935b91cb1a2bd5827e09a..f0babc80127a7e0580067a9fb702113c1c9aec79 100644 (file)
@@ -37,6 +37,8 @@ import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters.Buck
 import org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits;
 import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder;
 import org.elasticsearch.search.highlight.HighlightBuilder;
+import org.elasticsearch.search.sort.FieldSortBuilder;
+import org.elasticsearch.search.sort.ScoreSortBuilder;
 import org.sonar.server.es.EsClient;
 import org.sonar.server.es.textsearch.ComponentTextSearchFeature;
 import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire;
@@ -116,7 +118,9 @@ public class ComponentIndex {
       .setHighlighterPostTags("</mark>")
       .addHighlightedField(createHighlighter())
       .setFrom(query.getSkip())
-      .setSize(query.getLimit());
+      .setSize(query.getLimit())
+      .addSort(new ScoreSortBuilder())
+      .addSort(new FieldSortBuilder(ComponentIndexDefinition.FIELD_NAME));
     return sub.setFetchSource(false);
   }
 
index 06914666cab0622f4fb9923f66c3465e8a0ea0f3..4961047345438b115b020108637fd712cc188987 100644 (file)
@@ -145,6 +145,13 @@ public class ComponentIndexScoreTest extends ComponentIndexTest {
     assertExactResults("Class java", file3, file2, file1);
   }
 
+  @Test
+  public void if_relevancy_is_equal_fall_back_to_alphabetical_ordering() {
+    assertResultOrder("sonarqube",
+      "sonarqubeA",
+      "sonarqubeB");
+  }
+
   @Test
   public void scoring_test_DbTester() {
     features.set(ComponentTextSearchFeatureRepertoire.PARTIAL);