indexes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +245 -0
- data/Rakefile +19 -0
- data/lib/generators/indexes/index_generator.rb +15 -0
- data/lib/generators/indexes/install_generator.rb +15 -0
- data/lib/generators/indexes/templates/index.rb +12 -0
- data/lib/generators/indexes/templates/initializer.rb +10 -0
- data/lib/indexes.rb +102 -0
- data/lib/indexes/collection.rb +195 -0
- data/lib/indexes/concern.rb +23 -0
- data/lib/indexes/configuration.rb +39 -0
- data/lib/indexes/dsl/api.rb +85 -0
- data/lib/indexes/dsl/mappings.rb +14 -0
- data/lib/indexes/dsl/search.rb +25 -0
- data/lib/indexes/dsl/serializer.rb +17 -0
- data/lib/indexes/index.rb +149 -0
- data/lib/indexes/pagination.rb +33 -0
- data/lib/indexes/proxy.rb +17 -0
- data/lib/indexes/railtie.rb +15 -0
- data/lib/indexes/version.rb +5 -0
- data/lib/tasks/indexes.rake +11 -0
- data/test/dsl_test.rb +92 -0
- data/test/dummy/Rakefile +5 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/indexes/products_index.rb +51 -0
- data/test/dummy/app/indexes/shops_index.rb +28 -0
- data/test/dummy/app/models/product.rb +5 -0
- data/test/dummy/app/models/shop.rb +5 -0
- data/test/dummy/app/views/layouts/application.html.erb +12 -0
- data/test/dummy/bin/bundle +4 -0
- data/test/dummy/bin/rails +5 -0
- data/test/dummy/bin/rake +5 -0
- data/test/dummy/bin/setup +30 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +25 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +7 -0
- data/test/dummy/config/database.yml.travis +3 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/indexes.rb +67 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/migrate/20161104164148_create_products.rb +14 -0
- data/test/dummy/db/migrate/20161104182219_create_shops.rb +9 -0
- data/test/dummy/db/schema.rb +36 -0
- data/test/dummy/log/development.log +38 -0
- data/test/dummy/log/test.log +408 -0
- data/test/dummy/public/404.html +61 -0
- data/test/dummy/public/422.html +61 -0
- data/test/dummy/public/500.html +60 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/generators_test.rb +25 -0
- data/test/indexes_test.rb +46 -0
- data/test/record_test.rb +25 -0
- data/test/search_test.rb +164 -0
- data/test/tasks_test.rb +25 -0
- data/test/test_helper.rb +14 -0
- metadata +230 -0
@@ -0,0 +1,33 @@
|
|
1
|
+
module Indexes
|
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 Indexes
|
2
|
+
class Proxy
|
3
|
+
|
4
|
+
def initialize(name, options={}, &block)
|
5
|
+
@options = options
|
6
|
+
instance_eval &block
|
7
|
+
Indexes.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
|
data/test/dsl_test.rb
ADDED
@@ -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
|
+
Indexes::Dsl::Search.new(&block).to_h
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
data/test/dummy/Rakefile
ADDED
@@ -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,51 @@
|
|
1
|
+
Indexes.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
|
+
Indexes.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,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>
|
data/test/dummy/bin/rake
ADDED
@@ -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,25 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
Bundler.require(*Rails.groups)
|
6
|
+
require 'indexes'
|
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
|