*/
package org.sonar.server.rule;
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.sonar.server.qualityprofile.QProfileRuleResult;
import org.sonar.server.search.SearchIndex;
+import javax.annotation.Nullable;
+
import java.util.Collection;
import java.util.List;
import java.util.Map;
+import static com.google.common.collect.Lists.newArrayList;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
import static org.sonar.server.rule.RuleRegistry.*;
return new QProfileRuleResult(result, PagingResult.create(paging.pageSize(), paging.pageIndex(), hits.getTotalHits()));
}
+ // FIXME Due to a bug in E/S, As the query filter contain a filter with has_parent, nothing will be returned
+ public List<Integer> searchProfileRuleIds(ProfileRuleQuery query) {
+ BoolFilterBuilder filter = activeRuleFilter(query);
+
+ SearchRequestBuilder builder = index.client()
+ .prepareSearch(INDEX_RULES)
+ .setTypes(TYPE_ACTIVE_RULE)
+ .setFilter(filter);
+ List<String> documentIds = index.findDocumentIds(builder, 2);
+ return newArrayList(Iterables.transform(documentIds, new Function<String, Integer>() {
+ @Override
+ public Integer apply(@Nullable String input) {
+ return Integer.valueOf(input);
+ }
+ }));
+ }
+
public QProfileRuleResult searchInactiveProfileRules(ProfileRuleQuery query, Paging paging) {
BoolFilterBuilder filter = parentRuleFilter(query);
addMustTermOrTerms(filter, RuleDocument.FIELD_SEVERITY, query.severities());
}
}
-
public void addMappingFromClasspath(String index, String type, String resourcePath) {
try {
URL resource = getClass().getResource(resourcePath);
}
public List<String> findDocumentIds(SearchQuery searchQuery) {
+ SearchRequestBuilder builder = searchQuery.toBuilder(client);
+ return findDocumentIds(builder, searchQuery.scrollSize());
+ }
+
+ public List<String> findDocumentIds(SearchRequestBuilder builder, int scrollSize) {
List<String> result = Lists.newArrayList();
final int scrollTime = 100;
- SearchRequestBuilder builder = searchQuery.toBuilder(client);
StopWatch watch = createWatch();
SearchResponse scrollResp = builder.addField("_id")
- .setSearchType(SearchType.SCAN)
- .setScroll(new TimeValue(scrollTime))
- .setSize(searchQuery.scrollSize()).execute().actionGet();
+ .setSearchType(SearchType.SCAN)
+ .setScroll(new TimeValue(scrollTime))
+ .setSize(scrollSize).execute().actionGet();
//Scroll until no hits are returned
while (true) {
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(scrollTime)).execute().actionGet();
import org.elasticsearch.client.Requests;
import org.junit.After;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.api.rules.RulePriority;
}
@Test
- public void should_find_active_rules() throws Exception {
-
+ public void find_profile_rules() {
Paging paging = Paging.create(10, 1);
// All rules for profile 1
}
@Test
- public void should_get_from_active_rule() {
+ @Ignore("bug in E/S : fail to do a scroll when filter contain has_parent -> return good total_hits but hits is empty")
+ public void find_profile_rule_ids() {
+ // All rules for profile 1
+ List<Integer> result = profileRules.searchProfileRuleIds(ProfileRuleQuery.create(1));
+ assertThat(result).hasSize(3);
+ assertThat(result.get(0)).isEqualTo(1); }
+
+ @Test
+ public void get_from_active_rule() {
assertThat(profileRules.getFromActiveRuleId(391)).isNotNull();
}
@Test
- public void should_get_from_rule() {
+ public void get_from_rule() {
assertThat(profileRules.getFromActiveRuleId(25)).isNotNull();
}
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
public class SearchIndexTest {
assertThat(docIds).hasSize(numberOfDocuments);
}
+ @Test
+ public void should_iterate_over_small_dataset() throws Exception {
+ final int numberOfDocuments = 3;
+
+ searchIndex.addMappingFromClasspath("index", "type1", "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json");
+ String[] ids = new String[numberOfDocuments];
+ BytesStream[] sources = new BytesStream[numberOfDocuments];
+ for (int i=0; i<numberOfDocuments; i++) {
+ ids[i] = Integer.toString(i);
+ sources[i] = XContentFactory.jsonBuilder().startObject().field("value", Integer.toString(i)).endObject();
+ }
+ searchIndex.bulkIndex("index", "type1", ids, sources);
+
+ List<String> docIds = searchIndex.findDocumentIds(SearchQuery.create().scrollSize(100));
+ assertThat(docIds).hasSize(numberOfDocuments);
+ }
+
@Test(expected = StrictDynamicMappingException.class)
public void should_forbid_dynamic_mapping() throws Exception {
searchIndex.addMappingFromClasspath("index", "type1", "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json");