title: How to send search request to elastic by ElasticsearchRestTemplate with terminate_after param?
body: <p>I do request to elastic cluster for Document entity using ElasticsearchRestTemplate. My DAO class:</p>
  <pre><code>import lombok.RequiredArgsConstructor;
  import org.jetbrains.annotations.NotNull;
  import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
  import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
  import org.springframework.data.elasticsearch.core.query.SearchQuery;
  import org.springframework.stereotype.Component;
  import APP_PACKAGE.dto.request.DocElasticFilterDto;
  import APP_PACKAGE.dto.request.DocElasticPaginationDto;
  import APP_PACKAGE.model.entity.Document;
  
  @Component
  @RequiredArgsConstructor
  public class DocumentDao {
      private final ElasticsearchRestTemplate esTemplate;
  
      @NotNull
      public AggregatedPage search(@NotNull DocElasticFilterDto filter, @NotNull DocElasticPaginationDto pagination) {
          SearchQuery query = DocumentQueryBuilderKt.buildSearchQuery(filter, pagination);
          return esTemplate.queryForPage(query, Document.class);
      }
  }
  </code></pre>
  <p>ElasticsearchRestTemplate send to elastic this http request:</p>
  <pre><code>curl -iX POST 'http://HOST:9200/INDEX/_doc/_search?rest_total_hits_as_int=true&amp;typed_keys=true&amp;ignore_unavailable=false&amp;expand_wildcards=open&amp;allow_no_indices=true&amp;ignore_throttled=true&amp;search_type=dfs_query_then_fetch&amp;batched_reduce_size=512' -d '{&quot;from&quot;:0,&quot;size&quot;:1,&quot;query&quot;:{&quot;bool&quot;:{&quot;filter&quot;:[{&quot;terms&quot;:{&quot;pgs&quot;:[2],&quot;boost&quot;:1.0}},{&quot;terms&quot;:{&quot;t&quot;:[2],&quot;boost&quot;:1.0}}],&quot;adjust_pure_negative&quot;:true,&quot;boost&quot;:1.0}},&quot;version&quot;:true,&quot;sort&quot;:[{&quot;t&quot;:{&quot;order&quot;:&quot;desc&quot;}}]}'
  </code></pre>
  <p>I want send request with <a href="http://terminate_after" rel="nofollow noreferrer">terminate_after</a> (compute for each search like <code>from</code> + <code>size</code> (params from pagination DTO)) param like this:</p>
  <pre><code>curl -iX POST 'http://HOST:9200/INDEX/_doc/_search?rest_total_hits_as_int=true&amp;typed_keys=true&amp;ignore_unavailable=false&amp;expand_wildcards=open&amp;allow_no_indices=true&amp;ignore_throttled=true&amp;search_type=dfs_query_then_fetch&amp;batched_reduce_size=512&amp;terminate_after=1' -d '{&quot;from&quot;:0,&quot;size&quot;:1,&quot;query&quot;:{&quot;bool&quot;:{&quot;filter&quot;:[{&quot;terms&quot;:{&quot;pgs&quot;:[2],&quot;boost&quot;:1.0}},{&quot;terms&quot;:{&quot;t&quot;:[2],&quot;boost&quot;:1.0}}],&quot;adjust_pure_negative&quot;:true,&quot;boost&quot;:1.0}},&quot;version&quot;:true,&quot;sort&quot;:[{&quot;t&quot;:{&quot;order&quot;:&quot;desc&quot;}}]}'
  </code></pre>
  <p>How can i do this with spring-data-elasticsearch?</p>
  <hr />
  <p>I found possible solution - using RestHighLevelClient without spring data library</p>
  <pre><code>    fun search(filter: DocElasticFilterDto, pagination: DocElasticPaginationDto): DocElasticSearchResult&lt;DocElasticDto&gt; {
          val query = buildSearchQuery(filter)
          log.debug(&quot;#search: query: $query&quot;)
          val searchSourceBuilder = buildSourceBuilder(query)
          searchSourceBuilder.size(pagination.limit)
          val from = pagination.page * pagination.limit
          searchSourceBuilder.from(from)
          searchSourceBuilder.terminateAfter(from + pagination.limit)
  
          val response = esClient.search(buildRequest(searchSourceBuilder), RequestOptions.DEFAULT)
  
          return DocElasticSearchResult(getResults(response), response.hits.totalHits!!.value)
      }
  
      private fun buildRequest(searchSourceBuilder: SearchSourceBuilder) :SearchRequest {
          val searchRequest = SearchRequest(indexName)
          searchRequest.source(searchSourceBuilder)
          return searchRequest
      }
  
      private fun buildSourceBuilder(queryBuilder: QueryBuilder) :SearchSourceBuilder {
          val searchSourceBuilder = SearchSourceBuilder()
          searchSourceBuilder.query(queryBuilder)
          searchSourceBuilder.timeout(TimeValue.parseTimeValue(readTimeout, &quot;timeout&quot;))
          return searchSourceBuilder
      }
  </code></pre>