Sha256: fd3c779fda9fc1c0e10d3ba8ff092516da42fc0286d025226e2b880e27f50868

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

# encoding: utf-8
#
require 'rsolr'

module Indexers

  # Deprecated. Only here as an example.
  #
  class Solr # :nodoc:

    attr_reader :type, :fields, :solr

    # Takes a Configuration::Type.
    #
    def initialize type
      @type   = type
      @fields = type.solr_fields.map(&:name).map(&:to_sym)
      @solr   = RSolr.connect
    end

    def index
      timed_exclaim "Indexing solr for #{type.name}:#{fields.join(', ')}"
      statement = "SELECT indexed_id, #{fields.join(',')} FROM #{type.snapshot_table_name}"

      DB.connect
      results   = DB.connection.execute statement

      return unless results

      type_name = @type.name.to_s

      solr.delete_by_query "type:#{type_name}"
      solr.commit

      documents = []

      results.each do |indexed_id, *values|
        values.each &:downcase!
        documents << hashed(values).merge(id: indexed_id, type: type_name)
      end

      solr.add documents
      solr.commit
      solr.optimize
    end

    def hashed values
      result = {}
      fields.zip(values).each do |field, value|
        result[field] = value
      end
      result
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
picky-2.6.0 lib/picky/indexers/solr.rb
picky-2.5.2 lib/picky/internals/indexers/solr.rb
picky-2.5.1 lib/picky/internals/indexers/solr.rb
picky-2.5.0 lib/picky/internals/indexers/solr.rb
picky-2.4.3 lib/picky/internals/indexers/solr.rb
picky-2.4.2 lib/picky/internals/indexers/solr.rb
picky-2.4.1 lib/picky/internals/indexers/solr.rb