elastify 0.1.5 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4ab502f7c03b5f8a7fa7af85d90b19aa802f4db
4
- data.tar.gz: a6c9b23857f1570301e01ff350a12a898abb5720
3
+ metadata.gz: 0776e84590a5077aede923f0523f3565590174ee
4
+ data.tar.gz: db4c2697ee68585beafa19121411e802ccddb3eb
5
5
  SHA512:
6
- metadata.gz: c9726dc4fc3c9ea720707d2cc456ab76cef7b8d33b633c885c8a31b4699d9f268616a92e6355bd40184c1adaff02266ae9b8dbad580a4fae92d4662cdc217c2a
7
- data.tar.gz: 172f7ca92c8826728cd7bca85b3d73f4e26368d9370a15eb24da29b63be7987a4633ca22cd64b31848a98f7d23042e2da03c24c81ede762359cf4d3478f59a89
6
+ metadata.gz: 2187b6b1b38cb00326579c023c7e0c678cb87fb88b892315eaa961c7868ae221681aff0ca9a9796a03e2bfa0043233f990dff85043d68608b61e7e66ad027a14
7
+ data.tar.gz: f34de86be910ce647fd68ee355cdc4fb12ce659d8713182bd0587a84707875171e62295fd1a3a55a80708faf429695a528b415cdbf78b9e9a062e605b7d4cbb8
data/bin/console CHANGED
File without changes
data/bin/setup CHANGED
File without changes
data/lib/elastify.rb CHANGED
@@ -1,5 +1,14 @@
1
- require "elastify/version"
2
- require "elastify/active_record_extensions"
1
+ require 'elastify/version'
2
+ require 'elastify/errors/base'
3
+ require 'elastify/errors/bad_request'
4
+ require 'elastify/errors/connection'
5
+ require 'elastify/configurators/model'
6
+ require 'elastify/active_record_extensions'
7
+ require 'elastify/helpers/elastic_search/connector'
8
+ require 'elastify/helpers/elastic_search/document'
9
+ require 'elastify/helpers/elastic_search/search_result'
10
+ require 'elastify/helpers/elastic_search/search_result_collection'
11
+
3
12
 
4
13
  module Elastify
5
14
 
@@ -7,14 +16,14 @@ module Elastify
7
16
  def configure &block
8
17
  mappings = {}
9
18
  configs = OpenStruct.new({
10
- base_url: "http://localhost:9200",
11
- mappings_path: Rails.root.join("config/elastify/mappings")
19
+ base_url: 'http://localhost:9200',
20
+ mappings_path: Rails.root.join('config/elastify/mappings')
12
21
  })
13
22
  block.call(configs) if block_given?
14
23
  dir = configs.mappings_path
15
24
  if Dir.exist?(dir)
16
25
  Dir.glob("#{dir}/*.json") do |file_path|
17
- mappings[File.basename(file_path, ".json")] = JSON.parse(File.read(file_path))
26
+ mappings[File.basename(file_path, '.json')] = JSON.parse(File.read(file_path))
18
27
  end
19
28
  end
20
29
  Rails.application.config.elastify = {
@@ -1,114 +1,100 @@
1
1
  require 'active_record'
2
2
  require 'backgrounded'
3
- require 'elastify/elastic_search_helper'
4
3
  require 'elastify'
5
4
 
6
- module Elastify
7
- module ActiveRecordExtensions
8
- extend ActiveSupport::Concern
5
+ module Elastify::ActiveRecordExtensions
6
+ extend ActiveSupport::Concern
9
7
 
10
- module ClassMethods
11
- def elastify type, index, &block
12
- include Elastify::ActiveRecordExtensions::LocalMethods
13
- cattr_accessor :elastify_options, :elastify_model_block
14
- attr_accessor :elastify_serialized_document
8
+ module ClassMethods
9
+ extend ActiveSupport::Concern
15
10
 
16
- self.elastify_options = {
17
- base_url: Elastify.configs[:base_url],
18
- index: index,
19
- type: type,
20
- map: Elastify.mappings.symbolize_keys[type.to_sym]
21
- }
11
+ def elastify_setup(&block)
12
+ include Elastify::ActiveRecordExtensions::LocalMethods
13
+ cattr_accessor :elastify_options
14
+ attr_accessor :elastify_serialized_document
15
+
16
+ config = Elastify::Configurators::Model.new
17
+ yield(config) if block_given?
18
+ self.elastify_options = {} if self.elastify_options.blank?
19
+ self.elastify_options[:base_url] = Elastify.configs[:base_url]
20
+ self.elastify_options[:index] = config.opt_index if config.opt_index.present?
21
+ self.elastify_options[:type] = config.opt_type if config.opt_type.present?
22
+ self.elastify_options[:map] = config.opt_mapping if config.opt_mapping.present?
23
+ self.elastify_options[:decode] = config.opt_decode if config.opt_decode.present?
24
+ self.elastify_options[:encode] = config.opt_encode if config.opt_encode.present?
25
+ end
22
26
 
23
- self.elastify_options.each do |key, value|
24
- if value.blank?
25
- raise ElastifyError, "You must specify the #{key} value"
26
- end
27
- end
27
+ def elastify_search(dsl: nil, scroll_timer: "1m")
28
+ return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).search(dsl, scroll_timer)
29
+ end
28
30
 
