DistinctAction extends Action<DistinctRequest> { @Override public ActionResponse execute(DistinctRequest request) { SearchRequestBuilder query; try { query = getConnection().getClient() .prepareSearch(ElasticsearchUtils.getIndices(request.getTable(), request)) .setIndicesOptions(Utils.indicesOptions()); query.setQuery(new ElasticSearchQueryGenerator().genFilter(request.getFilters())) .setSize(QUERY_SIZE) .addAggregation(Utils.buildTermsAggregation( request.getNesting(), Sets.newHashSet(), elasticsearchTuningConfig.getAggregationSize())); } catch (Exception e) { throw FoxtrotExceptions.queryCreationException(request, e); } try { SearchResponse response = query.execute() .actionGet(getGetQueryTimeout()); return getResponse(response, getParameter()); } catch (ElasticsearchException e) { throw FoxtrotExceptions.createQueryExecutionException(request, e); } } DistinctAction(DistinctRequest parameter, AnalyticsLoader analyticsLoader); @Override void preprocess(); @Override String getMetricKey(); @Override String getRequestCacheKey(); @Override void validateImpl(DistinctRequest parameter); @Override ActionResponse execute(DistinctRequest request); @Override SearchRequestBuilder getRequestBuilder(DistinctRequest request); @Override ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, DistinctRequest parameter);  }
@Test public void testDistinctAsc() throws FoxtrotException { 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(getQueryExecutor().execute(distinctRequest)); assertNotNull(distinctResponse); assertEquals(expectedResponse, distinctResponse); }
@Test public void testDistinctDesc() throws FoxtrotException { 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(getQueryExecutor().execute(distinctRequest)); assertNotNull(distinctResponse); assertEquals(expectedResponse, distinctResponse); }
@Test public void testDistinctMultipleNestingAscAsc() throws FoxtrotException, 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(getQueryExecutor().execute(distinctRequest)); assertNotNull(distinctResponse); }
@Test public void testDistinctMultipleNestingAscDesc() throws FoxtrotException, 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(getQueryExecutor().execute(distinctRequest)); assertNotNull(distinctResponse); }