Class: LogStash::Search::Twitter

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/search/twitter.rb

Instance Method Summary collapse

Methods inherited from Base

#count, #popular_terms

Constructor Details

#initialize(settings = {}) ⇒ Twitter

Returns a new instance of Twitter.



13
14
15
16
17
# File 'lib/logstash/search/twitter.rb', line 13

def initialize(settings={})
  @host = (settings[:host] || "search.twitter.com")
  @port = (settings[:port] || 80).to_i
  @logger = LogStash::Logger.new(STDOUT)
end

Instance Method Details

#histogram(query, field, interval = nil) {|result| ... } ⇒ Object

def search

Yields:

  • (result)


85
86
87
88
89
# File 'lib/logstash/search/twitter.rb', line 85

def histogram(query, field, interval=nil)
  # Nothing to histogram.
  result = LogStash::Search::FacetResult.new
  yield result
end

#search(query) ⇒ Object



20
21
22
23
24
25
26
27
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
# File 'lib/logstash/search/twitter.rb', line 20

def search(query)
  raise "No block given for search call." if !block_given?
  if query.is_a?(String)
    query = LogStash::Search::Query.parse(query)
  end

  # TODO(sissel): only search a specific index?
  http = EventMachine::HttpRequest.new("http://#{@host}:#{@port}/search.json?q=#{URI.escape(query.query_string)}&rpp=#{URI.escape(query.count) rescue query.count}")

  @logger.info(["Query", query])

  start_time = Time.now
  req = http.get

  result = LogStash::Search::Result.new
  req.callback do
    data = JSON.parse(req.response)
    result.duration = Time.now - start_time

    hits = (data["results"] || nil) rescue nil

    if hits.nil? or !data["error"].nil?
      # Use the error message if any, otherwise, return the whole
      # data object as json as the error message for debugging later.
      result.error_message = (data["error"] rescue false) || data.to_json
      yield result
      next
    end

    hits.each do |hit|
      hit["@message"]  = hit["text"]
      hit["@timestamp"] = hit["created_at"]
      hit.delete("text")
    end

    @logger.info(["Got search results", 
                 { :query => query.query_string, :duration => data["duration"],
                   :result_count => hits.size }])

    if req.response_header.status != 200
      result.error_message = data["error"] || req.inspect
      @error = data["error"] || req.inspect
    end

    # We want to yield a list of LogStash::Event objects.
    hits.each do |hit|
      result.events << LogStash::Event.new(hit)
    end

    # Total hits this search could find if not limited
    result.total = hits.size
    result.offset = 0

    yield result
  end

  req.errback do 
    @logger.warn(["Query failed", query, req, req.response])
    result.duration = Time.now - start_time
    result.error_message = req.response

    yield result
  end
end