import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.index.query.FilterBuilders;
+import org.elasticsearch.index.query.OrFilterBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.sonar.core.activity.Activity;
import org.sonar.core.activity.db.ActivityDto;
.setTypes(this.getIndexType())
.setIndices(this.getIndexName());
+ OrFilterBuilder filter = FilterBuilders.orFilter();
+ for (Activity.Type type : query.getTypes()) {
+ filter.add(FilterBuilders.termFilter(ActivityNormalizer.LogFields.TYPE.field(), type));
+ }
+
// TODO implement query and filters based on LogQuery
- esSearch.setQuery(QueryBuilders.matchAllQuery());
+ esSearch.setQuery(QueryBuilders.filteredQuery(
+ QueryBuilders.matchAllQuery(), filter));
if (options.isScroll()) {
esSearch.setSearchType(SearchType.SCAN);
package org.sonar.server.activity;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.collect.Iterables;
import org.junit.After;
import org.junit.Before;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.activity.db.ActivityDao;
import org.sonar.server.activity.index.ActivityIndex;
+import org.sonar.server.activity.index.ActivityQuery;
import org.sonar.server.db.DbClient;
import org.sonar.server.search.QueryOptions;
import org.sonar.server.search.Result;
@Test
public void find_all() throws InterruptedException {
- final String testValue = "hello world";
+
service.write(dbSession, Activity.Type.ACTIVE_RULE, testValue);
dbSession.commit();
assertThat(index.findAll().getTotal()).isEqualTo(1);
@Test
public void search_message_log() throws InterruptedException {
- final String testValue = "hello world";
service.write(dbSession, Activity.Type.ACTIVE_RULE, testValue);
dbSession.commit();
assertThat(index.findAll().getTotal()).isEqualTo(1);
assertThat(result.getHits().get(0).message()).isEqualTo(testValue);
}
+
@Test
public void search_activity_log() throws InterruptedException {
- final String value = "world";
- final String key = "hello";
- ActivityLog activity = new ActivityLog() {
- @Override
- public Map<String, String> getDetails() {
- return ImmutableMap.of(key, value);
- }
- };
- service.write(dbSession, Activity.Type.ACTIVE_RULE, activity);
+
+ service.write(dbSession, Activity.Type.ACTIVE_RULE, getActivity());
dbSession.commit();
assertThat(index.findAll().getTotal()).isEqualTo(1);
Result<Activity> result = index.search(service.newActivityQuery(), new QueryOptions());
assertThat(result.getTotal()).isEqualTo(1L);
- assertThat(result.getHits().get(0).details().get(key)).isEqualTo(value);
+ assertThat(result.getHits().get(0).details().get(test_key)).isEqualTo(test_value);
+ }
+
+
+ @Test
+ public void search_by_type() {
+ service.write(dbSession, Activity.Type.NONE, getActivity());
+ service.write(dbSession, Activity.Type.SERVER, getActivity());
+ service.write(dbSession, Activity.Type.SERVER, testValue);
+ service.write(dbSession, Activity.Type.ACTIVE_RULE, getActivity());
+ dbSession.commit();
+
+ assertThat(service.search(new ActivityQuery(),
+ new QueryOptions()).getHits()).hasSize(4);
+
+ assertThat(service.search(new ActivityQuery()
+ .setTypes(ImmutableSet.of(Activity.Type.SERVER)),
+ new QueryOptions()).getHits()).hasSize(2);
+
+ assertThat(service.search(new ActivityQuery()
+ .setTypes(ImmutableSet.of(Activity.Type.ACTIVE_RULE)),
+ new QueryOptions()).getHits()).hasSize(1);
}
@Test
}
assertThat(count).isEqualTo(max);
}
+
+
+ final String test_key = "hello";
+ final String test_value = "world";
+ final String testValue = "hello world";
+
+ private ActivityLog getActivity() {
+ return new ActivityLog() {
+ @Override
+ public Map<String, String> getDetails() {
+ return ImmutableMap.of(test_key, test_value);
+ }
+ };
+ }
}