lucene/client/ruby/solr-ruby/lib/solr/indexer.rb

29 lines
1.2 KiB
Ruby
Raw Normal View History

# 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.
class Solr::Indexer
def self.index(data_source, mapper, options={})
solr_url = options[:solr_url] || ENV["SOLR_URL"] || "http://localhost:8983/solr"
solr = Solr::Connection.new(solr_url)
data_source.each do |record|
document = mapper.map(record)
# yield(document) if block_given? # TODO: does yielding add value here? possibly to allow the caller
# to manipulate the document outside of mappings just before indexing?
solr.add(document) unless options[:debug]
puts document.inspect if options[:debug]
end
solr.commit unless options[:debug]
end
end