indices 0.0.1 → 4.0.0.1

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