Class: Solr::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/solr/indexer.rb

Overview

The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_source, mapper_or_mapping, options = {}) ⇒ Indexer

TODO: document options!



17
18
19
20
21
22
23
24
25
26
# File 'lib/solr/indexer.rb', line 17

def initialize(data_source, mapper_or_mapping, options={})
  solr_url = options[:solr_url] || ENV["SOLR_URL"] || "http://localhost:8983/solr"
  @solr = Solr::Connection.new(solr_url, options) #TODO - these options contain the solr_url and debug keys also, so tidy up what gets passed

  @data_source = data_source
  @mapper = mapper_or_mapping.is_a?(Hash) ? Solr::Importer::Mapper.new(mapper_or_mapping) : mapper_or_mapping

  @buffer_docs = options[:buffer_docs]
  @debug = options[:debug]
end

Instance Attribute Details

#solrObject (readonly)

Returns the value of attribute solr.



14
15
16
# File 'lib/solr/indexer.rb', line 14

def solr
  @solr
end

Instance Method Details

#add_docs(documents) ⇒ Object



48
49
50
51
# File 'lib/solr/indexer.rb', line 48

def add_docs(documents)
  @solr.add(documents) unless @debug
  puts documents.inspect if @debug
end

#indexObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/solr/indexer.rb', line 28

def index
  buffer = []
  @data_source.each do |record|
    document = @mapper.map(record)
    
    # TODO: check arrity of block, if 3, pass counter as 3rd argument
    yield(record, document) if block_given? # TODO check return of block, if not true then don't index, or perhaps if document.empty?
    
    buffer << document
    
    if !@buffer_docs || buffer.size == @buffer_docs
      add_docs(buffer)
      buffer.clear
    end
  end
  add_docs(buffer) if !buffer.empty?
  
  @solr.commit unless @debug
end