29
- if block_given?
30
- self.elastify_model_block = block
31
- else
32
- if self.respond_to?(:to_elastify)
33
- self.elastify_model_block = Proc.new { |item| next item.to_elastify }
34
- elsif self.respond_to?(:to_serial)
35
- self.elastify_model_block = Proc.new { |item| next item.to_serial }
36
- else
37
- self.elastify_model_block = Proc.new { |item| next item.serializable_hash }
38
- end
39
- end
40
- end
31
+ def elastify_scroll(scroll_id: nil, scroll_timer: "1m")
32
+ return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).scroll(scroll_id, scroll_timer)
41
33
  end
34
+ end
42
35
 
43
- module LocalMethods
44
- extend ActiveSupport::Concern
36
+ module LocalMethods
37
+ extend ActiveSupport::Concern
45
38
 
46
- def elastify_create
47
- run_callbacks(:elastify_sync) do
48
- if not self.elastify_serialized_document.blank?
49
- run_callbacks(:elastify_create) do
50
- ElasticSearchHelper::Document.new(self.elastify_options).create(self.elastify_serialized_document)
51
- end
39
+ def elastify_create
40
+ run_callbacks(:elastify_sync) do
41
+ if not self.elastify_serialized_document.blank?
42
+ run_callbacks(:elastify_create) do
43
+ Elastify::Helpers::ElasticSearch::Document.new(self.class.elastify_options).create(self.elastify_serialized_document)
52
44
  end
53
45
  end
54
46
  end
47
+ end
55
48
 
56
- def elastify_update
57
- run_callbacks(:elastify_sync) do
58
- if not self.elastify_serialized_document.blank?
59
- run_callbacks(:elastify_update) do
60
- ElasticSearchHelper::Document.new(self.elastify_options).update(self.elastify_serialized_document)
61
- end
49
+ def elastify_update
50
+ run_callbacks(:elastify_sync) do
51
+ if not self.elastify_serialized_document.blank?
52
+ run_callbacks(:elastify_update) do
53
+ Elastify::Helpers::ElasticSearch::Document.new(self.class.elastify_options).update(self.elastify_serialized_document)
62
54
  end
63
55
  end
64
56
  end
57
+ end
65
58
 
66
- def elastify_destroy
67
- run_callbacks(:elastify_sync) do
68
- if not self.elastify_serialized_document.blank?
69
- run_callbacks(:elastify_destroy) do
70
- ElasticSearchHelper::Document.new(self.elastify_options).destroy(self.elastify_serialized_document)
71
- end
59
+ def elastify_destroy
60
+ run_callbacks(:elastify_sync) do
61
+ if not self.elastify_serialized_document.blank?
62
+ run_callbacks(:elastify_destroy) do
63
+ Elastify::Helpers::ElasticSearch::Document.new(self.class.elastify_options).destroy(self.elastify_serialized_document)
72
64
  end
73
65
  end
74
66
  end
75
-
76
- included do
77
- define_model_callbacks :elastify_create
78
- define_model_callbacks :elastify_update
79
- define_model_callbacks :elastify_destroy
80
- define_model_callbacks :elastify_sync
81
- # define_model_callbacks :elastify_serialize
67
+ end
82
68
 
83
- after_commit on: :create do |item|
84
- item.elastify_create
85
- end
69
+ included do
70
+ define_model_callbacks :elastify_create
71
+ define_model_callbacks :elastify_update
72
+ define_model_callbacks :elastify_destroy
73
+ define_model_callbacks :elastify_sync
74
+ # define_model_callbacks :elastify_serialize
86
75
 
