indices 0.0.1 → 4.0.0.1

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.
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
@@ -1,14 +0,0 @@
1
- module Indices
2
- module Dsl
3
- class Mappings < Api
4
-
5
- def properties(*names)
6
- @parent[:properties] ||= {}
7
- names.each do |name|
8
- @parent[:properties][name] = Indices.configuration.mappings[name]
9
- end
10
- end
11
-
12
- end
13
- end
14
- end
@@ -1,25 +0,0 @@
1
- module Indices
2
- module Dsl
3
- class Search < Api
4
-
5
- private
6
-
7
- def add_block(name, args, options)
8
- if %i(functions must must_not should).include?(name)
9
- @parent[name] = []
10
- else
11
- super
12
- end
13
- end
14
-
15
- def add_argument(name, args, options)
16
- if name == :query && args.first.is_a?(Symbol)
17
- @parent[name] = Indices[args.first].search(options).query[:query]
18
- else
19
- super
20
- end
21
- end
22
-
23
- end
24
- end
25
- end
@@ -1,17 +0,0 @@
1
- module Indices
2
- module Dsl
3
- class Serializer < Api
4
-
5
- def set(object, *names)
6
- names.each do |name|
7
- send name, object.send(name)
8
- end
9
- end
10
-
11
- def transliterate(value)
12
- ActiveSupport::Inflector.transliterate value
13
- end
14
-
15
- end
16
- end
17
- end
@@ -1,149 +0,0 @@
1
- module Indices
2
- class Index
3
- include Comparable
4
-
5
- attr_reader :name, :type, :options
6
-
7
- def initialize(name, options={})
8
- @name = name
9
- @type = @name.to_s.singularize
10
- @options = options
11
- make_indexable
12
- end
13
-
14
- def mappings
15
- @mappings ||= Dsl::Mappings.new(&options[:mappings]).to_h
16
- end
17
-
18
- def model
19
- (options[:class_name] || type.classify).constantize
20
- end
21
-
22
- def has_parent?
23
- mappings.has_key? :_parent
24
- end
25
-
26
- def <=>(other)
27
- if has_parent? && other.has_parent?
28
- 0
29
- elsif other.has_parent?
30
- 1
31
- else
32
- -1
33
- end
34
- end
35
-
36
- def any?(*args)
37
- search(*args).count > 0
38
- end
39
-
40
- def none?(*args)
41
- !any?(*args)
42
- end
43
-
44
- def search(*args)
45
- Collection.new self, *args, &options[:search]
46
- end
47
-
48
- def raw_search(query)
49
- client.search(
50
- index: namespace,
51
- type: type,
52
- body: query
53
- )
54
- end
55
-
56
- def exists?(record)
57
- client.exists?(
58
- with_parent(
59
- record,
60
- index: namespace,
61
- type: type,
62
- id: record.id
63
- )
64
- )
65
- end
66
-
67
- def index(record)
68
- client.create(
69
- with_parent(
70
- record,
71
- index: namespace,
72
- type: type,
73
- id: record.id,
74
- body: serialize(record)
75
- )
76
- )
77
- end
78
-
79
- def reindex(record)
80
- client.bulk(
81
- index: namespace,
82
- type: type,
83
- body: [
84
- { delete: with_parent(record, _id: record.id) },
85
- { index: with_parent(record, _id: record.id, data: serialize(record)) }
86
- ]
87
- )
88
- end
89
-
90
- def unindex(record)
91
- client.delete(
92
- with_parent(
93
- record,
94
- index: namespace,
95
- type: type,
96
- id: record.id
97
- )
98
- )
99
- end
100
-
101
- def build
102
- client.indices.put_mapping(
103
- index: namespace,
104
- type: type,
105
- body: mappings
106
- )
107
- model.find_in_batches do |records|
108
- client.bulk(
109
- index: namespace,
110
- type: type,
111
- body: records.map do |record|
112
- { index: with_parent(record, _id: record.id, data: serialize(record)) }
113
- end
114
- )
115
- end
116
- end
117
-
118
- private
119
-
120
- %i(client namespace).each do |name|
121
- define_method name do
122
- Indices.send name
123
- end
124
- end
125
-
126
- def with_parent(record, hash)
127
- if has_parent?
128
- hash.merge parent: record.send("#{mappings[:_parent][:type].singularize}_id")
129
- else
130
- hash
131
- end
132
- end
133
-
134
- def serialize(record)
135
- Dsl::Serializer.new(record, &options[:serializer]).to_h
136
- end
137
-
138
- def make_indexable
139
- index = self
140
- model.class_eval do
141
- include Indices::Concern
142
- define_singleton_method :index do
143
- index
144
- end
145
- end
146
- end
147
-
148
- end
149
- end
@@ -1,33 +0,0 @@
1
- module Indices
2
- module Pagination
3
-
4
- def total_pages
5
- @total_pages ||= [(total_count.to_f / page_length).ceil, 1].max
6
- end
7
-
8
- def previous_page
9
- @previous_page ||= (current_page > 1 ? (current_page - 1) : nil)
10
- end
11
-
12
- def next_page
13
- @next_page ||= (current_page < total_pages ? (current_page + 1) : nil)
14
- end
15
-
16
- def first_page
17
- 1
18
- end
19
-
20
- def last_page
21
- total_pages
22
- end
23
-
24
- def out_of_bounds?
25
- @out_of_bounds ||= (current_page > total_pages || current_page < first_page)
26
- end
27
-
28
- def total_count
29
- @total_count ||= (response['hits']['total'].to_i - padding)
30
- end
31
-
32
- end
33
- end
@@ -1,17 +0,0 @@
1
- module Indices
2
- class Proxy
3
-
4
- def initialize(name, options={}, &block)
5
- @options = options
6
- instance_eval &block
7
- Indices.add name, @options
8
- end
9
-
10
- %i(mappings serializer search).each do |name|
11
- define_method name do |&block|
12
- @options[name] = block
13
- end
14
- end
15
-
16
- end
17
- end
@@ -1,15 +0,0 @@
1
- module Indices
2
- class Railtie < Rails::Railtie
3
-
4
- initializer :indices do
5
- Dir[Rails.root.join('app/indices/*')].each do |index|
6
- load index
7
- end
8
- end
9
-
10
- rake_tasks do
11
- load 'tasks/indices.rake'
12
- end
13
-
14
- end
15
- end
@@ -1,5 +0,0 @@
1
- module Indices
2
-
3
- VERSION = '0.0.1'
4
-
5
- end
@@ -1,11 +0,0 @@
1
- namespace :indices do
2
- desc 'Build all indices.'
3
- task build: :environment do
4
- Indices.build
5
- end
6
-
7
- desc 'Rebuild all indices.'
8
- task rebuild: :environment do
9
- Indices.rebuild
10
- end
11
- end
@@ -1,92 +0,0 @@
1
- require 'test_helper'
2
-
3
- class DslTest < ActiveSupport::TestCase
4
-
5
- test 'search' do
6
- seed = rand
7
- days = [1,2,3]
8
- opens_at = Time.now.to_i
9
- closes_at = (Time.now + 1.day).to_i
10
- assert_equal(
11
- {
12
- query: {
13
- function_score: {
14
- functions: [
15
- { random_score: { seed: seed } },
16
- {
17
- weight: 400,
18
- filter: {
19
- bool: {
20
- must: [
21
- { term: {} }
22
- ]
23
- }
24
- }
25
- }
26
- ]
27
- },
28
- query: {
29
- filtered: {
30
- filter: {
31
- bool: {
32
- should: [
33
- { term: { :'schedules.days' => days } }
34
- ],
35
- must: [
36
- { range: { :'schedules.opens_at' => { lte: opens_at } } }
37
- ],
38
- must_not: []
39
- }
40
- }
41
- }
42
- }
43
- },
44
- size: {}
45
- },
46
- build_request do
47
- query do
48
- function_score do
49
- functions do
50
- random_score do
51
- seed seed
52
- end
53
- filter weight: 400 do
54
- bool do
55
- must do
56
- term
57
- end
58
- end
59
- end
60
- end
61
- end
62
- query do
63
- filtered do
64
- filter do
65
- bool do
66
- should do
67
- term 'schedules.days' => days
68
- end
69
- must do
70
- range 'schedules.opens_at' do
71
- lte opens_at
72
- end
73
- end
74
- must_not do
75
- end
76
- end
77
- end
78
- end
79
- end
80
- end
81
- size
82
- end
83
- )
84
- end
85
-
86
- private
87
-
88
- def build_request(&block)
89
- Indices::Dsl::Search.new(&block).to_h
90
- end
91
-
92
- end
@@ -1,5 +0,0 @@
1
- # Add your own tasks in files placed in lib/tasks ending in .rake,
2
- # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
-
4
- require File.expand_path('../config/application', __FILE__)
5
- Rails.application.load_tasks
@@ -1,13 +0,0 @@
1
- // This is a manifest file that'll be compiled into application.js, which will include all the files
2
- // listed below.
3
- //
4
- // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
- //
7
- // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file.
9
- //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
12
- //
13
- //= require_tree .
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any styles
10
- * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
- * file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
@@ -1,5 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- # Prevent CSRF attacks by raising an exception.
3
- # For APIs, you may want to use :null_session instead.
4
- protect_from_forgery with: :exception
5
- end
@@ -1,2 +0,0 @@
1
- module ApplicationHelper
2
- end
@@ -1,51 +0,0 @@
1
- Indices.define :products do
2
-
3
- mappings do
4
- properties :name, :category, :shop_id, :price, :currency, :product_suggestions
5
- _parent type: 'shop'
6
- end
7
-
8
- serializer do |record|
9
- set record, :name, :category, :shop_id, :price, :currency
10
- product_suggestions do
11
- input [record.name, transliterate(record.name)].uniq
12
- output record.name
13
- context do
14
- shop_id [record.shop_id.to_s, 'all'].compact
15
- end
16
- end
17
- end
18
-
19
- search do |*args|
20
- options = args.extract_options!
21
- shop = options[:shop]
22
- term = args.first
23
- query do
24
- filtered do
25
- filter do
26
- bool do
27
- must do
28
- if shop
29
- term do
30
- _parent shop.id
31
- end
32
- end
33
- end
34
- end
35
- end
36
- query do
37
- if term.present?
38
- multi_match do
39
- query term
40
- type 'phrase_prefix'
41
- fields %w(name category)
42
- end
43
- else
44
- match_all
45
- end
46
- end
47
- end
48
- end
49
- end
50
-
51
- end