Commit 3b35e215936c959d3297c384fee0622512bda267
1 parent
43548e50
Exists in
master
and in
27 other branches
[search-term-improvements] Simple query normalization using downcase
Showing
5 changed files
with
35 additions
and
4 deletions
Show diff stats
app/controllers/public/search_controller.rb
| @@ -144,7 +144,7 @@ class SearchController < PublicController | @@ -144,7 +144,7 @@ class SearchController < PublicController | ||
| 144 | end | 144 | end |
| 145 | 145 | ||
| 146 | def suggestions | 146 | def suggestions |
| 147 | - render :text => find_suggestions(params[:term], environment, params[:asset]).to_json | 147 | + render :text => find_suggestions(normalize_term(params[:term]), environment, params[:asset]).to_json |
| 148 | end | 148 | end |
| 149 | 149 | ||
| 150 | ####################################################### | 150 | ####################################################### |
app/helpers/search_term_helper.rb
| @@ -10,8 +10,8 @@ module SearchTermHelper | @@ -10,8 +10,8 @@ module SearchTermHelper | ||
| 10 | #FIXME For some reason the job is created but nothing is ran. | 10 | #FIXME For some reason the job is created but nothing is ran. |
| 11 | #handle_asynchronously :register_search_term | 11 | #handle_asynchronously :register_search_term |
| 12 | 12 | ||
| 13 | - #TODO Think better on how to normalize them properly | 13 | + #TODO Think smarter criteria to normalize search terms properly |
| 14 | def normalize_term(search_term) | 14 | def normalize_term(search_term) |
| 15 | - search_term | 15 | + search_term.downcase |
| 16 | end | 16 | end |
| 17 | end | 17 | end |
public/javascripts/application.js
| @@ -1070,7 +1070,7 @@ jQuery(document).ready(function(){ | @@ -1070,7 +1070,7 @@ jQuery(document).ready(function(){ | ||
| 1070 | $(this).closest('form').submit(); | 1070 | $(this).closest('form').submit(); |
| 1071 | }, | 1071 | }, |
| 1072 | source: function(request, response) { | 1072 | source: function(request, response) { |
| 1073 | - var term = request.term; | 1073 | + var term = request.term.toLowerCase(); |
| 1074 | if (term in suggestions_cache) { | 1074 | if (term in suggestions_cache) { |
| 1075 | response(suggestions_cache[term]); | 1075 | response(suggestions_cache[term]); |
| 1076 | return; | 1076 | return; |
test/functional/search_controller_test.rb
| @@ -662,6 +662,14 @@ class SearchControllerTest < ActionController::TestCase | @@ -662,6 +662,14 @@ class SearchControllerTest < ActionController::TestCase | ||
| 662 | assert_equal [st1,st2].to_json, response.body | 662 | assert_equal [st1,st2].to_json, response.body |
| 663 | end | 663 | end |
| 664 | 664 | ||
| 665 | + should 'normalize search term for suggestions' do | ||
| 666 | + st1 = 'UnIvErSe A' | ||
| 667 | + st2 = 'uNiVeRsE B' | ||
| 668 | + @controller.stubs(:find_suggestions).with('universe', Environment.default, 'communities').returns([st1, st2]) | ||
| 669 | + get :suggestions, :term => 'UNIveRSE', :asset => 'communities' | ||
| 670 | + assert_equal [st1,st2].to_json, response.body | ||
| 671 | + end | ||
| 672 | + | ||
| 665 | protected | 673 | protected |
| 666 | 674 | ||
| 667 | def create_event(profile, options) | 675 | def create_event(profile, options) |
| @@ -0,0 +1,23 @@ | @@ -0,0 +1,23 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | + | ||
| 3 | +class SearchTermTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + include SearchTermHelper | ||
| 6 | + | ||
| 7 | + should 'register search term' do | ||
| 8 | + environment = Environment.default | ||
| 9 | + assert_difference 'SearchTerm.count', 1 do | ||
| 10 | + register_search_term('fred mercury', 10, 1, environment, 'people') | ||
| 11 | + end | ||
| 12 | + end | ||
| 13 | + | ||
| 14 | + should 'register search term normalized' do | ||
| 15 | + environment = Environment.default | ||
| 16 | + assert_difference 'SearchTerm.count', 1 do | ||
| 17 | + register_search_term('FrEd mErCuRy', 10, 1, environment, 'people') | ||
| 18 | + register_search_term('fReD MeRcUrY', 10, 1, environment, 'people') | ||
| 19 | + assert SearchTerm.find_or_create('fred mercury', environment, 'people') | ||
| 20 | + end | ||
| 21 | + end | ||
| 22 | + | ||
| 23 | +end |