-
-
Save brianhempel/745318 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
criteria.merge!( :_keywords => { :$in => words } ) | |
search_result = collection.map_reduce(search_map(words), search_reduce, :query => criteria) | |
def search_reduce | |
"function( key , values ){return values[0];}" | |
end | |
def search_map(words) | |
#convert words into Regex OR | |
q = words.map do |k| | |
Regexp.escape(k) | |
end.join("|") | |
"function(){" + | |
"this.relevance = this._keywords.filter(" + | |
"function(z){" + | |
"return String(z).match(/(#{q})/);" + | |
"}).length;" + | |
"emit(this._id, this);" + | |
"}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment