indexes 4.0.0.2 → 4.0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. metadata +11 -198
  3. data/MIT-LICENSE +0 -20
  4. data/README.md +0 -265
  5. data/Rakefile +0 -19
  6. data/lib/generators/index/index_generator.rb +0 -15
  7. data/lib/generators/index/templates/index.rb +0 -12
  8. data/lib/generators/indexes/install/install_generator.rb +0 -15
  9. data/lib/generators/indexes/install/templates/initializer.rb +0 -10
  10. data/lib/indexes.rb +0 -85
  11. data/lib/indexes/collection.rb +0 -186
  12. data/lib/indexes/concern.rb +0 -23
  13. data/lib/indexes/configuration.rb +0 -39
  14. data/lib/indexes/definitions.rb +0 -24
  15. data/lib/indexes/dsl/api.rb +0 -90
  16. data/lib/indexes/dsl/mappings.rb +0 -14
  17. data/lib/indexes/dsl/search.rb +0 -27
  18. data/lib/indexes/dsl/serialization.rb +0 -17
  19. data/lib/indexes/index.rb +0 -147
  20. data/lib/indexes/pagination.rb +0 -33
  21. data/lib/indexes/proxy.rb +0 -17
  22. data/lib/indexes/railtie.rb +0 -15
  23. data/lib/indexes/version.rb +0 -5
  24. data/lib/tasks/indexes.rake +0 -11
  25. data/test/dsl_test.rb +0 -128
  26. data/test/dummy/Rakefile +0 -5
  27. data/test/dummy/app/assets/javascripts/application.js +0 -13
  28. data/test/dummy/app/assets/stylesheets/application.css +0 -15
  29. data/test/dummy/app/controllers/application_controller.rb +0 -5
  30. data/test/dummy/app/helpers/application_helper.rb +0 -2
  31. data/test/dummy/app/indexes/products_index.rb +0 -51
  32. data/test/dummy/app/indexes/shops_index.rb +0 -28
  33. data/test/dummy/app/models/product.rb +0 -5
  34. data/test/dummy/app/models/shop.rb +0 -5
  35. data/test/dummy/app/views/layouts/application.html.erb +0 -12
  36. data/test/dummy/bin/bundle +0 -4
  37. data/test/dummy/bin/rails +0 -5
  38. data/test/dummy/bin/rake +0 -5
  39. data/test/dummy/bin/setup +0 -30
  40. data/test/dummy/config.ru +0 -4
  41. data/test/dummy/config/application.rb +0 -25
  42. data/test/dummy/config/boot.rb +0 -5
  43. data/test/dummy/config/database.yml +0 -7
  44. data/test/dummy/config/database.yml.travis +0 -3
  45. data/test/dummy/config/environment.rb +0 -5
  46. data/test/dummy/config/environments/development.rb +0 -41
  47. data/test/dummy/config/environments/production.rb +0 -79
  48. data/test/dummy/config/environments/test.rb +0 -42
  49. data/test/dummy/config/initializers/assets.rb +0 -11
  50. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  51. data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  52. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  53. data/test/dummy/config/initializers/indexes.rb +0 -67
  54. data/test/dummy/config/initializers/inflections.rb +0 -16
  55. data/test/dummy/config/initializers/mime_types.rb +0 -4
  56. data/test/dummy/config/initializers/session_store.rb +0 -3
  57. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  58. data/test/dummy/config/locales/en.yml +0 -23
  59. data/test/dummy/config/routes.rb +0 -56
  60. data/test/dummy/config/secrets.yml +0 -22
  61. data/test/dummy/db/migrate/20161104164148_create_products.rb +0 -14
  62. data/test/dummy/db/migrate/20161104182219_create_shops.rb +0 -9
  63. data/test/dummy/db/schema.rb +0 -36
  64. data/test/dummy/log/development.log +0 -38
  65. data/test/dummy/log/test.log +0 -3643
  66. data/test/dummy/public/404.html +0 -61
  67. data/test/dummy/public/422.html +0 -61
  68. data/test/dummy/public/500.html +0 -60
  69. data/test/dummy/public/favicon.ico +0 -0
  70. data/test/generator_test.rb +0 -25
  71. data/test/index_test.rb +0 -46
  72. data/test/record_test.rb +0 -25
  73. data/test/search_test.rb +0 -164
  74. data/test/task_test.rb +0 -25
  75. data/test/test_helper.rb +0 -14
data/Rakefile DELETED
@@ -1,19 +0,0 @@
1
- begin
2
- require 'bundler/setup'
3
- rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
- end
6
-
7
- Bundler::GemHelper.install_tasks
8
-
9
- require 'rake/testtask'
10
-
11
- Rake::TestTask.new(:test) do |t|
12
- t.libs << 'lib'
13
- t.libs << 'test'
14
- t.pattern = 'test/**/*_test.rb'
15
- t.verbose = false
16
- t.warning = false
17
- end
18
-
19
- task default: :test
@@ -1,15 +0,0 @@
1
- require 'rails/generators'
2
-
3
- module Indexes
4
- module Generators
5
- class IndexGenerator < ::Rails::Generators::NamedBase
6
-
7
- source_root File.expand_path('../templates', __FILE__)
8
-
9
- def create_index_file
10
- template 'index.rb', "app/indexes/#{table_name}_index.rb"
11
- end
12
-
13
- end
14
- end
15
- end
@@ -1,12 +0,0 @@
1
- Indexes.define :<%= table_name %> do
2
-
3
- mappings do
4
- end
5
-
6
- serialization do |record|
7
- end
8
-
9
- search do |*args|
10
- end
11
-
12
- end
@@ -1,15 +0,0 @@
1
- require 'rails/generators'
2
-
3
- module Indexes
4
- module Generators
5
- class InstallGenerator < ::Rails::Generators::Base
6
-
7
- source_root File.expand_path('../templates', __FILE__)
8
-
9
- def create_initializer_file
10
- copy_file 'initializer.rb', 'config/initializers/indexes.rb'
11
- end
12
-
13
- end
14
- end
15
- end
@@ -1,10 +0,0 @@
1
- Indexes.configure do |config|
2
-
3
- config.hosts = %w(localhost:9200)
4
- config.log = true
5
- config.trace = true
6
-
7
- config.mappings do
8
- end
9
-
10
- end
@@ -1,85 +0,0 @@
1
- require 'indexes/dsl/api'
2
- require 'indexes/dsl/mappings'
3
- require 'indexes/dsl/search'
4
- require 'indexes/dsl/serialization'
5
- require 'indexes/collection'
6
- require 'indexes/concern'
7
- require 'indexes/configuration'
8
- require 'indexes/definitions'
9
- require 'indexes/index'
10
- require 'indexes/pagination'
11
- require 'indexes/proxy'
12
- require 'indexes/railtie'
13
- require 'indexes/version'
14
-
15
- module Indexes
16
- class << self
17
-
18
- delegate :any?, :none?, to: :registry
19
-
20
- def namespace
21
- "#{Rails.application.class.parent_name} #{Rails.env}".parameterize('_')
22
- end
23
-
24
- def client
25
- @client ||= begin
26
- require 'elasticsearch'
27
- Elasticsearch::Client.new(
28
- hosts: configuration.hosts,
29
- log: configuration.log,
30
- trace: configuration.trace
31
- )
32
- end
33
- end
34
-
35
- def configure
36
- yield configuration
37
- end
38
-
39
- def configuration
40
- @configuration ||= Configuration.new
41
- end
42
-
43
- def definitions
44
- @definitions ||= Definitions.new
45
- end
46
-
47
- def define(*args, &block)
48
- Proxy.new *args, &block
49
- end
50
-
51
- def build
52
- unless client.indices.exists?(index: namespace)
53
- client.indices.create(
54
- index: namespace,
55
- body: { settings: configuration.analysis }
56
- )
57
- end
58
- definitions.each &:build
59
- end
60
-
61
- def rebuild
62
- destroy
63
- build
64
- end
65
-
66
- def exist?(type)
67
- client.indices.exists? index: namespace, type: type
68
- end
69
-
70
- def destroy
71
- if client.indices.exists?(index: namespace)
72
- client.indices.delete index: namespace
73
- end
74
- end
75
-
76
- def suggest(*args)
77
- response = client.suggest(
78
- index: namespace,
79
- body: { suggestions: Dsl::Api.new(args, &configuration.suggestions).to_h }
80
- )
81
- response['suggestions'].first['options'].map &:symbolize_keys
82
- end
83
-
84
- end
85
- end
@@ -1,186 +0,0 @@
1
- module Indexes
2
- class Collection
3
- include Enumerable
4
-
5
- delegate :model, to: :index
6
- delegate :model_name, to: :model
7
- delegate :each, :map, :size, :length, :count, :[], :to_a, to: :records
8
-
9
- alias_method :to_ary, :to_a
10
-
11
- def initialize(index, args=[], options={}, &block)
12
- @loaded = false
13
- @index = index
14
- @args = args
15
- @options = options
16
- @block = block
17
- end
18
-
19
- def with(ids)
20
- chain with: ids
21
- end
22
-
23
- def without(ids)
24
- chain without: ids
25
- end
26
-
27
- def includes(*args)
28
- chain includes: args
29
- end
30
-
31
- def page(number, options={})
32
- length = page_option(options, :length, 10)
33
- padding = page_option(options, :padding, 0)
34
- current_page = [number.to_i, 1].max
35
- values = Module.new do
36
- define_method :page_length do
37
- length
38
- end
39
- define_method :padding do
40
- padding
41
- end
42
- define_method :current_page do
43
- current_page
44
- end
45
- end
46
- chain(
47
- Pagination,
48
- values,
49
- from: ((length * (current_page - 1)) + padding),
50
- size: length
51
- )
52
- end
53
-
54
- def order(options)
55
- mappings = Indexes.configuration.mappings
56
- values = []
57
- options.each do |property, direction|
58
- if block = Indexes.configuration.computed_sorts[property]
59
- values << Dsl::Api.new(direction, &block).to_h
60
- elsif property == :id
61
- values << { _uid: { order: direction } }
62
- elsif mappings.has_key?(property) && mappings[property][:type] == 'string'
63
- values << { "#{property}.raw" => { order: direction } }
64
- end
65
- end
66
- if values.any?
67
- chain sort: values
68
- else
69
- chain
70
- end
71
- end
72
-
73
- def response
74
- if @loaded == true
75
- @response
76
- else
77
- @loaded = true
78
- @response = index.raw_search(query)
79
- end
80
- end
81
-
82
- def query
83
- @query ||= begin
84
- pagination = options.slice(:from, :size, :sort)
85
- without_ids = fetch_ids(options[:without])
86
- body = Dsl::Search.new(args, &block).to_h[:query]
87
- request = Dsl::Search.new do
88
- if without_ids.any?
89
- query do
90
- filtered do
91
- filter do
92
- bool do
93
- must_not do
94
- without_ids.each do |id|
95
- term do
96
- _id id
97
- end
98
- end
99
- end
100
- end
101
- end
102
- query body
103
- end
104
- end
105
- else
106
- query body
107
- end
108
- %i(from size).each do |name|
109
- if pagination.has_key?(name)
110
- send name, pagination[name]
111
- end
112
- end
113
- if pagination.has_key?(:sort)
114
- sort pagination[:sort]
115
- else
116
- sort do
117
- _uid do
118
- order 'desc'
119
- end
120
- end
121
- end
122
- end
123
- request.to_h
124
- end
125
- end
126
-
127
- private
128
-
129
- attr_reader :index, :args, :options, :block
130
-
131
- def records
132
- @records ||= begin
133
- hit_ids = response['hits']['hits'].map{ |hit| hit['_id'].to_i }
134
- missing_ids = (fetch_ids(options[:with]) - hit_ids)
135
- if missing_ids.any?
136
- last_index = -(missing_ids.length + 1)
137
- ids = (missing_ids.sort.reverse + hit_ids.to(last_index))
138
- else
139
- ids = hit_ids
140
- end
141
- includes = options.fetch(:includes, [])
142
- index.model.includes(includes).where(id: ids).sort do |a,b|
143
- ids.index(a.id) <=> ids.index(b.id)
144
- end
145
- end
146
- end
147
-
148
- def page_option(source, name, default)
149
- source[name] || begin
150
- if Rails.configuration.cache_classes == false
151
- Rails.application.eager_load!
152
- end
153
- if defined?(Pagers)
154
- Pagers.configuration.send name
155
- else
156
- default
157
- end
158
- end
159
- end
160
-
161
- def fetch_ids(source)
162
- case source
163
- when Fixnum,String
164
- [source.to_i]
165
- when ActiveRecord::Base
166
- [source.id]
167
- when ActiveRecord::Relation
168
- source.ids
169
- when Array
170
- source.map{ |value| fetch_ids(value) }.flatten
171
- else
172
- []
173
- end
174
- end
175
-
176
- def chain(*extensions)
177
- overrides = extensions.extract_options!
178
- collection = Collection.new(index, args, options.merge(overrides), &block)
179
- extensions.each do |extension|
180
- collection.extend extension
181
- end
182
- collection
183
- end
184
-
185
- end
186
- end
@@ -1,23 +0,0 @@
1
- module Indexes
2
- module Concern
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- after_commit :index, on: :create
7
- after_commit :reindex, on: :update
8
- after_commit :unindex, on: :destroy
9
- end
10
-
11
- %i(index reindex unindex).each do |name|
12
- define_method name do
13
- self.class.index.send name, self
14
- end
15
- end
16
-
17
- module ClassMethods
18
-
19
- delegate :search, to: :index
20
-
21
- end
22
- end
23
- end
@@ -1,39 +0,0 @@
1
- module Indexes
2
- class Configuration
3
-
4
- attr_accessor :hosts, :log, :trace
5
-
6
- def computed_sorts
7
- @computed_sorts ||= {}
8
- end
9
-
10
- def mappings(&block)
11
- if block_given?
12
- @mappings = Dsl::Mappings.new(&block).to_h
13
- else
14
- @mappings
15
- end
16
- end
17
-
18
- def analysis(&block)
19
- if block_given?
20
- @analysis = { analysis: Dsl::Api.new(&block).to_h }
21
- else
22
- @analysis
23
- end
24
- end
25
-
26
- def suggestions(&block)
27
- if block_given?
28
- @suggestions = block
29
- else
30
- @suggestions
31
- end
32
- end
33
-
34
- def computed_sort(name, &block)
35
- self.computed_sorts[name] = block
36
- end
37
-
38
- end
39
- end
@@ -1,24 +0,0 @@
1
- module Indexes
2
- class Definitions
3
-
4
- def add(*args)
5
- index = Index.new(*args)
6
- registry[index.name] = index
7
- end
8
-
9
- def find(name)
10
- registry[name]
11
- end
12
-
13
- def each(&block)
14
- registry.values.sort.each &block
15
- end
16
-
17
- private
18
-
19
- def registry
20
- @registry ||= {}
21
- end
22
-
23
- end
24
- end
@@ -1,90 +0,0 @@
1
- module Indexes
2
- module Dsl
3
- class Api
4
-
5
- def initialize(args=[], parent={}, &block)
6
- @parent = parent
7
- instance_exec *args, &block
8
- end
9
-
10
- def method_missing(name, *args, &block)
11
- options = args.extract_options!
12
- name = name.to_sym
13
- if block_given?
14
- add_block name, args, options, &block
15
- elsif args.size > 0
16
- add_argument name, args, options
17
- elsif options.any?
18
- add_options name, options
19
- else
20
- add_empty name
21
- end
22
- end
23
-
24
- def to_h
25
- @parent
26
- end
27
-
28
- private
29
-
30
- def add_block(name, args, options, &block)
31
- case args.first
32
- when String,Symbol
33
- child = {}
34
- node = { args.first.to_sym => child }
35
- when Enumerable,ActiveRecord::Relation
36
- child = node = []
37
- else
38
- child = node = {}
39
- end
40
- case @parent
41
- when Array
42
- @parent << options.merge(name => node)
43
- when Hash
44
- @parent[name] = node
45
- end
46
- case args.first
47
- when Enumerable,ActiveRecord::Relation
48
- args.first.each do |arg|
49
- self.class.new [arg], child, &block
50
- end
51
- else
52
- self.class.new [], child, &block
53
- end
54
- end
55
-
56
- def add_argument(name, args, options)
57
- case @parent
58
- when Array
59
- @parent << { name => args.first }
60
- when Hash
61
- @parent[name] = args.first
62
- end
63
- end
64
-
65
- def add_options(name, options)
66
- options.symbolize_keys!
67
- case @parent
68
- when Array
69
- @parent << { name => options }
70
- when Hash
71
- if @parent.has_key?(name)
72
- @parent[name].merge! options
73
- else
74
- @parent[name] = options
75
- end
76
- end
77
- end
78
-
79
- def add_empty(name)
80
- case @parent
81
- when Array
82
- @parent << { name => {} }
83
- when Hash
84
- @parent[name] = {}
85
- end
86
- end
87
-
88
- end
89
- end
90
- end