How to implement full-text search of MonoDB pages?

Now that my search query returns more than 100 documents, how do I paginate all returned documents? Is there a way to implement it using mongoDB or do I need to get all the results in the server memory and implement pagination (which does not seem reasonable). Note that CommandResult is not returned by DBCursor!

DBObject searchCommand = new BasicDBObject(); searchCommand.put("text", collectionName); searchCommand.put("search", searchQuery); CommandResult commandResult = db.command(searchCommand); 

Note. I am using Java.

+6
source share
2 answers

The text search command does not have the skip option, as in MongoDB 2.4, so any page numbering should be implemented in your application code.

If you consider text search behavior (which should return results ranked based on relevance), the skip parameter will still need to cache or calculate the initial results in order to skip.

Efficiency

Regarding efficient pagination in your application, a few suggestions:

  • cache initial search results and page size subsection for your rendering application
  • use a client-side plugin that presents the results of a single query in a beautifully paginated (for example, using the jQuery DataTables plugin)

The number of results returned by the limit

By default, limit for text search returns no more than 100 results. You can increase the limit, but keep in mind that the overall resulting document should still match the maximum BSON document size supported by your MongoDB server (16 MB, as in MongoDB 2.4). It’s also worth considering that most users have limited patience when searching for results pages, so if you have several hundred results, it’s better to suggest refining your search criteria.

Other options

If you outgrew the current limitations of MongoDB 2.4 text search (which, by the way, is still considered "experimental"), you can always upgrade to a more fully functional search product, such as ElasticSearch or Apache Lucene . There are ways to transfer MongoDB data updates to external search products, for example, using the ElasticSearch River plugin or Mongo Connector .

+7
source

For MongoDB v2.6 +

Spring.IO MongoDB driver recently added TextCriteria, see $ text $ search your documents with Spring Data MongoDB

 TextCriteria criteria = TextCriteria.forDefaultLanguage() .matchingAny("coffee", "cake"); Query query = TextQuery.queryText(criteria) .sortByScore() .with(new PageRequest(0, 5)); List<CookingRecipe> recipes = template.find(query, CookingRecipe); 

If you use the Grails MongoDB GORM plugin , starting with version 3.0.2, TextCriteria is not available in this API.

Instead, you can use the mongodb api to execute the request and paginate:

 // groovy/grails code... java code is very similar BasicDBObject textSearch = new BasicDBObject('$text', new BasicDBObject('$search', 'cookie')) BasicDBObject projection = new BasicDBObject('score', new BasicDBObject('$meta', 'textScore')) BasicDBObject sort = new BasicDBObject('score', new BasicDBObject('$meta', 'textScore')) DBCursor cursor = CookingRecipe.getCollection().find(textSearch, projection).sort(sort).skip(10).limit(5) 
+3
source

All Articles