@Override
 public <T> List<String> queryForIds(SearchQuery query) {
   SearchRequestBuilder request = prepareSearch(query).setQuery(query.getQuery()).setNoFields();
   if (query.getFilter() != null) {
     request.setPostFilter(query.getFilter());
   }
   SearchResponse response = request.execute().actionGet();
   return extractIds(response);
 }
  @Override
  public <T> long count(SearchQuery searchQuery, Class<T> clazz) {
    String indexName[] =
        isNotEmpty(searchQuery.getIndices())
            ? searchQuery.getIndices().toArray(new String[searchQuery.getIndices().size()])
            : retrieveIndexNameFromPersistentEntity(clazz);
    String types[] =
        isNotEmpty(searchQuery.getTypes())
            ? searchQuery.getTypes().toArray(new String[searchQuery.getTypes().size()])
            : retrieveTypeFromPersistentEntity(clazz);

    Assert.notNull(indexName, "No index defined for Query");

    CountRequestBuilder countRequestBuilder = client.prepareCount(indexName);

    if (types != null) {
      countRequestBuilder.setTypes(types);
    }
    if (searchQuery.getQuery() != null) {
      countRequestBuilder.setQuery(searchQuery.getQuery());
    }
    return countRequestBuilder.execute().actionGet().getCount();
  }
  private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery searchQuery) {
    if (searchQuery.getFilter() != null) {
      searchRequest.setPostFilter(searchQuery.getFilter());
    }

    if (CollectionUtils.isNotEmpty(searchQuery.getElasticsearchSorts())) {
      for (SortBuilder sort : searchQuery.getElasticsearchSorts()) {
        searchRequest.addSort(sort);
      }
    }

    if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
      for (FacetRequest facetRequest : searchQuery.getFacets()) {
        FacetBuilder facet = facetRequest.getFacet();
        if (facetRequest.applyQueryFilter() && searchQuery.getFilter() != null) {
          facet.facetFilter(searchQuery.getFilter());
        }
        searchRequest.addFacet(facet);
      }
    }

    if (searchQuery.getHighlightFields() != null) {
      for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
        searchRequest.addHighlightedField(highlightField);
      }
    }

    if (CollectionUtils.isNotEmpty(searchQuery.getAggregations())) {
      for (AbstractAggregationBuilder aggregationBuilder : searchQuery.getAggregations()) {
        searchRequest.addAggregation(aggregationBuilder);
      }
    }

    return searchRequest.setQuery(searchQuery.getQuery()).execute().actionGet();
  }
  @Override
  public String scan(SearchQuery searchQuery, long scrollTimeInMillis, boolean noFields) {
    Assert.notNull(searchQuery.getIndices(), "No index defined for Query");
    Assert.notNull(searchQuery.getTypes(), "No type define for Query");
    Assert.notNull(searchQuery.getPageable(), "Query.pageable is required for scan & scroll");

    SearchRequestBuilder requestBuilder =
        client
            .prepareSearch(toArray(searchQuery.getIndices()))
            .setSearchType(SCAN)
            .setQuery(searchQuery.getQuery())
            .setTypes(toArray(searchQuery.getTypes()))
            .setScroll(TimeValue.timeValueMillis(scrollTimeInMillis))
            .setFrom(0)
            .setSize(searchQuery.getPageable().getPageSize());

    if (searchQuery.getFilter() != null) {
      requestBuilder.setPostFilter(searchQuery.getFilter());
    }

    if (isNotEmpty(searchQuery.getFields())) {
      requestBuilder.addFields(toArray(searchQuery.getFields()));
    }

    if (noFields) {
      requestBuilder.setNoFields();
    }
    return requestBuilder.execute().actionGet().getScrollId();
  }
 @Override
 public <T> FacetedPage<T> queryForPage(
     SearchQuery query, Class<T> clazz, SearchResultMapper mapper) {
   SearchResponse response = doSearch(prepareSearch(query, clazz), query);
   return mapper.mapResults(response, clazz, query.getPageable());
 }