Commit f4699a8e484f86fdaab420746c7682b4321239d9

Authored by Rodrigo Souto
1 parent e9b2ae4f

[search-improvements] If no results indexed, relevance must be zero

app/models/search_term.rb
... ... @@ -42,6 +42,7 @@ class SearchTerm < ActiveRecord::Base
42 42  
43 43 def calculate_relevance(valid_occurrences)
44 44 indexed = valid_occurrences.last.indexed.to_f
  45 + return 0 if indexed == 0
45 46 total = valid_occurrences.last.total.to_f
46 47 (1 - indexed/total)*100
47 48 end
... ...
test/unit/search_term_test.rb
... ... @@ -118,4 +118,12 @@ class SearchTermTest < ActiveSupport::TestCase
118 118  
119 119 assert st1.score > st2.score, "Less ratio results:total are not getting higher scores."
120 120 end
  121 +
  122 + should 'consider relevance zero if no results indexed' do
  123 + st = SearchTerm.find_or_create('st', Environment.default)
  124 + SearchTermOccurrence.create!(:search_term => st, :total => 10, :indexed => 0)
  125 + SearchTerm.calculate_scores
  126 + st.reload
  127 + assert_equal 0, st.score
  128 + end
121 129 end
... ...