class PrettySearch::IndexedCollection

Example extension

PrettySearch::IndexedCollection is like MemoryCollection, but builds an index on a given field for faster searching. Ideal for multiple searches when memory size is not an issue

Public Class Methods

new(data_file, index_field: nil, first: false) click to toggle source
# File lib/pretty_search/collection/indexed_collection.rb, line 9
def initialize(data_file, index_field: nil, first: false)
  if index_field.nil?
    raise MissingParameter, 'Field name to be indexed required'
  end
  data = Yajl::Parser.parse(File.new(data_file))
  @index_field = index_field
  @index = build_index(data, index_field)
end

Public Instance Methods

Private Instance Methods

build_index(data, index_field) click to toggle source
# File lib/pretty_search/collection/indexed_collection.rb, line 28
def build_index(data, index_field)
  Hash.new([]).tap do |index|
    data.each do |doc|
      index[doc[index_field]] << doc
    end
  end
end