How to implement MongoDB full text search pagination?
java, mongodb
Solution
The `text` search command does not have a `skip` option as at MongoDB 2.4, so any pagination will have to be implemented in your application code.
If you consider the behaviour of text search (which is to return results ranked based on relevance), a `skip` option would still have to cache or calculate the initial results to skip.
Efficiency
As far as efficient pagination in your application, a few suggestions would be:
- cache the results of the initial search and slice page-sized subsets for your application to render
- use a client-side plugin which presents the results from a single query in a nicely paginated view (for example using the jQuery DataTables plugin)
Number of results returned by limit
The default `limit` for text search is to return a maximum of 100 results. You can increase the limit, but keep in mind that the overall result document must still fit within the maximum BSON document size supported by your MongoDB server (16Mb, as at MongoDB 2.4). It's also worth considering that most users have a finite patience in searching through pages of results, so if you have a few hundred results it may better to suggest refining the search criteria.
Other options
If you have outgrown the current limitations of MongoDB 2.4's text search (which, incidentally, is still considered "experimental") you can always upgrade to a more full featured search product such as ElasticSearch or Apache Lucene. There are ways to feed your MongoDB data updates into external search products such as using an ElasticSearch River plugin or the Mongo Connector.
Problem
Now that my search query returns more than 100 documents, How do I implement pagination for all returned documents ? Is there A way to implement it withing mongoDB or I have to fetch all results in the server memory and implement pagination ( which does not seems reasonable ). Note that CommandResult is returned not a DBCursor ! ``` DBObject searchCommand = new BasicDBObject(); searchCommand.put("text", collectionName); searchCommand.put("search", searchQuery); CommandResult commandResult = db.command(searchCommand); ``` Note: I am using Java.