87
- after_commit on: :update do |item|
88
- item.elastify_update
89
- end
76
+ after_commit on: :create do |item|
77
+ item.elastify_create
78
+ end
90
79
 
91
- after_commit on: :destroy do |item|
92
- item.elastify_destroy
93
- end
80
+ after_commit on: :update do |item|
81
+ item.elastify_update
82
+ end
94
83
 
95
- before_elastify_sync do |item|
96
- item.elastify_serialized_document = self.elastify_model_block.call(item)
97
- end
98
- end
84
+ after_commit on: :destroy do |item|
85
+ item.elastify_destroy
86
+ end
99
87
 
100
- module ClassMethods
101
- def elastify_search(dsl: nil, scroll_timer: "1m")
102
- return ElasticSearchHelper::Document.new(self.elastify_options).search(dsl, scroll_timer)
103
- end
104
-
105
- def elastify_scroll(scroll_id: nil, scroll_timer: "1m")
106
- return ElasticSearchHelper::Document.new(self.elastify_options).scroll(scroll_id, scroll_timer)
88
+ before_elastify_sync do |item|
89
+ if self.class.elastify_options[:encode].present?
90
+ encoder = self.class.elastify_options[:encode]
91
+ elsif self.respond_to?(:to_serial)
92
+ encoder = Proc.new { |item| next item.to_serial }
93
+ else
94
+ encoder = Proc.new { |item| next item.serializable_hash }
107
95
  end
96
+ item.elastify_serialized_document = encoder.call(item)
108
97
  end
109
98
  end
110
-
111
99
  end
