@@ -26,12 +26,9 @@ import java.util.ArrayList; | |||
import java.util.Arrays; | |||
import java.util.Collection; | |||
import java.util.HashMap; | |||
import java.util.HashSet; | |||
import java.util.Iterator; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.lang.StringUtils; | |||
@@ -67,6 +64,7 @@ import org.sonar.server.es.SearchOptions; | |||
import org.sonar.server.es.StickyFacetBuilder; | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
import static java.util.Optional.ofNullable; | |||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery; | |||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; | |||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery; | |||
@@ -120,6 +118,7 @@ public class RuleIndex { | |||
Collections2.filter( | |||
Collections2.transform(Arrays.asList(RuleStatus.values()), RuleStatus::toString), | |||
input -> !RuleStatus.REMOVED.toString().equals(input))); | |||
private static final String AGGREGATION_NAME = "_ref"; | |||
private static final String AGGREGATION_NAME_FOR_TAGS = "tagsAggregation"; | |||
private final EsClient client; | |||
@@ -491,14 +490,12 @@ public class RuleIndex { | |||
esSearch.setSize(options.getLimit()); | |||
} | |||
public Set<String> terms(String fields) { | |||
public List<String> terms(String fields) { | |||
return terms(fields, null, Integer.MAX_VALUE); | |||
} | |||
public Set<String> terms(String fields, @Nullable String query, int size) { | |||
String aggregationKey = "_ref"; | |||
TermsBuilder termsAggregation = AggregationBuilders.terms(aggregationKey) | |||
public List<String> terms(String fields, @Nullable String query, int size) { | |||
TermsBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME) | |||
.field(fields) | |||
.size(size) | |||
.minDocCount(1); | |||
@@ -512,19 +509,10 @@ public class RuleIndex { | |||
.addAggregation(termsAggregation); | |||
SearchResponse esResponse = request.get(); | |||
return extractAggregationTerms(aggregationKey, esResponse); | |||
} | |||
private static Set<String> extractAggregationTerms(String aggregationKey, SearchResponse esResponse) { | |||
Set<String> terms = new HashSet<>(); | |||
Terms aggregation = esResponse.getAggregations().get(aggregationKey); | |||
if (aggregation != null) { | |||
aggregation.getBuckets().forEach(value -> terms.add(value.getKeyAsString())); | |||
} | |||
return terms; | |||
return EsUtils.termsKeys(esResponse.getAggregations().get(AGGREGATION_NAME)); | |||
} | |||
public Set<String> listTags(OrganizationDto organization, @Nullable String query, int size) { | |||
public List<String> listTags(OrganizationDto organization, @Nullable String query, int size) { | |||
TermsQueryBuilder scopeFilter = QueryBuilders.termsQuery( | |||
FIELD_RULE_EXTENSION_SCOPE, | |||
RuleExtensionScope.system().getScope(), | |||
@@ -533,8 +521,9 @@ public class RuleIndex { | |||
TermsBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS) | |||
.field(FIELD_RULE_EXTENSION_TAGS) | |||
.size(size) | |||
.order(Terms.Order.term(true)) | |||
.minDocCount(1); | |||
Optional.ofNullable(query) | |||
ofNullable(query) | |||
.map(EsUtils::escapeSpecialRegexChars) | |||
.map(queryString -> ".*" + queryString + ".*") | |||
.ifPresent(termsAggregation::include); | |||
@@ -546,7 +535,7 @@ public class RuleIndex { | |||
.addAggregation(termsAggregation); | |||
SearchResponse esResponse = request.get(); | |||
return extractAggregationTerms(AGGREGATION_NAME_FOR_TAGS, esResponse); | |||
return EsUtils.termsKeys(esResponse.getAggregations().get(AGGREGATION_NAME_FOR_TAGS)); | |||
} | |||
private static boolean isNotEmpty(@Nullable Collection list) { |
@@ -20,8 +20,8 @@ | |||
package org.sonar.server.rule.ws; | |||
import com.google.common.io.Resources; | |||
import java.util.List; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
@@ -80,7 +80,7 @@ public class TagsAction implements RulesWsAction { | |||
String query = request.param(Param.TEXT_QUERY); | |||
int pageSize = request.mandatoryParamAsInt("ps"); | |||
Set<String> tags = ruleIndex.listTags(organization, query, pageSize == 0 ? Integer.MAX_VALUE : pageSize); | |||
List<String> tags = ruleIndex.listTags(organization, query, pageSize == 0 ? Integer.MAX_VALUE : pageSize); | |||
writeResponse(response, tags); | |||
} | |||
@@ -95,7 +95,7 @@ public class TagsAction implements RulesWsAction { | |||
} | |||
} | |||
private static void writeResponse(Response response, Set<String> tags) { | |||
private static void writeResponse(Response response, List<String> tags) { | |||
JsonWriter json = response.newJsonWriter().beginObject(); | |||
json.name("tags").beginArray(); | |||
tags.forEach(json::value); |
@@ -26,12 +26,10 @@ import com.google.common.collect.ImmutableSet; | |||
import com.google.common.collect.Sets; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import javax.annotation.Nonnull; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.ClassRule; | |||
import org.junit.Ignore; | |||
import org.junit.Test; | |||
import org.sonar.api.rule.RuleKey; | |||
import org.sonar.api.rule.RuleStatus; | |||
@@ -195,7 +193,6 @@ public class RuleUpdaterMediumTest { | |||
assertThat(rule.getNoteUpdatedAt()).isNull(); | |||
} | |||
@Ignore | |||
@Test | |||
public void set_tags() { | |||
// insert db | |||
@@ -205,7 +202,9 @@ public class RuleUpdaterMediumTest { | |||
dbSession.commit(); | |||
// java8 is a system tag -> ignore | |||
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(Sets.newHashSet("bug", "java8")); | |||
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY) | |||
.setTags(Sets.newHashSet("bug", "java8")) | |||
.setOrganization(defaultOrganization); | |||
underTest.update(dbSession, update, defaultOrganization, userSessionRule); | |||
RuleDto rule = ruleDao.selectOrFailByKey(dbSession, defaultOrganization, RULE_KEY); | |||
@@ -213,8 +212,8 @@ public class RuleUpdaterMediumTest { | |||
assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc"); | |||
// verify that tags are indexed in index | |||
Set<String> tags = ruleIndex.listTags(defaultOrganization, null, 10); | |||
assertThat(tags).containsOnly("bug", "java8", "javadoc"); | |||
List<String> tags = ruleIndex.listTags(defaultOrganization, null, 10); | |||
assertThat(tags).containsExactly("bug", "java8", "javadoc"); | |||
} | |||
@Test | |||
@@ -237,8 +236,8 @@ public class RuleUpdaterMediumTest { | |||
assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc"); | |||
// verify that tags are indexed in index | |||
Set<String> tags = ruleIndex.listTags(defaultOrganization, null, 10); | |||
assertThat(tags).containsOnly("java8", "javadoc"); | |||
List<String> tags = ruleIndex.listTags(defaultOrganization, null, 10); | |||
assertThat(tags).containsExactly("java8", "javadoc"); | |||
} | |||
@Test |
@@ -793,7 +793,7 @@ public class RuleIndexTest { | |||
} | |||
@Test | |||
public void tags_facet_should_be_available_if_organization_is_speficied() { | |||
public void tags_facet_should_be_available_if_organization_is_specified() { | |||
RuleQuery query = new RuleQuery() | |||
.setOrganizationUuid("some_org_id"); | |||
SearchOptions options = new SearchOptions().addFacets(singletonList(FACET_TAGS)); |