import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.index.query.AndFilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.OrFilterBuilder;
import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.index.query.RangeFilterBuilder;
import org.sonar.core.activity.Activity;
import org.sonar.core.activity.db.ActivityDto;
import org.sonar.core.activity.db.ActivityKey;
}
public Result<Activity> search(ActivityQuery query, QueryOptions options) {
+
+ // Prepare query
SearchRequestBuilder esSearch = getClient()
.prepareSearch(this.getIndexName())
.setTypes(this.getIndexType())
.setIndices(this.getIndexName());
- OrFilterBuilder filter = FilterBuilders.orFilter();
+ AndFilterBuilder filter = FilterBuilders.andFilter();
+
+ // implement Type Filtering
+ OrFilterBuilder typeFilter = FilterBuilders.orFilter();
for (Activity.Type type : query.getTypes()) {
- filter.add(FilterBuilders.termFilter(ActivityNormalizer.LogFields.TYPE.field(), type));
+ typeFilter.add(FilterBuilders.termFilter(ActivityNormalizer.LogFields.TYPE.field(), type));
}
+ filter.add(typeFilter);
+
+ // Implement date Filter
+ if (query.getSince() != null || query.getTo() != null) {
+ RangeFilterBuilder dateFilter = FilterBuilders.rangeFilter(ActivityNormalizer.LogFields.CREATED_AT.field());
+ if (query.getSince() != null) {
+ dateFilter.from(query.getSince());
+ }
+ if (query.getTo() != null) {
+ dateFilter.to(query.getTo());
+ }
+ filter.add(dateFilter);
+ }
+
- // TODO implement query and filters based on LogQuery
esSearch.setQuery(QueryBuilders.filteredQuery(
QueryBuilders.matchAllQuery(), filter));
import org.sonar.server.search.Result;
import org.sonar.server.tester.ServerTester;
+import java.util.Date;
import java.util.Iterator;
import java.util.Map;
@Test
- public void search_by_type() {
+ public void filter_by_type() {
service.write(dbSession, Activity.Type.NONE, getActivity());
service.write(dbSession, Activity.Type.SERVER, getActivity());
service.write(dbSession, Activity.Type.SERVER, testValue);
new QueryOptions()).getHits()).hasSize(1);
}
+ @Test
+ public void filter_by_date() throws InterruptedException {
+
+ Date t0 = new Date();
+ service.write(dbSession, Activity.Type.SERVER, testValue);
+ service.write(dbSession, Activity.Type.SERVER, testValue);
+ dbSession.commit();
+ Date t1 = new Date();
+ service.write(dbSession, Activity.Type.SERVER, testValue);
+ dbSession.commit();
+ Date t2 = new Date();
+
+ assertThat(service.search(new ActivityQuery()
+ .setSince(t0),
+ new QueryOptions()).getHits()).hasSize(3);
+
+ assertThat(service.search(new ActivityQuery()
+ .setSince(t1),
+ new QueryOptions()).getHits()).hasSize(1);
+
+ assertThat(service.search(new ActivityQuery()
+ .setSince(t2),
+ new QueryOptions()).getHits()).hasSize(0);
+
+ //FIXME bracket not working yet
+// assertThat(service.search(new ActivityQuery()
+// .setSince(t0)
+// .setTo(t1),
+// new QueryOptions()).getHits()).hasSize(2);
+ }
+
@Test
public void iterate_all() throws InterruptedException {
int max = QueryOptions.DEFAULT_LIMIT + 3;