112
- end
113
-
114
- ActiveRecord::Base.send(:include, Elastify::ActiveRecordExtensions)
100
+ end
@@ -0,0 +1,28 @@
1
+ module Elastify
2
+ module Configurators
3
+ class Model
4
+
5
+ attr_accessor :opt_index, :opt_type, :opt_mapping, :opt_encode, :opt_decode
6
+
7
+ def index(index)
8
+ @opt_index = index
9
+ end
10
+
11
+ def type(type)
12
+ @opt_type = type
13
+ end
14
+
15
+ def mapping(&block)
16
+ @opt_mapping = block.call() if block_given?
17
+ end
18
+
19
+ def encode(&block)
20
+ @opt_encode = block if block_given?
21
+ end
22
+
23
+ def decode(&block)
24
+ @opt_decode = block if block_given?
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module Elastify
2
+ module Errors
3
+ class BadRequest < ::Elastify::Errors::Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Elastify
2
+ module Errors
3
+ class Base < ::StandardError
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Elastify
2
+ module Errors
3
+ class Connection < Elastify::Errors::Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,69 @@
1
+ module Elastify
2
+ module Helpers
3
+ module ElasticSearch
4
+ class Connector
5
+ def self.create(options, data)
6
+ if data.blank?
7
+ raise :elastify__create__required_data
8
+ end
9
+ if data[:id].blank?
10
+ raise :elastify__create__required_data_id
11
+ end
12
+ url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
13
+ response = JSON.parse(RestClient.put(url, data.to_json, {}))
14
+ end
15
+ def self.update options, data
16
+ if data.blank?
17
+ raise :elastify__update__required_data
18
+ end
19
+ if data[:id].blank?
20
+ raise :elastify__update__required_data_id
21
+ end
22
+ url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
23
+ response = JSON.parse(RestClient.put(url, data.to_json, {})).to_hash
24
+ end
25
+ def self.destroy options, data
26
+ if data.blank?
27
+ raise :elastify__delete__required_data
28
+ end
29
+ if data[:id].blank?
30
+ raise :elastify__delete__required_data_id
31
+ end
32
+ url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
33
+ response = JSON.parse(RestClient.delete(url)).to_hash
34
+ end
35
+ def self.search options, dsl, scroll_timer
36
+ if dsl.blank?
37
+ raise :elastify__search__required_dsl
38
+ end
39
+ url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/_search"
40
+ url += "?scroll=#{scroll_timer}" if scroll_timer.present?
41
+ puts url
42
+ response = Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
43
+ end
44
+ def self.scroll options, scroll_id, scroll_timer
45
+ if scroll_id.blank?
46
+ raise :elastify__search__required_scroll_id
47
+ end
48
+ url = "#{options[:base_url]}/_search/scroll"
49
+ dsl = { scroll: scroll_timer, scroll_id: scroll_id }
50
+ puts dsl.to_json
51
+ response = Elastify::Helpers::ElasticSearch::SearchResultCollection.new(RestClient.post(url, dsl.to_json, {}), options)
52
+ end
53
+ def self.create_index options
54
+ url = "#{options[:base_url]}/#{options[:index]}"
55
+ response = JSON.parse(RestClient.put(url, {}.to_json, {})).to_hash
56
+ end
57
+ def self.destroy_index options
58
+ url = "#{options[:base_url]}/#{options[:index]}"
59
+ response = JSON.parse(RestClient.delete(url)).to_hash
60
+ end
61
+ def self.create_mapping options
62
+ url = "#{options[:base_url]}/#{options[:index]}/_mappings/#{options[:type]}"
63
+ puts options[:map]
64
+ response = JSON.parse(RestClient.put(url, options[:map].squish, {})).to_hash
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,27 @@
1
+ module Elastify
2
+ module Helpers
3
+ module ElasticSearch
4
+ class Document
5
+ cattr_accessor :options
6
+ def initialize(options = {})
7
+ @options = options
8
+ end
9
+ def create(model)
10
+ Connector.create(@options, model)
11
+ end
12
+ def update(model)
13
+ Connector.update(@options, model)
14
+ end
15
+ def destroy(model)
16
+ Connector.destroy(@options, model)
17
+ end
18
+ def search(dsl, scroll_timer = nil)
19
+ Connector.search(@options, dsl, scroll_timer)
20
+ end
21
+ def scroll(scroll_id, scroll_timer = nil)
22
+ Connector.scroll(@options, scroll_id, scroll_timer)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ module Elastify
2
+ module Helpers
3
+ module ElasticSearch
4
+ class SearchResult
5
+ attr_accessor :index, :type, :id, :score, :source,
6
+ :elastify_options
7
+
8
+ def initialize(elasticsearch_search_result_hit, elastify_options)
9
+ @index = elasticsearch_search_result_hit["_index"]
10
+ @type = elasticsearch_search_result_hit["_type"]
11
+ @id = elasticsearch_search_result_hit["_id"]
12
+ @source = elasticsearch_search_result_hit["_source"]
13
+ @elastify_options = elastify_options
14
+ end
15
+
16
+ def decode
17
+ data = {}
18
+ if @elastify_options[:decode]
19
+ data = @elastify_options[:decode].call(@source)
20
+ end
21
+ return data
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ module Elastify
2
+ module Helpers
3
+ module ElasticSearch
4
+ class SearchResultCollection
5
+ attr_accessor :scroll_id, :took, :timed_out, :shards_total, :shards_successful, :shards_failed, :hits_total, :hits_maxscore, :hits,
6
+ :elastify_options
7
+
8
+ def initialize(elasticsearch_search_result, elastify_options)
9
+ esr = JSON.parse(elasticsearch_search_result)
10
+ @scroll_id = esr["_scroll_id"]
11
+ @took = esr["took"]
12
+ @timed_out = esr["timed_out"]
13
+ @shards_total = esr["_shards"]["total"]
14
+ @shards_successful = esr["_shards"]["successful"]
15
+ @shards_failed = esr["_shards"]["failed"]
16
+ @hits_total = esr["hits"]["total"]
17
+ @hits_maxscore = esr["hits"]["maxscore"]
18
+ @hits = esr["hits"]["hits"].map{ |hit| Elastify::Helpers::ElasticSearch::SearchResult.new(hit, elastify_options) }
19
+ @elastify_options = elastify_options
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Elastify
2
- VERSION = "0.1.5"
2
+ VERSION = '0.2.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Bortolotti Ribeiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-17 00:00:00.000000000 Z
11
+ date: 2017-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -174,7 +174,14 @@ files:
174
174
  - elastify.gemspec
175
175
  - lib/elastify.rb
176
176
  - lib/elastify/active_record_extensions.rb
177
- - lib/elastify/elastic_search_helper.rb
177
+ - lib/elastify/configurators/model.rb
178
+ - lib/elastify/errors/bad_request.rb
179
+ - lib/elastify/errors/base.rb
180
+ - lib/elastify/errors/connection.rb
181
+ - lib/elastify/helpers/elastic_search/connector.rb
182
+ - lib/elastify/helpers/elastic_search/document.rb
183
+ - lib/elastify/helpers/elastic_search/search_result.rb
184
+ - lib/elastify/helpers/elastic_search/search_result_collection.rb
178
185
  - lib/elastify/version.rb
179
186
  homepage: https://github.com/brunobortolotti/elastify.git
180
187
  licenses:
