diff --git a/lib/acts_as_indexed/class_methods.rb b/lib/acts_as_indexed/class_methods.rb index 97b43f0..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. @@ -149,7 +151,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_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 @@ -194,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 39825ed..0441f69 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.