Paging
Tip: There are many examples of sorting in the FluentApiTests
source code to use as examples/reference.
Paging and Limiting results
To limit results we can use the QueryOptions
class when executing a search query. The QueryOptions
class provides the ability to skip and take.
Examples:
var searcher = indexer.GetSearcher();
var takeFirstTenInIndex = searcher
.CreateQuery()
.All()
.Execute(QueryOptions.SkipTake(0, 10))
var skipFiveAndTakeFirstTenInIndex = searcher
.CreateQuery()
.All()
.Execute(QueryOptions.SkipTake(5, 10))
var takeThreeResults = searcher
.CreateQuery("content")
.Field("writerName", "administrator")
.OrderBy(new SortableField("name", SortType.String))
.Execute(QueryOptions.SkipTake(0, 3));
var takeSevenHundredResults = searcher
.CreateQuery("content")
.Field("writerName", "administrator")
.OrderByDescending(new SortableField("name", SortType.String))
.Execute(QueryOptions.SkipTake(0, 700));
By default when using Execute()
or Execute(QueryOptions.SkipTake(0))
where no take parameter is provided the take of the search will be set to QueryOptions.DefaultMaxResults
(500).
Deep Paging
When using Lucene.NET as the Examine provider it is possible to more efficiently perform deep paging. Steps:
- Build and execute your query as normal.
- Cast the ISearchResults from IQueryExecutor.Execute to ILuceneSearchResults
- Store ILuceneSearchResults.SearchAfter (SearchAfterOptions) for the next page.
- Create the same query as the previous request.
- When calling IQueryExecutor.Execute. Pass in new LuceneQueryOptions(skip,take, SearchAfterOptions); Skip will be ignored, the next take documents will be retrieved after the SearchAfterOptions document.
- Repeat Steps 2-5 for each page.