@@ -1,112 +0,0 @@
1
- module Elastify
2
- module ElasticSearchHelper
3
- class Document
4
- cattr_accessor :options
5
- def initialize options = {}
6
- self.options = options
7
- end
8
- def create model
9
- Connector.create(self.options, model)
10
- end
11
- def update model
12
- Connector.update(self.options, model)
13
- end
14
- def destroy model
15
- Connector.destroy(self.options, model)
16
- end
17
- def search dsl, scroll_timer = nil
18
- Connector.search(self.options, dsl, scroll_timer)
19
- end
20
- def scroll scroll_id, scroll_timer = nil
21
- Connector.scroll(self.options, scroll_id, scroll_timer)
22
- end
23
- end
24
- class Connector
25
- def self.create(options, data)
26
- if data.blank?
27
- raise :elastify__create__required_data
28
- end
29
- if data[:id].blank?
30
- raise :elastify__create__required_data_id
31
- end
32
- url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
33
- response = JSON.parse(RestClient.put(url, data.to_json, {}))
34
- end
35
- def self.update options, data
36
- if data.blank?
37
- raise :elastify__update__required_data
38
- end
39
- if data[:id].blank?
40
- raise :elastify__update__required_data_id
41
- end
42
- url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
43
- response = JSON.parse(RestClient.put(url, data.to_json, {})).to_hash
44
- end
45
- def self.destroy options, data
46
- if data.blank?
47
- raise :elastify__delete__required_data
48
- end
49
- if data[:id].blank?
50
- raise :elastify__delete__required_data_id
51
- end
52
- url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/#{data[:id]}"
53
- response = JSON.parse(RestClient.delete(url)).to_hash
54
- end
55
- def self.search options, dsl, scroll_timer
56
- if dsl.blank?
57
- raise :elastify__search__required_dsl
58
- end
59
- url = "#{options[:base_url]}/#{options[:index]}/#{options[:type]}/_search"
60
- url += "?scroll=#{scroll_timer}" if scroll_timer.present?
61
- puts url
62
- response = SearchResultSet.new(RestClient.post(url, dsl.to_json, {}))
63
- end
64
- def self.scroll options, scroll_id, scroll_timer
65
- if scroll_id.blank?
66
- raise :elastify__search__required_scroll_id
67
- end
68
- url = "#{options[:base_url]}/_search/scroll"
69
- dsl = { scroll: scroll_timer, scroll_id: scroll_id }
70
- puts dsl.to_json
71
- response = SearchResultSet.new(RestClient.post(url, dsl.to_json, {}))
72
- end
73
- def self.create_index options
74
- url = "#{options[:base_url]}/#{options[:index]}"
75
- response = JSON.parse(RestClient.put(url, {}.to_json, {})).to_hash
76
- end
77
- def self.destroy_index options
78
- url = "#{options[:base_url]}/#{options[:index]}"
79
- response = JSON.parse(RestClient.delete(url)).to_hash
80
- end
81
- def self.create_mapping options
82
- url = "#{options[:base_url]}/#{options[:index]}/_mappings/#{options[:type]}"
83
- puts options[:map]
84
- response = JSON.parse(RestClient.put(url, options[:map].squish, {})).to_hash
85
- end
86
- end
87
- class SearchResultSet
88
- attr_accessor :scroll_id, :took, :timed_out, :shards_total, :shards_successful, :shards_failed, :hits_total, :hits_maxscore, :hits
89
- def initialize elasticsearch_search_result
90
- esr = JSON.parse(elasticsearch_search_result)
91
- self.scroll_id = esr["_scroll_id"]
92
- self.took = esr["took"]
93
- self.timed_out = esr["timed_out"]
94
- self.shards_total = esr["_shards"]["total"]
95
- self.shards_successful = esr["_shards"]["successful"]
96
- self.shards_failed = esr["_shards"]["failed"]
97
- self.hits_total = esr["hits"]["total"]
98
- self.hits_maxscore = esr["hits"]["maxscore"]
99
- self.hits = esr["hits"]["hits"].map{ |hit| SearchResult.new(hit) }
100
- end
101
- end
102
- class SearchResult
103
- attr_accessor :index, :type, :id, :score, :source
104
- def initialize elasticsearch_search_result_hit
105
- self.index = elasticsearch_search_result_hit["_index"]
106
- self.type = elasticsearch_search_result_hit["_type"]
107
- self.id = elasticsearch_search_result_hit["_id"]
108
- self.source = elasticsearch_search_result_hit["_source"]
109
- end
110
- end
111
- end
112
- end