Class: LogStash::Filters::Elasticsearch::DslExecutor
- Inherits:
-
Object
- Object
- LogStash::Filters::Elasticsearch::DslExecutor
- Defined in:
- lib/logstash/filters/elasticsearch/dsl_executor.rb
Instance Method Summary collapse
-
#initialize(plugin, logger) ⇒ DslExecutor
constructor
A new instance of DslExecutor.
- #process(client, event) ⇒ Object
Constructor Details
#initialize(plugin, logger) ⇒ DslExecutor
Returns a new instance of DslExecutor.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/logstash/filters/elasticsearch/dsl_executor.rb', line 7 def initialize(plugin, logger) @index = plugin.params["index"] @query = plugin.params["query"] @query_dsl = plugin.query_dsl @fields = plugin.params["fields"] @result_size = plugin.params["result_size"] @docinfo_fields = plugin.params["docinfo_fields"] @tag_on_failure = plugin.params["tag_on_failure"] @enable_sort = plugin.params["enable_sort"] @sort = plugin.params["sort"] @aggregation_fields = plugin.params["aggregation_fields"] @logger = logger @event_decorator = plugin.method(:decorate) @target_field = plugin.params["target"] if @target_field def self.apply_target(path); "[#{@target_field}][#{path}]"; end else def self.apply_target(path); path; end end end |
Instance Method Details
#process(client, event) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 90 91 |
# File 'lib/logstash/filters/elasticsearch/dsl_executor.rb', line 28 def process(client, event) matched = false begin params = { :index => event.sprintf(@index) } if @query_dsl query = LogStash::Json.load(event.sprintf(@query_dsl)) params[:body] = query else query = event.sprintf(@query) params[:q] = query params[:size] = @result_size params[:sort] = @sort if @enable_sort end @logger.debug("Querying elasticsearch for lookup", :params => params) results = client.search(params) raise "Elasticsearch query error: #{results["_shards"]["failures"]}" if results["_shards"].include? "failures" event.set("[@metadata][total_hits]", extract_total_from_hits(results['hits'])) result_hits = results["hits"]["hits"] if !result_hits.nil? && !result_hits.empty? matched = true @fields.each do |old_key, new_key| old_key_path = extract_path(old_key) extracted_hit_values = result_hits.map do |doc| extract_value(doc["_source"], old_key_path) end value_to_set = extracted_hit_values.count > 1 ? extracted_hit_values : extracted_hit_values.first set_to_event_target(event, new_key, value_to_set) end @docinfo_fields.each do |old_key, new_key| old_key_path = extract_path(old_key) extracted_docs_info = result_hits.map do |doc| extract_value(doc, old_key_path) end value_to_set = extracted_docs_info.count > 1 ? extracted_docs_info : extracted_docs_info.first set_to_event_target(event, new_key, value_to_set) end end result_aggregations = results["aggregations"] if !result_aggregations.nil? && !result_aggregations.empty? matched = true @aggregation_fields.each do |agg_name, ls_field| set_to_event_target(event, ls_field, result_aggregations[agg_name]) end end rescue => e if @logger.trace? @logger.warn("Failed to query elasticsearch for previous event", :index => @index, :query => @query, :event => event.to_hash, :error => e., :backtrace => e.backtrace) elsif @logger.debug? @logger.warn("Failed to query elasticsearch for previous event", :index => @index, :error => e., :backtrace => e.backtrace) else @logger.warn("Failed to query elasticsearch for previous event", :index => @index, :error => e.) end @tag_on_failure.each { |tag| event.tag(tag) } else @event_decorator.call(event) if matched end end |