forked from OpenNeo/impress
Matchu
e42de795dd
That is, once we get our list of IDs from the search engine, only fetch records whose JSON we don't already have cached. It's simpler here to use as_json, but it'd probably be even faster if I figure out how to serve a plain JSON string from a Rails controller. In the meantime, requests of entirely cached items are coming in at about 85ms on average on my box (dev, cache classes, many items), about 10ms better than the last iteration.
45 lines
1.4 KiB
Ruby
45 lines
1.4 KiB
Ruby
# see the detailed Extenders documentation at https://github.com/ddnexus/flex/wiki/Extenders
|
|
|
|
module FlexSearchExtender
|
|
|
|
# set this method to restrict this extender to certain types of results
|
|
# see the other Flex extenders for reference (https://github.com/ddnexus/flex/tree/master/lib/flex/result)
|
|
def self.should_extend?(response)
|
|
true
|
|
end
|
|
|
|
def proxied_collection
|
|
Item.build_proxies(collection.map(&:_id)).tap do |proxies|
|
|
proxies.extend Flex::Result::Collection
|
|
proxies.setup(self['hits']['total'], variables)
|
|
end
|
|
end
|
|
|
|
def scoped_loaded_collection(options)
|
|
options[:scopes] ||= {}
|
|
@loaded_collection ||= begin
|
|
records_by_class_and_id_str = {}
|
|
grouped_collection = collection.group_by { |d|
|
|
d.mapped_class(should_raise=true)
|
|
}
|
|
grouped_collection.each do |klass, docs|
|
|
record_ids = docs.map(&:_id)
|
|
scope = options[:scopes][klass.name] || klass.scoped
|
|
records = scope.find(record_ids)
|
|
records.each do |record|
|
|
records_by_class_and_id_str[record.class] ||= {}
|
|
records_by_class_and_id_str[record.class][record.id.to_s] = record
|
|
end
|
|
end
|
|
|
|
# Reorder records to preserve order from search results
|
|
records = collection.map do |d|
|
|
records_by_class_and_id_str[d.mapped_class][d._id]
|
|
end
|
|
records.extend Flex::Result::Collection
|
|
records.setup(self['hits']['total'], variables)
|
|
records
|
|
end
|
|
end
|
|
|
|
end
|