CountAction extends Action<CountRequest> { @Override public ActionResponse execute(CountRequest parameter) throws QueryStoreException { parameter.setTable(ElasticsearchUtils.getValidTableName(parameter.getTable())); if (null == parameter.getFilters() || parameter.getFilters().isEmpty()) { parameter.setFilters(Lists.<Filter>newArrayList(new AnyFilter(parameter.getTable()))); } if (parameter.getField() != null) { parameter.getFilters().add(new ExistsFilter(parameter.getField())); } try { if (parameter.isDistinct()){ SearchRequestBuilder query = getConnection().getClient() .prepareSearch(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .setSearchType(SearchType.COUNT) .setQuery(new ElasticSearchQueryGenerator(FilterCombinerType.and) .genFilter(parameter.getFilters())) .addAggregation(AggregationBuilders .cardinality(Utils.sanitizeFieldForAggregation(parameter.getField())) .field(parameter.getField()) ); SearchResponse response = query.execute().actionGet(); Aggregations aggregations = response.getAggregations(); Cardinality cardinality = aggregations.get(Utils.sanitizeFieldForAggregation(parameter.getField())); if (cardinality == null){ return new CountResponse(0); } else { return new CountResponse(cardinality.getValue()); } } else { CountRequestBuilder countRequestBuilder = getConnection().getClient() .prepareCount(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .setQuery(new ElasticSearchQueryGenerator(FilterCombinerType.and).genFilter(parameter.getFilters())); org.elasticsearch.action.count.CountResponse countResponse = countRequestBuilder.execute().actionGet(); return new CountResponse(countResponse.getCount()); } } catch (Exception e) { logger.error("Error running count: ", e); throw new QueryStoreException(QueryStoreException.ErrorCode.QUERY_EXECUTION_ERROR, "Error running count query.", e); } } CountAction(CountRequest parameter,
                       TableMetadataManager tableMetadataManager,
                       DataStore dataStore,
                       QueryStore queryStore,
                       ElasticsearchConnection connection,
                       String cacheToken); @Override ActionResponse execute(CountRequest parameter);  }
@Test public void testCount() throws QueryStoreException { CountRequest countRequest = new CountRequest(); countRequest.setTable(TestUtils.TEST_TABLE_NAME); countRequest.setField("os"); countRequest.setDistinct(false); CountResponse countResponse = CountResponse.class.cast(queryExecutor.execute(countRequest)); assertNotNull(countResponse); assertEquals(11, countResponse.getCount()); }
@Test public void testCountWithFilter() throws QueryStoreException { CountRequest countRequest = new CountRequest(); countRequest.setTable(TestUtils.TEST_TABLE_NAME); countRequest.setField("os"); ArrayList<Filter> filters = new ArrayList<Filter>(); filters.add(new EqualsFilter("os", "android")); countRequest.setFilters(filters); countRequest.setDistinct(false); CountResponse countResponse = CountResponse.class.cast(queryExecutor.execute(countRequest)); assertNotNull(countResponse); assertEquals(7, countResponse.getCount()); }
@Test public void testCountDistinct() throws QueryStoreException { CountRequest countRequest = new CountRequest(); countRequest.setTable(TestUtils.TEST_TABLE_NAME); countRequest.setField("os"); countRequest.setDistinct(true); CountResponse countResponse = CountResponse.class.cast(queryExecutor.execute(countRequest)); assertNotNull(countResponse); assertEquals(2, countResponse.getCount()); }
@Test public void testCountDistinctWithFilter() throws QueryStoreException { CountRequest countRequest = new CountRequest(); countRequest.setTable(TestUtils.TEST_TABLE_NAME); countRequest.setField("os"); ArrayList<Filter> filters = new ArrayList<Filter>(); filters.add(new EqualsFilter("device", "nexus")); countRequest.setFilters(filters); countRequest.setDistinct(true); CountResponse countResponse = CountResponse.class.cast(queryExecutor.execute(countRequest)); assertNotNull(countResponse); assertEquals(2, countResponse.getCount()); }
@Test public void testCountDistinctWithFilterOnSameField() throws QueryStoreException { CountRequest countRequest = new CountRequest(); countRequest.setTable(TestUtils.TEST_TABLE_NAME); countRequest.setField("os"); ArrayList<Filter> filters = new ArrayList<Filter>(); filters.add(new EqualsFilter("os", "android")); countRequest.setFilters(filters); countRequest.setDistinct(true); CountResponse countResponse = CountResponse.class.cast(queryExecutor.execute(countRequest)); assertNotNull(countResponse); assertEquals(1, countResponse.getCount()); }