/**
  * tophits 查询
  *
  * <p><a>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html</a>
  *
  * @throws IOException
  * @throws SqlParseException
  */
 @Test
 public void topHitTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   SqlElasticSearchRequestBuilder result =
       (SqlElasticSearchRequestBuilder)
           MainTestSuite.getSearchDao()
               .explain("select topHits('size'=3,age='desc') from bank  group by gender ")
               .explain();
   System.out.println(result);
 }
 /**
  * 时间范围聚合
  *
  * <p><a>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html</a>
  *
  * @throws IOException
  * @throws SqlParseException
  */
 @Test
 public void countDateRangeTest()
     throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   SqlElasticSearchRequestBuilder result =
       (SqlElasticSearchRequestBuilder)
           MainTestSuite.getSearchDao()
               .explain(
                   "select online from online  group by date_range(field='insert_time','format'='yyyy-MM-dd' ,'2014-08-18','2014-08-17','now-8d','now-7d','now-6d','now') ")
               .explain();
   System.out.println(result);
 }
 /**
  * 时间 聚合 , 每天按照天聚合 参数说明:
  *
  * <p><a>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html</a>
  *
  * @throws IOException
  * @throws SqlParseException
  */
 @Test
 public void countGroupByDateTest()
     throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   SqlElasticSearchRequestBuilder result =
       (SqlElasticSearchRequestBuilder)
           MainTestSuite.getSearchDao()
               .explain(
                   "select insert_time from online  group by date_histogram(field='insert_time','interval'='1.5h','format'='yyyy-MM') ")
               .explain();
   System.out.println(result);
 }
 @Test
 public void countGroupByDateTestWithAlias()
     throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   SqlElasticSearchRequestBuilder result =
       (SqlElasticSearchRequestBuilder)
           MainTestSuite.getSearchDao()
               .explain(
                   "select insert_time from online  group by date_histogram(field='insert_time','interval'='1.5h','format'='yyyy-MM','alias'='myAlias') ")
               .explain();
   boolean containAlias =
       result
           .toString()
           .replaceAll("\\s+", "")
           .contains(
               "myAlias\":{\"date_histogram\":{\"field\":\"insert_time\",\"interval\":\"1.5h\",\"format\":\"yyyy-MM\"}}");
   Assert.assertTrue(containAlias);
 }
 private SearchHits query(String query)
     throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException {
   SearchDao searchDao = MainTestSuite.getSearchDao();
   SearchRequestBuilder select = (SearchRequestBuilder) searchDao.explain(query);
   return select.get().getHits();
 }
 private SqlElasticSearchRequestBuilder getSearchRequestBuilder(String query)
     throws SqlParseException, SQLFeatureNotSupportedException {
   SearchDao searchDao = MainTestSuite.getSearchDao();
   return (SqlElasticSearchRequestBuilder) searchDao.explain(query).explain();
 }