Skip to content

Instantly share code, notes, and snippets.

@brianhempel
Forked from kernow/gist:745275
Created December 17, 2010 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianhempel/745312 to your computer and use it in GitHub Desktop.
Save brianhempel/745312 to your computer and use it in GitHub Desktop.
criteria.merge!( :_keywords => { :$in => words } )
search_result = collection.map_reduce(search_map(words), search_reduce, {:query => criteria, :finalize => search_finalize})
def search_reduce
"function( key , values ){return { model: values[0]};}"
end
def search_finalize
"function( key , values ){return values.model;}"
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