On the other hand, the time to retrieve one page goes up tremendously for a smaller number of results. Obviously, this is the time required to scan the entire collection document by document as no index can be used here. The time for counting the number of matches among the 100,000 entries is pretty consistent at about 200ms. Let’s explain the results for searching with regular expressions first. The regular expression search takes longer for queries with just a few results while the full text search gets faster and is clearly superior in those cases. The following bar chart illustrates the corresponding performances for counting the results and retrieving them: – “Mayzie” which delivers 2 matches in both cases – “newspaper” which delivers 318 matches with the full text search and 320 with regular expressions – “movie” which delivers 3,533 matches with the full text search and 3,317 with regular expressions (the number differs due to the full text search’s stemming functionality) The time results in ms are printed to the console.įor our example we imported 100,000 movies and searched for three different words, always retrieving the first page with up to 15 entries but counting the number of all matches (for calculating the number of required pages): Entering a search term in the search field, one can decide to run it with or without the MongoDB Full Text Search. To get a feeling how fast the MongoDB Full Text Search works in different cases, we built a small demo application which imports data from The Movie Database and displays them in a list. How does the MongoDB Full Text Search perform compared to regular expressions? Despite of projecting to nothing but the “_id” field a huge set of matches will not be returned in its entirety if the result exceeds Mongo’s 16MB per document limit. The full text search does not work properly for really large datasets as all matches are returned as a single document and the command does not support a “skip” parameter to retrieve results page-by-page. What are limitations of the MongoDB Full Text Search? This two-step approach ensures we can use all the functionality of a regular MongoDB query (sorting, pagination, additional criteria, …) while taking advantage of MongoDB’s current full text search implementation. In order to provide search results with pagination and custom sorting for the application’s UI layer, we need another standard Spring Data query that does exactly that. Or from the Java application github:ab8302b2ca76b85a515e Note that there is no indicator for the field to search in! As MongoDB supports only one text index per collection this information is implicitly specified after defining it in the shell github:a9234cf1ed987cbbb686 Translating this into Java code that extracts the IDs of all matches works as follows. The command returns a single json document with all objects that match the query and some statistics on the search that has just been executed. For implementing our own solution, we have to understand how a full text search can be executed in the Mongo shell: github:b7b4a8487c0291176309 Unfortunately, this is a little harder as Spring Data does not yet support the feature. Nevertheless, searching via regular expressions is really easy to implement using Spring Data as demonstrated by the following code snippet: github:7bf5b7002f2a491d13bb How to use the MongoDB Full Text Search? Second, they cannot use traditional indexes which makes queries in large datasets really slow. First, regular expressions have their natural limitations because they lack any stemming functionality and cannot handle convenient search queries such as “action -superhero” in a trivial way. Why not continue to use regular expressions?īasically, there are two major reasons. However, up to now it is in beta state and not recommended to use in production systems. MongoDB Full Text Search is a new feature in MongoDB 2.4. We will provide code snippets explaining how to use both features in a Java application as well as an empirical performance evaluation. Today we would like to introduce you to the new MongoDB Full Text Search and compare its capabilities and performance with simple regular expressions, which are currently state of the art for searching in MongoDB.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |