indices 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 +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +245 -0
  4. data/Rakefile +19 -0
  5. data/lib/generators/indices/index_generator.rb +15 -0
  6. data/lib/generators/indices/install_generator.rb +15 -0
  7. data/lib/generators/indices/templates/index.rb +12 -0
  8. data/lib/generators/indices/templates/initializer.rb +10 -0
  9. data/lib/indices.rb +102 -0
  10. data/lib/indices/collection.rb +195 -0
  11. data/lib/indices/concern.rb +23 -0
  12. data/lib/indices/configuration.rb +39 -0
  13. data/lib/indices/dsl/api.rb +85 -0
  14. data/lib/indices/dsl/mappings.rb +14 -0
  15. data/lib/indices/dsl/search.rb +25 -0
  16. data/lib/indices/dsl/serializer.rb +17 -0
  17. data/lib/indices/index.rb +149 -0
  18. data/lib/indices/pagination.rb +33 -0
  19. data/lib/indices/proxy.rb +17 -0
  20. data/lib/indices/railtie.rb +15 -0
  21. data/lib/indices/version.rb +5 -0
  22. data/lib/tasks/indices.rake +11 -0
  23. data/test/dsl_test.rb +92 -0
  24. data/test/dummy/Rakefile +5 -0
  25. data/test/dummy/app/assets/javascripts/application.js +13 -0
  26. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  27. data/test/dummy/app/controllers/application_controller.rb +5 -0
  28. data/test/dummy/app/helpers/application_helper.rb +2 -0
  29. data/test/dummy/app/indices/products_index.rb +51 -0
  30. data/test/dummy/app/indices/shops_index.rb +28 -0
  31. data/test/dummy/app/models/product.rb +5 -0
  32. data/test/dummy/app/models/shop.rb +5 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +12 -0
  34. data/test/dummy/bin/bundle +4 -0
  35. data/test/dummy/bin/rails +5 -0
  36. data/test/dummy/bin/rake +5 -0
  37. data/test/dummy/bin/setup +30 -0
  38. data/test/dummy/config.ru +4 -0
  39. data/test/dummy/config/application.rb +25 -0
  40. data/test/dummy/config/boot.rb +5 -0
  41. data/test/dummy/config/database.yml +7 -0
  42. data/test/dummy/config/database.yml.travis +3 -0
  43. data/test/dummy/config/environment.rb +5 -0
  44. data/test/dummy/config/environments/development.rb +41 -0
  45. data/test/dummy/config/environments/production.rb +79 -0
  46. data/test/dummy/config/environments/test.rb +42 -0
  47. data/test/dummy/config/initializers/assets.rb +11 -0
  48. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  50. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  51. data/test/dummy/config/initializers/indices.rb +67 -0
  52. data/test/dummy/config/initializers/inflections.rb +16 -0
  53. data/test/dummy/config/initializers/mime_types.rb +4 -0
  54. data/test/dummy/config/initializers/session_store.rb +3 -0
  55. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  56. data/test/dummy/config/locales/en.yml +23 -0
  57. data/test/dummy/config/routes.rb +56 -0
  58. data/test/dummy/config/secrets.yml +22 -0
  59. data/test/dummy/db/migrate/20161104164148_create_products.rb +12 -0
  60. data/test/dummy/db/migrate/20161104182219_create_shops.rb +7 -0
  61. data/test/dummy/db/schema.rb +36 -0
  62. data/test/dummy/log/development.log +156 -0
  63. data/test/dummy/log/test.log +10249 -0
  64. data/test/dummy/public/404.html +67 -0
  65. data/test/dummy/public/422.html +67 -0
  66. data/test/dummy/public/500.html +66 -0
  67. data/test/dummy/public/favicon.ico +0 -0
  68. data/test/generators_test.rb +25 -0
  69. data/test/indices_test.rb +46 -0
  70. data/test/record_test.rb +25 -0
  71. data/test/search_test.rb +164 -0
  72. data/test/tasks_test.rb +25 -0
  73. data/test/test_helper.rb +14 -0
  74. metadata +230 -0
@@ -0,0 +1,33 @@
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
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,5 @@
1
+ module Indices
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
@@ -0,0 +1,11 @@
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
@@ -0,0 +1,92 @@
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
@@ -0,0 +1,5 @@
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
@@ -0,0 +1,13 @@
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 .
@@ -0,0 +1,15 @@
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
+ */
@@ -0,0 +1,5 @@
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
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,51 @@
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
@@ -0,0 +1,28 @@
1
+ Indices.define :other, class_name: 'Shop' do
2
+
3
+ mappings do
4
+ properties :name
5
+ end
6
+
7
+ serializer do |record|
8
+ set record, :name
9
+ end
10
+
11
+ search do |*args|
12
+ options = args.extract_options!
13
+ term = args.first
14
+ query do
15
+ if term.present?
16
+ match do
17
+ name do
18
+ query term
19
+ type 'phrase_prefix'
20
+ end
21
+ end
22
+ else
23
+ match_all
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,5 @@
1
+ class Product < ActiveRecord::Base
2
+
3
+ belongs_to :shop
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ class Shop < ActiveRecord::Base
2
+
3
+ has_many :products
4
+
5
+ end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <%= yield %>
11
+ </body>
12
+ </html>
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
4
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
4
+ require_relative '../config/boot'
5
+ require 'rails/commands'
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../config/boot'
4
+ require 'rake'
5
+ Rake.application.run
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+
5
+ # path to your application root.
6
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
7
+
8
+ Dir.chdir APP_ROOT do
9
+ # This script is a starting point to setup your application.
10
+ # Add necessary setup steps to this file:
11
+
12
+ puts "== Installing dependencies =="
13
+ system 'gem install bundler --conservative'
14
+ system 'bundle check || bundle install'
15
+
16
+ # puts "\n== Copying sample files =="
17
+ # unless File.exist?('config/database.yml')
18
+ # system 'cp config/database.yml.sample config/database.yml'
19
+ # end
20
+
21
+ puts "\n== Preparing database =="
22
+ system 'bin/rake db:setup'
23
+
24
+ puts "\n== Removing old logs and tempfiles =="
25
+ system 'rm -f log/*'
26
+ system 'rm -rf tmp/cache'
27
+
28
+ puts "\n== Restarting application server =="
29
+ system 'touch tmp/restart.txt'
30
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require 'indices'
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+
22
+ # Do not swallow errors in after_commit/after_rollback callbacks.
23
+ config.active_record.raise_in_transactional_callbacks = true
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,7 @@
1
+ development:
2
+ adapter: postgresql
3
+ database: indices_development
4
+
5
+ test:
6
+ adapter: postgresql
7
+ database: indices_test
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: <%= 'jdbc' if RUBY_ENGINE == 'jruby' %>postgresql
3
+ database: travis
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!