]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6019 Restore "facets=true" behavior
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 12 Jan 2015 16:31:51 +0000 (17:31 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 12 Jan 2015 16:32:06 +0000 (17:32 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java
server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/filter_by_tags.json

index e8c188ca5b165da60cf81121f1b780fccaa089ce..f056364cf47651f65e057abd8307b13b853b1dcb 100644 (file)
@@ -27,7 +27,14 @@ import org.elasticsearch.action.search.SearchResponse;
 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;
@@ -41,11 +48,23 @@ import org.sonar.api.server.debt.DebtCharacteristic;
 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;
 
@@ -315,13 +334,22 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     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;
 
@@ -342,7 +370,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     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);
       }
     }
index 88763142bca914a3a1dcf677eb59a845ebe3a9b4..89cd40c61fe67dfb533092e2f26f39eed7cc5045 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.rule.ws;
 
 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;
@@ -32,7 +33,6 @@ import org.sonar.server.rule.Rule;
 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;
@@ -42,7 +42,6 @@ import javax.annotation.CheckForNull;
 
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Map;
 
 /**
  * @since 4.4
@@ -68,6 +67,8 @@ public class SearchAction extends SearchRequestHandler<RuleQuery, Rule> {
 
   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;
@@ -248,27 +249,20 @@ public class SearchAction extends SearchRequestHandler<RuleQuery, Rule> {
     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
@@ -289,9 +283,6 @@ public class SearchAction extends SearchRequestHandler<RuleQuery, Rule> {
     if (contextForResponse.getFieldsToReturn().contains("actives")) {
       activeRuleCompleter.completeSearch(doQuery(request), result.getHits(), json);
     }
-    if (contextForResponse.isFacet()) {
-      writeFacets(result, json);
-    }
   }
 
   @Override
index b842bad4de944e2a9712725fc8715260823e151b..50c8bf802ba9bee77cba1e09858e235af2b280c5 100644 (file)
@@ -146,7 +146,7 @@ public class RuleIndexMediumTest {
     // Repositories Facet is preset
     result = index.search(query, new QueryContext().addFacets(Arrays.asList("repositories", "tags")));
     assertThat(result.getFacets()).isNotNull();
-    assertThat(result.getFacets()).hasSize(3);
+    assertThat(result.getFacets()).hasSize(2);
 
     // Verify the value of a given facet
     Collection<FacetValue> repoFacets = result.getFacetValues("repositories");
index e7299ab3c100b11047f5210c77b04b6621ff4b47..85ad9dd64885c3c2d371f2122cbfd8c06115684e 100644 (file)
@@ -449,6 +449,7 @@ public class RulesWebServiceMediumTest {
     WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
     request.setParam(SearchAction.PARAM_TAGS, "tag1");
     request.setParam(SearchOptions.PARAM_FIELDS, "sysTags, tags");
+    request.setParam(SearchAction.PARAM_FACETS, "tags");
     WsTester.Result result = request.execute();
     result.assertJson(this.getClass(), "filter_by_tags.json");
   }
index a1bf812f7a374c23533a9a53fb3661b857c60738..e109ed28cc87826e1ab8f6687095dc9420a875da 100644 (file)
@@ -4,4 +4,19 @@
     "sysTags": ["tag1"],
     "tags": []
   }
+],
+"facets": [
+  {
+    "property": "tags",
+    "values": [
+      {
+        "val": "tag1",
+        "count": 1
+      },
+      {
+        "val": "tag2",
+        "count": 1
+      }
+    ]
+  }
 ]}