Class: LogStash::Outputs::Solr

Inherits:
Base
  • Object
show all
Includes:
Stud::Buffer
Defined in:
lib/logstash/outputs/solr.rb

Overview

An Solr output that send data to Apache Solr.

Constant Summary collapse

MODE_STANDALONE =
'Standalone'
MODE_SOLRCLOUD =
'SolrCloud'

Instance Method Summary collapse

Instance Method Details

#closeObject



147
148
149
150
151
# File 'lib/logstash/outputs/solr.rb', line 147

def close
  unless @zk.nil? then
    @zk.close
  end
end

#flush(events, close = false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/logstash/outputs/solr.rb', line 97

def flush(events, close=false)
  documents_per_col = {}

  events.each do |event|
    document = event.to_hash()

    unless document.has_key?(@unique_key_field) then
      document.merge!({@unique_key_field => SecureRandom.uuid})
    end
    
    unless document.has_key?(@timestamp_field) then
      document.merge!({@timestamp_field => document['@timestamp']})
    end
    
    @logger.debug 'Record: %s' % document.inspect
    
    collection = @collection
    if @collection_field and document.has_key?(@collection_field) then
      collection = document[@collection_field]
      document.delete(@collection_field)
    end
    
    documents = documents_per_col.fetch(collection, [])
    documents.push(document)
    documents_per_col[collection] = documents
  end

  params = {}
  if @commit
    params[:commit] = true
  end  
  params[:commitWithin] = @commitWithin
  
  documents_per_col.each do |collection, documents|
    if @mode == MODE_STANDALONE then
      collection_url = @url.rpartition('/')[0] + '/' + collection
      @solr_std[collection] ||= RSolr.connect :url => collection_url
      @solr_std[collection].add documents, :params => params
      @logger.info 'Added %d document(s) to Solr at "%s"' % [documents.count, collection_url]
    elsif @mode == MODE_SOLRCLOUD then
      @solr_cloud.add documents, collection: collection, :params => params
      @logger.info 'Added %d document(s) to "%s" collection' % [documents.count, collection]
    end
  end

  rescue Exception => e
    @logger.warn("An error occurred while indexing", :exception => e.inspect)
end

#receive(event) ⇒ Object



92
93
94
# File 'lib/logstash/outputs/solr.rb', line 92

def receive(event)
  buffer_receive(event)
end

#registerObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/logstash/outputs/solr.rb', line 58

def register
  @mode = nil
  if ! @url.nil? then
    @mode = MODE_STANDALONE
  elsif ! @zk_host.nil?
    @mode = MODE_SOLRCLOUD
  end

  @solr_std = {}
  @solr_cloud = nil
  @zk = nil

  if @mode == MODE_STANDALONE then
    if !@user.nil? and !@user.to_s.strip.empty? and !@password.nil? and !@password.to_s.strip.empty? then
      uri = URI.parse(@url)
      uri.user = @user
      uri.password = @password
      @url = uri.to_s()
    end
    @solr_std[@collection] = RSolr.connect :url => @url
  elsif @mode == MODE_SOLRCLOUD then
    @zk = ZK.new(@zk_host)
    cloud_connection = RSolr::Cloud::Connection.new(@zk, :user => @user, :password => @password)
    @solr_cloud = RSolr::Client.new(cloud_connection, read_timeout: 60, open_timeout: 60)
  end

  buffer_initialize(
    :max_items => @flush_size,
    :max_interval => @idle_flush_time,
    :logger => @logger
  )
end