Class: Elastify::Helpers::ElasticSearch::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/elastify/helpers/elastic_search/connector.rb

Class Method Summary collapse

Class Method Details

.create(options, data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 6

def self.create(options, data)
    if data.blank?
        raise :elastify__create__required_data
    end
    if data[:id].blank?
        raise :elastify__create__required_data_id
    end
    url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
    JSON.parse(RestClient.put(url, data.to_json, {}))
end

.create_index(options) ⇒ Object



60
61
62
63
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 60

def self.create_index(options)
    url = "#{options[:base_url]}/#{options[:index]}"
    JSON.parse(RestClient.put(url, {}.to_json, {})).to_hash
end

.create_mapping(options) ⇒ Object



70
71
72
73
74
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 70

def self.create_mapping(options)
    url = "#{options[:base_url]}/#{options[:index]}/_mappings/#{options[:type]}"
    puts options[:map]
    JSON.parse(RestClient.put(url, options[:map].squish, {})).to_hash
end

.destroy(options, data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 28

def self.destroy(options, data)
    if data.blank?
        raise :elastify__delete__required_data
    end
    if data[:id].blank?
        raise :elastify__delete__required_data_id
    end
    url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
    JSON.parse(RestClient.delete(url)).to_hash
end

.destroy_index(options) ⇒ Object



65
66
67
68
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 65

def self.destroy_index(options)
    url = "#{options[:base_url]}/#{options[:index]}"
    JSON.parse(RestClient.delete(url)).to_hash
end

.scroll(options, scroll_id, scroll_timeout) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 49

def self.scroll(options, scroll_id, scroll_timeout)
    if scroll_id.blank?
        raise :elastify__search__required_scroll_id
    end
    url = "#{options[:base_url]}/_search/scroll"
    dsl = { scroll_id: scroll_id }
    scroll_timeout ||= options[:scroll_timeout]
    dsl[:scroll] = scroll_timeout if scroll_timeout.present?
    Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
end

.search(options, dsl, scroll_timeout) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 39

def self.search(options, dsl, scroll_timeout)
    if dsl.blank?
        raise :elastify__search__required_dsl
    end
    url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/_search"
    scroll_timeout ||= options[:scroll_timeout]
    url += "?scroll=#{scroll_timeout}" if scroll_timeout.present?
    Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
end

.update(options, data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/elastify/helpers/elastic_search/connector.rb', line 17

def self.update(options, data)
    if data.blank?
        raise :elastify__update__required_data
    end
    if data[:id].blank?
        raise :elastify__update__required_data_id
    end
    url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
    JSON.parse(RestClient.put(url, data.to_json, {})).to_hash
end