@Test public void orderByDescTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { SearchHits response = query(String.format("SELECT age FROM %s/account ORDER BY age DESC LIMIT 1000", TEST_INDEX)); SearchHit[] hits = response.getHits(); ArrayList<Integer> ages = new ArrayList<Integer>(); for (SearchHit hit : hits) { ages.add((int) hit.getSource().get("age")); } ArrayList<Integer> sortedAges = (ArrayList<Integer>) ages.clone(); Collections.sort(sortedAges, Collections.reverseOrder()); Assert.assertTrue("The list is not ordered descending", sortedAges.equals(ages)); }
@Test public void orderByAscFieldWithSpaceTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { SearchHits response = query( String.format( "SELECT * FROM %s/phrase_2 ORDER BY `test field` ASC LIMIT 1000", TEST_INDEX)); SearchHit[] hits = response.getHits(); ArrayList<Integer> testFields = new ArrayList<Integer>(); for (SearchHit hit : hits) { testFields.add((int) hit.getSource().get("test field")); } ArrayList<Integer> sortedTestFields = (ArrayList<Integer>) testFields.clone(); Collections.sort(sortedTestFields); Assert.assertTrue("The list is not ordered ascending", sortedTestFields.equals(testFields)); }
@Test public void orderByDescTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { ArrayList<Long> agesCount = new ArrayList<>(); Aggregations result = query( String.format( "SELECT COUNT(*) FROM %s/account GROUP BY age ORDER BY COUNT(*) DESC", TEST_INDEX)); Terms age = result.get("age"); for (Terms.Bucket bucket : age.getBuckets()) { agesCount.add(((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue()); } ArrayList<Long> sortedAgesCount = (ArrayList<Long>) agesCount.clone(); Collections.sort(sortedAgesCount, Collections.reverseOrder()); Assert.assertTrue("The list is not ordered descending", agesCount.equals(agesCount)); }