import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.index.query.*;
+import org.elasticsearch.index.query.BoolFilterBuilder;
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.FilterBuilder;
+import org.elasticsearch.index.query.FilterBuilders;
+import org.elasticsearch.index.query.MatchQueryBuilder;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.index.query.SimpleQueryStringBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
import org.sonar.server.rule.Rule;
-import org.sonar.server.search.*;
+import org.sonar.server.search.BaseIndex;
+import org.sonar.server.search.IndexDefinition;
+import org.sonar.server.search.IndexField;
+import org.sonar.server.search.QueryContext;
+import org.sonar.server.search.Result;
+import org.sonar.server.search.SearchClient;
+import org.sonar.server.search.StickyFacetBuilder;
import javax.annotation.CheckForNull;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
import static com.google.common.collect.Lists.newArrayList;
return filters;
}
- protected Map<String, AggregationBuilder> getFacets(QueryBuilder query, Map<String, FilterBuilder> filters) {
+ protected Map<String, AggregationBuilder> getFacets(RuleQuery query, QueryContext options, QueryBuilder queryBuilder, Map<String, FilterBuilder> filters) {
Map<String, AggregationBuilder> aggregations = new HashMap<String, AggregationBuilder>();
- StickyFacetBuilder stickyFacetBuilder = stickyFacetBuilder(query, filters);
- aggregations.put(FACET_LANGUAGES + "global", stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.LANGUAGE.field(), FACET_LANGUAGES));
- aggregations.put(FACET_TAGS + "global", stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.ALL_TAGS.field(), FACET_TAGS));
- aggregations.put(FACET_REPOSITORIES + "global", stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.REPOSITORY.field(), FACET_REPOSITORIES));
+ StickyFacetBuilder stickyFacetBuilder = stickyFacetBuilder(queryBuilder, filters);
+ if (options.facets().contains("languages") || options.facets().contains("true")) {
+ aggregations.put(FACET_LANGUAGES, // + "global",
+ stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.LANGUAGE.field(), FACET_LANGUAGES, query.getLanguages() == null ? new String[0] : query.getLanguages().toArray()));
+ }
+ if (options.facets().contains("tags") || options.facets().contains("true")) {
+ aggregations.put(FACET_TAGS, // + "global",
+ stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.ALL_TAGS.field(), FACET_TAGS, query.getTags() == null ? new String[0] : query.getTags().toArray()));
+ }
+ if (options.facets().contains("repositories") || options.facets().contains("true")) {
+ aggregations.put(FACET_REPOSITORIES, // + "global",
+ stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.REPOSITORY.field(), FACET_REPOSITORIES, query.getRepositories() == null ? new String[0] : query.getRepositories().toArray()));
+ }
return aggregations;
Map<String, FilterBuilder> filters = this.getFilters(query, options);
if (options.isFacet()) {
- for (AggregationBuilder aggregation : getFacets(qb, filters).values()) {
+ for (AggregationBuilder aggregation : getFacets(query, options, qb, filters).values()) {
esSearch.addAggregation(aggregation);
}
}
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.server.rule.RuleService;
import org.sonar.server.rule.index.RuleNormalizer;
import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.search.FacetValue;
import org.sonar.server.search.QueryContext;
import org.sonar.server.search.Result;
import org.sonar.server.search.ws.SearchOptions;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Map;
/**
* @since 4.4
public static final String SEARCH_ACTION = "search";
+ private static final Collection<String> DEFAULT_FACETS = ImmutableSet.of("languages", "repositories", "tags");
+
private final RuleService ruleService;
private final ActiveRuleCompleter activeRuleCompleter;
private final RuleMapping mapping;
json.endArray();
}
- private void writeFacets(Result<Rule> results, JsonWriter json) {
- json.name("facets").beginArray();
- for (Map.Entry<String, Collection<FacetValue>> facet : results.getFacets().entrySet()) {
- json.beginObject();
- json.prop("property", facet.getKey());
- json.name("values").beginArray();
- for (FacetValue facetValue : facet.getValue()) {
- json.beginObject();
- json.prop("val", facetValue.getKey());
- json.prop("count", facetValue.getValue());
- json.endObject();
- }
- json.endArray().endObject();
- }
- json.endArray();
- }
-
@Override
protected QueryContext getQueryContext(Request request) {
// TODO Get rid of this horrible hack: fields on request are not the same as fields for ES search ! 1/2
- return mapping.newQueryOptions(SearchOptions.create(request));
+ QueryContext context = super.getQueryContext(request);
+ QueryContext searchQueryContext = mapping.newQueryOptions(SearchOptions.create(request))
+ .setLimit(context.getLimit())
+ .setOffset(context.getOffset())
+ .setScroll(context.isScroll());
+ if (context.facets().contains("true")) {
+ searchQueryContext.addFacets(DEFAULT_FACETS);
+ } else {
+ searchQueryContext.addFacets(context.facets());
+ }
+ return searchQueryContext;
}
@Override
if (contextForResponse.getFieldsToReturn().contains("actives")) {
activeRuleCompleter.completeSearch(doQuery(request), result.getHits(), json);
}
- if (contextForResponse.isFacet()) {
- writeFacets(result, json);
- }
}
@Override