Reputation: 341
I'm looking for ways how to build query on Mongodb with this condition or parameters.
For example, input text is "rtab" and will result "brats","bart" and etc.
I don't know the term of this search method if there's any I hope you can help me.
Upvotes: 0
Views: 269
Reputation: 43884
The biggest problem you have here is the mispelling of your words.
Stemming the words will not fix it. Stemming will only convert "trees" to "tree" etc. There is no lingual library (reliable that is) that can solve this issue for you.
This is infact somethig of a black spot within search tech. Google uses a search index of words gathered from various sources in their applications (email etc) to form a dictionary to give you (hopefully) the right spelling in the search box in autocomplete mode, however they will not auto correcrt your words unless the search terms are much like a far more popular phrase.
There is no functionality in Mongo, either through $where
or MR or an array based set of words that allows this.
You could solve this by: the word abrt
is similar to bart
as such you house a list of words and query on another index, of sorts, for a list of similar words (say a geoNear type word command) to search for similar but not exact results. This basically replicates how other people do it, it is not an exact science but should work most of the times.
The main problem to solving this by sorting the word is that you have many hundreds of combinations of mispellings sometimes so it might not scale so nicely.
As to using Mongo for full text searching, this link is really helpful: http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo and describes in detail considerations you will need to make.
Upvotes: 0
Reputation: 69783
I don't think that this is possible with the conventional syntax of the MongoDB query language. The only way it could be done is with the $where argument. This argument takes a Javascript function which is executed for each document in the collection (as the "this" variable) and returns true or false depending on whether or not it should be returned by the query.
This feature is documented here: http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-%7B%7B%24where%7D%7DClausesandFunctionsinQueries
Note that $where is very, very slow. When performance is a requirement for your use-case, then I don't think that you can do this with MongoDB.
Security warning: When this is part of an application, you will have to procedurally generate Javascript code from user-provided strings. You have to be careful that his doesn't allow the user to inject any script code.
Upvotes: 0
Reputation: 62708
It's not inherently supported, but you could achieve this by building an index that takes the input words "brats" and "brats", sorts them so they become "abrst" and "abrs", and build a lookup index consisting of:
stem: "abrst", matches: ["brats"]
stem: "abrt", matches: ["brat", "bart"]
Then, when searching for "brat", sort the letters in it so you get "abrt", and perform your search with that:
db.lookups.find({stem: /^abrt/})
All documents returned should have words that start with your input word. This should work, though it is rather rudimentary. There are likely special-based search engines like Solr indexers that'll do this better.
Upvotes: 2