import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.OrFilterBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
try {
OrFilterBuilder activityFilter = FilterBuilders.orFilter();
for (String profileKey : query.getQprofileKeys()) {
- activityFilter.add(FilterBuilders.termFilter("details.profileKey", profileKey));
+ activityFilter.add(FilterBuilders.nestedFilter("details",
+ QueryBuilders.matchQuery("details.profileKey", profileKey)));
}
SearchResponse response = index.get(ActivityIndex.class).search(query, options, activityFilter);
protected Map mapNestedField(IndexField field) {
Map<String, Object> mapping = new HashMap<String, Object>();
mapping.put("type", "nested");
+ mapping.put("dynamic", "true");
Map<String, Object> mappings = new HashMap<String, Object>();
for (IndexField nestedField : field.nestedFields()) {
if (nestedField != null) {
*/
package org.sonar.server.qualityprofile;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import org.junit.After;
import org.junit.Before;
assertThat(activity.ruleName()).isNull();
}
+ @Test
+ public void search_activity_by_qprofile() throws InterruptedException {
+
+ tester.get(ActivityService.class).write(dbSession, Activity.Type.QPROFILE,
+ ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)));
+ tester.get(ActivityService.class).write(dbSession, Activity.Type.QPROFILE,
+ ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P2_KEY, RuleTesting.XOO_X1)));
+ dbSession.commit();
+
+ // 0. Base case verify 2 activities in index
+ assertThat(service.findActivities(new QProfileActivityQuery(), new QueryOptions()))
+ .hasSize(2);
+
+ // 1. filter by QProfile
+ List<QProfileActivity> result = service.findActivities(new QProfileActivityQuery()
+ .setQprofileKeys(ImmutableSet.of(XOO_P1_KEY)), new QueryOptions());
+ assertThat(result).hasSize(1);
+
+ // 1. filter by QProfiles
+ assertThat(service.findActivities(new QProfileActivityQuery()
+ .setQprofileKeys(ImmutableSet.of(XOO_P1_KEY, XOO_P2_KEY))
+ , new QueryOptions())).hasSize(2);
+ }
+
}