这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/acts_as_indexed/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -194,4 +204,4 @@ def new_index

end

end
end
9 changes: 9 additions & 0 deletions test/integration/acts_as_indexed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down