# Pagination in elasticsearch

**URL:** <https://discuss.elastic.co/t/pagination-in-elasticsearch/19971>\
**Category:** Elasticsearch\
**Created:** [September 25, 2014, 10:56pm UTC](https://discuss.elastic.co/t/pagination-in-elasticsearch/19971 "2014-09-25T22:56:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malini](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Malini](https://discuss.elastic.co/u/Malini)\
**Post date:** [September 25, 2014, 10:56pm UTC](https://discuss.elastic.co/t/pagination-in-elasticsearch/19971/1 "2014-09-25T22:56:44Z")

</div>

I have

SearchRequestBuilder srb = client.prepareSearch("cs").setTypes("csdl");  
srb.setSearchType(SearchType.DFS\_QUERY\_AND\_FETCH);

QueryBuilder qb = (QueryBuilder) QueryBuilders.matchQuery("title",  
searchText);

FilterBuilder fb = FilterBuilders.andFilter (  
FilterBuilders.termsFilter("elasticdb", searchDB),  
//get from date and to date

FilterBuilders.rangeFilter("pubdate").gte("1890-09").lte("2014-08")  
);

FilteredQueryBuilder builder = QueryBuilders.filteredQuery(qb,fb);

FunctionScoreQueryBuilder functionbuilder = new  
FunctionScoreQueryBuilder(builder)  
.add(FilterBuilders.termsFilter("category",  
"acm"), factorFunction(-30.0f));

srb.setQuery(functionbuilder).setFrom(0).setSize(1);

SearchResponse response = srb.execute().actionGet();  
SearchHit[] results = response.getHits().getHits();

Even though I set from=0 and size = 1 ( to see only one result) I see more  
than 1 results.

How do we get this pagination working?

Thanks in advance

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/847b621d-4638-4096-8159-54ca97cf03d7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/847b621d-4638-4096-8159-54ca97cf03d7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [October 1, 2014, 4:03pm UTC](https://discuss.elastic.co/t/pagination-in-elasticsearch/19971/2 "2014-10-01T16:03:32Z")

</div>

The setSize method specifies the maximum number of responses per shard.

So if you query an index with _N_ shards and your query sets a size of _S_ then  
the query will return a maximum of up to _N_ x _S_ response hits.

Since you writing this in Java, it's a relatively easy matter to make  
further adjustments on the response, limiting the response to the page size  
you expect.

Brian

On Thursday, September 25, 2014 6:56:44 PM UTC-4, Malini wrote:

> I have
> 
> SearchRequestBuilder srb = client.prepareSearch("cs").setTypes("csdl");  
> srb.setSearchType(SearchType.DFS\_QUERY\_AND\_FETCH);
> 
> QueryBuilder qb = (QueryBuilder) QueryBuilders.matchQuery("title",  
> searchText);
> 
> FilterBuilder fb = FilterBuilders.andFilter (  
> FilterBuilders.termsFilter("elasticdb", searchDB),  
> //get from date and to date
> 
> FilterBuilders.rangeFilter("pubdate").gte("1890-09").lte("2014-08")  
> );
> 
> FilteredQueryBuilder builder = QueryBuilders.filteredQuery(qb,fb);
> 
> FunctionScoreQueryBuilder functionbuilder = new  
> FunctionScoreQueryBuilder(builder)
> 
> .add(FilterBuilders.termsFilter("category", "acm"), factorFunction(-30.0f));
> 
> srb.setQuery(functionbuilder).setFrom(0).setSize(1);
> 
> SearchResponse response = srb.execute().actionGet();  
> SearchHit results = response.getHits().getHits();
> 
> Even though I set from=0 and size = 1 ( to see only one result) I see more  
> than 1 results.
> 
> How do we get this pagination working?
> 
> Thanks in advance

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/00c324bd-fad1-404c-8b28-4274ffe11c24%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/00c324bd-fad1-404c-8b28-4274ffe11c24%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:58am UTC](https://discuss.elastic.co/t/pagination-in-elasticsearch/19971/3 "2017-07-06T00:58:55Z")

</div>


