Class: Elastic

Inherits:
Object
  • Object
show all
Defined in:
lib/cl/magic/common/elastic.rb

Instance Method Summary collapse

Constructor Details

#initialize(elastic_url) ⇒ Elastic

Returns a new instance of Elastic.



2
3
4
# File 'lib/cl/magic/common/elastic.rb', line 2

def initialize(elastic_url)
  @elastic_url = elastic_url
end

Instance Method Details

#create_index(elastic_index, body) ⇒ Object



37
38
39
40
# File 'lib/cl/magic/common/elastic.rb', line 37

def create_index(elastic_index, body)
  url = "#{elastic_index}"
  return post(url, "PUT", body)
end

#post(url, verb, data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cl/magic/common/elastic.rb', line 20

def post(url, verb, data)
  final_url = "#{@elastic_url}/#{url}"

  # sanitize
  sanitized_data = data.to_json
  ["'", ""].each { |c| sanitized_data.gsub!(c, "\#{c}") }

  # post
  cmd = """
    curl -s -X#{verb} \
      #{final_url} \
      -H 'Content-Type: application/json' \
      -d '#{sanitized_data}'
  """
  return `#{cmd}`
end

#query_by_id(ids) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cl/magic/common/elastic.rb', line 6

def query_by_id(ids)
  url = "/_search"
  verb = "POST"
  data = {
    query: {
      terms: {
        _id: ids
      }
    }
  }
  sanitized_data = data.to_json
  return post(url, verb, data)
end