Permalink
Browse files

performance optimizations on relevance_for

  • Loading branch information...
1 parent 635f20f commit 2fe72c1d46113d5b09f3cf91de84a6e3f9dda0f1 Tomas Jogin committed Nov 9, 2011
Showing with 16 additions and 8 deletions.
  1. +1 −1 Gemfile.lock
  2. +14 −6 lib/naive-search/naive_search_on.rb
  3. +1 −1 lib/naive-search/version.rb
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- naive-search (0.1.3)
+ naive-search (0.1.4)
rails (~> 3.1.1)
GEM
@@ -37,16 +37,24 @@ def search_for(query, page_no = 1, page_size = nil)
end
def relevance_for(query)
+ query = query.downcase
@naive_relevance ||= {}
return @naive_relevance[query] if @naive_relevance[query]
-
words = query.split " "
+
score = self.naive_search_fields.map do |field|
- partial_word_matches = words.map{|w| self.send(field).to_s.downcase.scan(w.downcase).size}.sum
- partial_query_matches = words.map{|w| self.send(field).to_s.downcase.scan(query.downcase).size}.sum
- exact_word_matches = words.map{|w| self.send(field).to_s.downcase == w.downcase ? 2 : 0 }.sum
- exact_query_matches = words.map{|w| self.send(field).to_s.downcase == query.downcase ? 2 : 0 }.sum
- partial_word_matches + partial_query_matches + exact_word_matches + exact_query_matches
+ content = self.send(field).to_s.downcase
+ words.map do |w|
+ w = w.downcase
+ # one point per partial word matches
+ (content.scan(w).size +
+ # one point per partial query matches
+ content.scan(query).size +
+ # two points for exact word match
+ (content == w ? 2 : 0) +
+ # two points for exact query match
+ (content == query ? 2 : 0))
+ end.sum
end.sum
@naive_relevance[query] = score
@@ -1,3 +1,3 @@
module NaiveSearch
- VERSION = "0.1.3"
+ VERSION = "0.1.4"
end

0 comments on commit 2fe72c1

Please sign in to comment.