Class: FetchEsData

Inherits:
Object
  • Object
show all
Defined in:
lib/fetch_es_data.rb

Overview

having methods to fetch data from Elastic search

Instance Method Summary collapse

Constructor Details

#initialize(search_host, search_port) ⇒ FetchEsData

initializing elastic search host and port

Parameters:

  • search_host (String)

    Elastic Host

  • search_port (String)

    Elastic port



8
9
10
11
# File 'lib/fetch_es_data.rb', line 8

def initialize(search_host, search_port)
	@search_host = search_host
	@search_port = search_port
end

Instance Method Details

#fetch_shortlisted_data_from_es(query:, index_name:, type_name:, extension: '_search') ⇒ String, Hash

fetches data from elastic search

Parameters:

  • query (String)

    the input

  • index_name (String)

    name of the index

  • type_name (String)

    index type

  • extension (String) (defaults to: '_search')

    extension to Elastic seach path (eg. ‘_search’, ‘_msearch’)

Returns:

  • (String, Hash)


20
21
22
23
24
25
26
27
28
29
# File 'lib/fetch_es_data.rb', line 20

def fetch_shortlisted_data_from_es(query:, index_name:, type_name:, extension: '_search')
	uri = URI("http://#{@search_host}:#{@search_port}/#{index_name}/#{type_name}/#{extension}")
	req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
	req.body = "#{query.to_json}\n"
	res = Net::HTTP.start(uri.hostname, uri.port) do |http|
		http.request(req)
	end
	body = JSON.parse(res.body) rescue {}
	return res.code, body
end