Class: LogStash::Inputs::Elasticsearch

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

Overview

.Compatibility Note

NOTE

Starting with Elasticsearch 5.3, there’s an refmodules-http.html[HTTP setting] called ‘http.content_type.required`. If this option is set to `true`, and you are using Logstash 2.4 through 5.2, you need to update the Elasticsearch input plugin to version 4.0.2 or higher.

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 {
  hosts => "localhost"
  query => '{ "query": { "match": { "statuscode": 200 } }, "sort": [ "_doc" ] }'
}

}

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
  }
},
"sort": [ "_doc" ]

}‘

Instance Method Summary collapse

Instance Method Details

#registerObject



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
145
146
147
148
# File 'lib/logstash/inputs/elasticsearch.rb', line 117

def register
  require "elasticsearch"

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

  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 do |h|
      host, port = h.split(":")
      { :host => host, :scheme => 'https', :port => port }
    end
  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



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/logstash/inputs/elasticsearch.rb', line 150

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

  r['hits']['hits'].each { |hit| push_hit(hit, output_queue) }
  has_hits = r['hits']['hits'].any?

  while has_hits && !stop?
    r = process_next_scroll(output_queue, r['_scroll_id'])
    has_hits = r['has_hits']
  end
end