From 80c291c9725faeb1378d68119974a2c19eab641e Mon Sep 17 00:00:00 2001 From: Evan Rolfe Date: Fri, 2 May 2014 13:21:47 +0100 Subject: [PATCH 1/2] made search_index method accept :include_rank option which returns the model objects and their corresponding search rank --- lib/acts_as_indexed/class_methods.rb | 10 +++++++++- test/integration/acts_as_indexed_test.rb | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_indexed/class_methods.rb b/lib/acts_as_indexed/class_methods.rb index 97b43f0..63574d4 100644 --- a/lib/acts_as_indexed/class_methods.rb +++ b/lib/acts_as_indexed/class_methods.rb @@ -149,7 +149,15 @@ def search_index(query, find_options={}, options={}) ranked_records[r] = @query_cache[query][r.id] end - sort(ranked_records.to_a).map{ |r| r.first } + sorted_records = sort(ranked_records.to_a) + + if options.include?(:include_rank) + sorted_records.map do |r| + {record: r.first, rank: r.second} + end + else + sorted_records.map{ |r| r.first } #Remove the rank + end end end diff --git a/test/integration/acts_as_indexed_test.rb b/test/integration/acts_as_indexed_test.rb index 39825ed..820fe4f 100644 --- a/test/integration/acts_as_indexed_test.rb +++ b/test/integration/acts_as_indexed_test.rb @@ -131,6 +131,15 @@ def test_start_quoted_queries run_queries(queries) end + def test_queries_return_rank + result = Post.find_with_index('crane', {}, {include_rank: true}); + assert_equal 2, result.length + assert_not_nil result.first[:record] + assert_not_nil result.first[:rank] + + assert_equal 2, result.first[:rank].floor + assert_equal 1, result.second[:rank].floor + end # NOTE: This test always fails for Rails 2.3. A bug somewhere in either # Rails or the SQLite adaptor which causes the offset to be ignored. From c2d3902d6268a0b053e8dd882dba0735f31b7d2e Mon Sep 17 00:00:00 2001 From: Evan Rolfe Date: Sun, 25 Oct 2015 09:19:22 +0000 Subject: [PATCH 2/2] ruby styleguide changes & doc comments --- lib/acts_as_indexed/class_methods.rb | 10 ++++++---- test/integration/acts_as_indexed_test.rb | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/acts_as_indexed/class_methods.rb b/lib/acts_as_indexed/class_methods.rb index 63574d4..76c6830 100644 --- a/lib/acts_as_indexed/class_methods.rb +++ b/lib/acts_as_indexed/class_methods.rb @@ -97,7 +97,9 @@ def index_update(record) # ====options # ids_only:: Method returns an array of integer IDs when set to true. # no_query_cache:: Turns off the query cache when set to true. Useful for testing. - + # include_rank:: If set to true then this returns the hash with the record + # and the rank. Otherwise it just returns the record object. + # Default is false. def search_index(query, find_options={}, options={}) # Clear the query cache off if the key is set. @@ -151,9 +153,9 @@ def search_index(query, find_options={}, options={}) sorted_records = sort(ranked_records.to_a) - if options.include?(:include_rank) + if options[:include_rank] sorted_records.map do |r| - {record: r.first, rank: r.second} + { record: r.first, rank: r.second } end else sorted_records.map{ |r| r.first } #Remove the rank @@ -202,4 +204,4 @@ def new_index end -end \ No newline at end of file +end diff --git a/test/integration/acts_as_indexed_test.rb b/test/integration/acts_as_indexed_test.rb index 820fe4f..0441f69 100644 --- a/test/integration/acts_as_indexed_test.rb +++ b/test/integration/acts_as_indexed_test.rb @@ -132,7 +132,7 @@ def test_start_quoted_queries end def test_queries_return_rank - result = Post.find_with_index('crane', {}, {include_rank: true}); + result = Post.find_with_index('crane', {}, { include_rank: true }); assert_equal 2, result.length assert_not_nil result.first[:record] assert_not_nil result.first[:rank]