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 {
  hosts => "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



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

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 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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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 = process_next_scroll(output_queue, r['_scroll_id'])
    has_hits = r['has_hits']
  else # not a scan, process the response
    r['hits']['hits'].each { |hit| push_hit(hit, output_queue) }
    has_hits = r['hits']['hits'].any?
  end

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