这是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
41 changes: 21 additions & 20 deletions lib/acts_as_indexed/class_methods.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: utf-8
# ActsAsIndexed
# Copyright (c) 2007 - 2011 Douglas F Shearer.
# http://douglasfshearer.com
Expand Down Expand Up @@ -103,15 +104,19 @@ def index_update(record)
# 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.

def search_index(query, find_options={}, options={})
def search_index(query, find_options={}, options={}, locale = nil)
# Setting to current locale if none specified.
locale = I18n.locale if locale.nil?
locale_separator = "¤"
query_with_locale = query + locale_separator + locale.to_s

# Clear the query cache off if the key is set.
@query_cache = {} if options[:no_query_cache]

# Run the query if not already in cache.
if !@query_cache || !@query_cache[query]
build_index unless aai_config.index_file.directory?
(@query_cache ||= {})[query] = new_index.search(query)
if !@query_cache || !@query_cache[query_with_locale]
build_index unless (aai_config.index_file + "/" + I18n.locale.to_s).directory?
(@query_cache ||= {})[query_with_locale] = new_index.search(query)
end

if options[:ids_only]
Expand All @@ -123,13 +128,12 @@ def search_index(query, find_options={}, options={})
end

if find_options.include?(:order)
part_query = @query_cache[query].map{ |r| r.first }

part_query = @query_cache[query_with_locale].map{ |r| r.first }
else
# slice up the results by offset and limit
offset = find_options[:offset] || 0
limit = find_options.include?(:limit) ? find_options[:limit] : @query_cache[query].size
part_query = @query_cache[query].sort_by{ |r| r.last }.slice(offset,limit).map{ |r| r.first }
limit = find_options.include?(:limit) ? find_options[:limit] : @query_cache[query_with_locale].size
part_query = @query_cache[query_with_locale].sort_by{ |r| r.last }.slice(offset,limit).map{ |r| r.first }

# Set these to nil as we are dealing with the pagination by setting
# exactly what records we want.
Expand All @@ -145,19 +149,16 @@ def search_index(query, find_options={}, options={})
records = find(:all, :conditions => [ "#{table_name}.#{primary_key} IN (?)", part_query])

if find_options.include?(:order)
records # Just return the records without ranking them.

else
# Results come back in random order from SQL, so order again.
ranked_records = {}
records.each do |r|
ranked_records[r] = @query_cache[query][r.id]
end

ranked_records.to_a.sort_by{ |a| a.last }.map{ |r| r.first}
end
records # Just return the records without ranking them.
else
# Results come back in random order from SQL, so order again.
ranked_records = {}
records.each do |r|
ranked_records[r] = @query_cache[query_with_locale][r.id]
end
ranked_records.to_a.sort_by{ |a| a.last }.map{ |r| r.first}
end
end

end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_indexed/search_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SearchIndex
# fields:: Fields or instance methods of ActiveRecord model to be indexed.
# config:: ActsAsIndexed::Configuration instance.
def initialize(fields, config)
@storage = Storage.new(Pathname.new(config.index_file.to_s), config.index_file_depth)
@storage = Storage.new(Pathname.new(config.index_file.to_s + "/" + I18n.locale.to_s), config.index_file_depth)
@fields = fields
@atoms = {}
@min_word_size = config.min_word_size
Expand Down