From c1f4f7790c70830e2f199751acb3006e31141bc7 Mon Sep 17 00:00:00 2001 From: Michal Duda Date: Tue, 12 Jan 2021 14:06:57 +0100 Subject: [PATCH] SONAR-14325 fix rule search results no longer sorted by name --- .../org/sonar/server/rule/ws/SearchAction.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java index f23a3da53d2..22e2025522b 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java @@ -23,6 +23,7 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.collect.Ordering; import java.util.ArrayList; import java.util.Collection; @@ -236,8 +237,17 @@ public class SearchAction implements RulesWsAction { private SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) { SearchIdResult result = ruleIndex.search(query, context); - List rules = dbClient.ruleDao().selectByUuids(dbSession, result.getUuids()); - List ruleUuids = rules.stream().map(RuleDto::getUuid).collect(Collectors.toList()); + List ruleUuids = result.getUuids(); + // rule order is managed by ES, this order by must be kept when fetching rule details + Map rulesByRuleKey = Maps.uniqueIndex(dbClient.ruleDao().selectByUuids(dbSession, ruleUuids), RuleDto::getUuid); + List rules = new ArrayList<>(); + for (String ruleUuid : ruleUuids) { + RuleDto rule = rulesByRuleKey.get(ruleUuid); + if (rule != null) { + rules.add(rule); + } + } + List templateRuleUuids = rules.stream() .map(RuleDto::getTemplateUuid) .filter(Objects::nonNull) -- 2.39.5