Class: LogStash::Inputs::Elasticsearch

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/elasticsearch.rb

Overview

Read from an Elasticsearch cluster, based on search query results. This is useful for replaying test logs, reindexing, etc.

Example:

source,ruby

input {

# Read all documents from Elasticsearch matching the given query
elasticsearch {
  host => "localhost"
  query => '{ "query": { "match": { "statuscode": 200 } } }'
}

}

This would create an Elasticsearch query with the following format:

source,json

curl ‘localhost:9200/logstash-*/_search?&scroll=1m&size=1000’ -d ‘{

"query": {
  "match": {
    "statuscode": 200
  }
}

}‘

Instance Method Summary collapse

Instance Method Details

#registerObject



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
# File 'lib/logstash/inputs/elasticsearch.rb', line 110

def register
  require "elasticsearch"

  @options = {
    :index => @index,
    :body => @query,
    :scroll => @scroll,
    :size => @size
  }

  @options[:search_type] = 'scan' if @scan

  transport_options = {}

  if @user && @password
    token = Base64.strict_encode64("#{@user}:#{@password.value}")
    transport_options[:headers] = { :Authorization => "Basic #{token}" }
  end

  hosts = if @ssl then
    @hosts.map { |h| { :host => h, :scheme => 'https' } }
  else
    @hosts
  end

  if @ssl && @ca_file
    transport_options[:ssl] = { :ca_file => @ca_file }
  end

  @client = Elasticsearch::Client.new(:hosts => hosts, :transport_options => transport_options)
end

#run(output_queue) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/logstash/inputs/elasticsearch.rb', line 143

def run(output_queue)

  # get first wave of data
  r = @client.search(@options)

  # since 'scan' doesn't return data on the search call, do an extra scroll
  if @scan
    r = scroll_request(r['_scroll_id'])
  end

  while r['hits']['hits'].any? do
    r['hits']['hits'].each do |hit|
      event = LogStash::Event.new(hit['_source'])
      decorate(event)

      if @docinfo
        event[@docinfo_target] ||= {}

        unless event[@docinfo_target].is_a?(Hash)
          @logger.error("Elasticsearch Input: Incompatible Event, incompatible type for the `@metadata` field in the `_source` document, expected a hash got:", :metadata_type => event[@docinfo_target].class)

          raise Exception.new("Elasticsearch input: incompatible event") 
        end

        @docinfo_fields.each do |field|
          event[@docinfo_target][field] = hit[field]
        end
      end

      output_queue << event
    end
    r = scroll_request(r['_scroll_id'])
  end
end