I am setting up a mongoDB db to allow (simple) keyword searching using multikeys as recommended here. A record looks similar too:
{ title: { title: "A river runs through", _keywords: ["a","river","runs","through"] ) , ... }
I using nodejs server side, so am using javascript. The following query will match (this was run in the mongo terminal):
> db.torrents_sorted.find({'title._keywords' : {"$all" : ["river","the"]} }).count()
210
However, these do not:
> db.torrents_sorted.find({'title._keywords' : {"$all" : ["/river/i","/the/i"]} }).count()
0
> db.torrents_sorted.find({'title._keywords' : {"$all" : [{ "$regex" : "river", "$options" : "i" },{ "$regex" : "the", "$options" : "i" }]} }).count()
0
Using a single regex (without using $and or $all) does match:
db.torrents_sorted.find({'title._keywords' : { "$regex" : "river", "$options" : "i" } }).count() 1461
Interestingly, using python and pymongo to compile the regular expressions does work:
>>> db.torrents_sorted.find({'title._keywords': { '$all': [re.compile('river'), re.compile('the')]}}).count();
236
I am not necessarily looking for a solution that uses regexes, however it is required that keywords are matched on shorter strings so "riv" matches "river", which seems ideal for regexes (or LIKE in sql).
My next idea is to try passing in a javascript function that performs the regex matching on the list, or perhaps passing in a seperate function for each regex (this does seem to scream hack at me :), although I'm guessing this would be slower and performance is very important.
所有评论(0)