DistinctAction extends Action<DistinctRequest> { @Override public ActionResponse execute(DistinctRequest request) throws QueryStoreException { request.setTable(ElasticsearchUtils.getValidTableName(request.getTable())); if (null == request.getFilters()) { request.setFilters(Lists.<Filter>newArrayList(new AnyFilter(request.getTable()))); } if (request.getTable() == null) { throw new QueryStoreException(QueryStoreException.ErrorCode.INVALID_REQUEST, "Invalid Table"); } try { SearchRequestBuilder query = getConnection().getClient().prepareSearch(ElasticsearchUtils.getIndices( request.getTable(), request)); TermsBuilder rootBuilder = null; TermsBuilder termsBuilder = null; for (ResultSort nestedField : request.getNesting()) { if (nestedField.getField() == null || nestedField.getField().trim().isEmpty()) { throw new QueryStoreException(QueryStoreException.ErrorCode.INVALID_REQUEST, "Illegal Nesting Parameters"); } String aggregationKey = Utils.sanitizeFieldForAggregation(nestedField.getField()); Terms.Order order = (nestedField.getOrder() == ResultSort.Order.desc) ? Terms.Order.term(false) : Terms.Order.term(true); if (null == termsBuilder) { termsBuilder = AggregationBuilders.terms(aggregationKey).field(nestedField.getField()).order(order); } else { TermsBuilder tempBuilder = AggregationBuilders.terms(aggregationKey).field(nestedField.getField()).order(order); termsBuilder.subAggregation(tempBuilder); termsBuilder = tempBuilder; } termsBuilder.size(0); if (null == rootBuilder) { rootBuilder = termsBuilder; } } query.setQuery(new ElasticSearchQueryGenerator(FilterCombinerType.and) .genFilter(request.getFilters())) .setSearchType(SearchType.COUNT) .addAggregation(rootBuilder); SearchResponse response = query.execute().actionGet(); Aggregations aggregations = response.getAggregations(); if (aggregations == null) { logger.error("Null response for Group. Request : " + request.toString()); return new DistinctResponse(new ArrayList<String>(), new ArrayList<List<String>>()); } return getDistinctResponse(request, aggregations); } catch (QueryStoreException ex) { throw ex; } catch (Exception e) { logger.error("Error running grouping: ", e); throw new QueryStoreException(QueryStoreException.ErrorCode.QUERY_EXECUTION_ERROR, "Error running group query.", e); } } DistinctAction(DistinctRequest request,
                          TableMetadataManager tableMetadataManager,
                          DataStore dataStore,
                          QueryStore queryStore,
                          ElasticsearchConnection connection,
                          String cacheToken); @Override ActionResponse execute(DistinctRequest request);  }
@Test public void testDistinctAsc() throws QueryStoreException { DistinctRequest distinctRequest = new DistinctRequest(); distinctRequest.setTable(TestUtils.TEST_TABLE_NAME); ResultSort resultSort = new ResultSort(); resultSort.setField("version"); resultSort.setOrder(ResultSort.Order.asc); distinctRequest.setNesting(Arrays.asList(resultSort)); DistinctResponse expectedResponse = new DistinctResponse(); expectedResponse.setHeaders(Arrays.asList("version")); List<List<String>> listResponse = new ArrayList<List<String>>(); listResponse.add(Arrays.asList("1")); listResponse.add(Arrays.asList("2")); listResponse.add(Arrays.asList("3")); expectedResponse.setResult(listResponse); DistinctResponse distinctResponse = DistinctResponse.class.cast(queryExecutor.execute(distinctRequest)); assertNotNull(distinctResponse); assertEquals(expectedResponse, distinctResponse); }
@Test public void testDistinctDesc() throws QueryStoreException { DistinctRequest distinctRequest = new DistinctRequest(); distinctRequest.setTable(TestUtils.TEST_TABLE_NAME); ResultSort resultSort = new ResultSort(); resultSort.setField("version"); resultSort.setOrder(ResultSort.Order.desc); distinctRequest.setNesting(Arrays.asList(resultSort)); DistinctResponse expectedResponse = new DistinctResponse(); expectedResponse.setHeaders(Arrays.asList("version")); List<List<String>> listResponse = new ArrayList<List<String>>(); listResponse.add(Arrays.asList("3")); listResponse.add(Arrays.asList("2")); listResponse.add(Arrays.asList("1")); expectedResponse.setResult(listResponse); DistinctResponse distinctResponse = DistinctResponse.class.cast(queryExecutor.execute(distinctRequest)); assertNotNull(distinctResponse); assertEquals(expectedResponse, distinctResponse); }
@Test public void testDistinctMultipleNestingAscAsc() throws QueryStoreException, JsonProcessingException { DistinctRequest distinctRequest = new DistinctRequest(); distinctRequest.setTable(TestUtils.TEST_TABLE_NAME); List<ResultSort> resultSorts = new ArrayList<ResultSort>(); ResultSort resultSort = new ResultSort(); resultSort.setField("version"); resultSort.setOrder(ResultSort.Order.asc); resultSorts.add(resultSort); resultSort = new ResultSort(); resultSort.setField("os"); resultSort.setOrder(ResultSort.Order.asc); resultSorts.add(resultSort); distinctRequest.setNesting(resultSorts); DistinctResponse expectedResponse = new DistinctResponse(); expectedResponse.setHeaders(Arrays.asList("version", "os")); List<List<String>> listResponse = new ArrayList<List<String>>(); listResponse.add(Arrays.asList("1", "android")); listResponse.add(Arrays.asList("1", "ios")); listResponse.add(Arrays.asList("2", "ios")); listResponse.add(Arrays.asList("3", "android")); expectedResponse.setResult(listResponse); DistinctResponse distinctResponse = DistinctResponse.class.cast(queryExecutor.execute(distinctRequest)); assertNotNull(distinctResponse); }
@Test public void testDistinctMultipleNestingAscDesc() throws QueryStoreException, JsonProcessingException { DistinctRequest distinctRequest = new DistinctRequest(); distinctRequest.setTable(TestUtils.TEST_TABLE_NAME); List<ResultSort> resultSorts = new ArrayList<ResultSort>(); ResultSort resultSort = new ResultSort(); resultSort.setField("version"); resultSort.setOrder(ResultSort.Order.asc); resultSorts.add(resultSort); resultSort = new ResultSort(); resultSort.setField("os"); resultSort.setOrder(ResultSort.Order.desc); resultSorts.add(resultSort); distinctRequest.setNesting(resultSorts); DistinctResponse expectedResponse = new DistinctResponse(); expectedResponse.setHeaders(Arrays.asList("version", "os")); List<List<String>> listResponse = new ArrayList<List<String>>(); listResponse.add(Arrays.asList("1", "ios")); listResponse.add(Arrays.asList("1", "android")); listResponse.add(Arrays.asList("2", "ios")); listResponse.add(Arrays.asList("3", "android")); expectedResponse.setResult(listResponse); DistinctResponse distinctResponse = DistinctResponse.class.cast(queryExecutor.execute(distinctRequest)); assertNotNull(distinctResponse); }