module Autocompletion
  AUTOCOMPLETION_KEY = "autocompletion"

  def self.lookup term
    limit = {limit: [0, 5]}

    $redis.zrangebylex(AUTOCOMPLETION_KEY, "(#{term.downcase}", "+", limit).map do |result|
      result = result.split("||")

      term = result[0]
      name = result[1]
      type = result[2]
      identifier = result[3]

      url = type == 'protected_area' ? "/#{identifier}" : "/country/#{identifier}"

      { title: name, url: url }
    end
  end

  def self.populate
    ProtectedArea.pluck(:name, :wdpa_id).each do |name, wdpa_id|
      $redis.zadd(AUTOCOMPLETION_KEY, 0, "#{name.downcase}||#{name}||protected_area||#{wdpa_id}")
    end

    Country.pluck(:name, :iso).each do |name, iso|
      $redis.zadd(AUTOCOMPLETION_KEY, 0, "#{name.downcase}||#{name}||country||#{iso}")
    end
  end

  def self.drop
    $redis.del(AUTOCOMPLETION_KEY)
  end
end

module Autocompletion
  AUTOCOMPLETION_KEY = "autocompletion".freeze
  IDENTIFIER_FIELDS = {
    'protected_area' => :wdpa_id,
    'country' => :iso_3
  }.freeze

  def self.lookup(term, search_index=Search::PA_INDEX)
    search = Search.search(term.downcase, {}, search_index)

    results = search.results.objects.values.compact.flatten

    results.map do |result|
      name = result.name
      type = result.class.name.underscore
      identifier = result.send(identifier_field(type))

      url = type == 'country' ? "/country/#{identifier}" : "/#{identifier}"

      { title: name, url: url }
    end
  end

  private

  def self.identifier_field(type)
    IDENTIFIER_FIELDS[type] || :id
  end
end

Refactor autocomplete to use ElasticSearch and without limit
Refactor autocomplete to use ElasticSearch and without limit
