package org.sonar.server.rule.index;
import com.google.common.base.Function;
+import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
+import org.elasticsearch.search.aggregations.bucket.global.GlobalBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.SEVERITY.field(), FACET_SEVERITIES, Severity.ALL.toArray()));
}
+ if (options.facets().contains(FACET_DEBT_CHARACTERISTICS)) {
+ int characsSize = 10;
+ int subCharacsSize = 300;
+ Collection<String> characsFromQuery = query.getDebtCharacteristics();
+ Object[] selectedChars = characsFromQuery == null ? new Object[0] : characsFromQuery.toArray();
+ AggregationBuilder debtChar = AggregationBuilders.filter(FACET_DEBT_CHARACTERISTICS + "__chars")
+ .filter(stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field()))
+ .subAggregation(
+ AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_top").field(RuleNormalizer.RuleField.CHARACTERISTIC.field())
+ .size(characsSize))
+ .subAggregation(
+ AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_selected").field(RuleNormalizer.RuleField.CHARACTERISTIC.field())
+ .include(Joiner.on('|').join(selectedChars))
+ .size(characsSize));
+ AggregationBuilder debtSubChar = AggregationBuilders.filter(FACET_DEBT_CHARACTERISTICS + "__subchars")
+ .filter(stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field()))
+ .subAggregation(
+ AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__subchars_top").field(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())
+ .size(subCharacsSize))
+ .subAggregation(
+ AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_selected").field(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())
+ .include(Joiner.on('|').join(selectedChars))
+ .size(subCharacsSize));
+ GlobalBuilder debtCharTopLevel = AggregationBuilders.global(FACET_DEBT_CHARACTERISTICS)
+ .subAggregation(debtChar)
+ .subAggregation(debtSubChar);
+ aggregations.put(FACET_DEBT_CHARACTERISTICS, debtCharTopLevel);
+ }
+
return aggregations;
}
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
public class Result<K> {
} else if (Terms.class.isAssignableFrom(aggregation.getClass())) {
Terms termAggregation = (Terms) aggregation;
for (Terms.Bucket value : termAggregation.getBuckets()) {
- this.facets.put(aggregation.getName().replace("_selected",""), new FacetValue(value.getKey(), value.getDocCount()));
+ String facetName = aggregation.getName().replace("_selected", "");
+ if (aggregation.getName().contains("__")) {
+ facetName = facetName.substring(0, facetName.indexOf("__"));
+ }
+ this.facets.put(facetName, new FacetValue(value.getKey(), value.getDocCount()));
}
} else if (HasAggregations.class.isAssignableFrom(aggregation.getClass())) {
HasAggregations hasAggregations = (HasAggregations) aggregation;
MockUserSession.set();
WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn");
+ request.setParam(SearchAction.PARAM_FACETS, "debt_characteristics");
WsTester.Result result = request.execute();
result.assertJson(this.getClass(), "search_debt_rule.json");
}