impress/app/flex/flex_search_extender.rb
Matchu 6e09b8bc10 globalized search first draft
Confirmed features:
    * Output (retrieval, sorting, etc.)
    * Name (positive and negative, but new behavior)
    * Flags (positive and negative)

Planned features:
    * users:owns, user:wants

Known issues:
    * Sets are broken
        * Don't render properly
        * Shouldn't actually be done as joined sets, anyway, since
          we actually want (set1_zone1 OR set1_zone2) AND
          (set2_zone1 OR set2_zone2), which will require breaking
          it into multiple terms queries.
    * Name has regressed: ignores phrases, doesn't require *all*
      words. While we're breaking sets into multiple queries,
      maybe we'll do something similar for name. In fact, we
      really kinda have to if we're gonna keep sorting by name,
      since "straw hat" returns all hats. Eww.
2013-01-24 18:24:35 -06:00

36 lines
1.2 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 scoped_loaded_collection(options)
options[:scopes] ||= {}
@loaded_collection ||= begin
records = []
# returns a structure like {Comment=>[{"_id"=>"123", ...}, {...}], BlogPost=>[...]}
h = Flex::Utils.group_array_by(collection) do |d|
d.mapped_class(should_raise=true)
end
h.each do |klass, docs|
scope = options[:scopes][klass.name] || klass.scoped
records |= scope.find(docs.map(&:_id))
end
class_ids = collection.map { |d| [d.mapped_class.to_s, d._id] }
# Reorder records to preserve order from search results
records = class_ids.map do |class_str, id|
records.detect do |record|
record.class.to_s == class_str && record.id.to_s == id.to_s
end
end
records.extend Flex::Result::Collection
records.setup(self['hits']['total'], variables)
records
end
end
end