Use Elasticsearch
This commit is contained in:
parent
ce8a7ffb20
commit
086f728d04
7 changed files with 65 additions and 0 deletions
6
Gemfile
6
Gemfile
|
@ -115,6 +115,12 @@ gem 'kaminari', '~> 1.1'
|
|||
# This gem is part of the AWS SDK for Ruby.
|
||||
gem 'aws-sdk-s3', require: false
|
||||
|
||||
# ActiveModel/Record integrations for Elasticsearch.
|
||||
gem 'elasticsearch-model', '~> 7.0'
|
||||
|
||||
# Ruby on Rails integrations for Elasticsearch.
|
||||
gem 'elasticsearch-rails', '~> 7.0'
|
||||
|
||||
group :development, :test do
|
||||
# factory_bot provides a framework and DSL for defining and using factories.
|
||||
gem 'factory_bot_rails', '~> 4.10'
|
||||
|
|
15
Gemfile.lock
15
Gemfile.lock
|
@ -168,6 +168,19 @@ GEM
|
|||
docile (1.3.2)
|
||||
domain_name (0.5.20190701)
|
||||
unf (>= 0.0.5, < 1.0.0)
|
||||
elasticsearch (7.4.0)
|
||||
elasticsearch-api (= 7.4.0)
|
||||
elasticsearch-transport (= 7.4.0)
|
||||
elasticsearch-api (7.4.0)
|
||||
multi_json
|
||||
elasticsearch-model (7.0.0)
|
||||
activesupport (> 3)
|
||||
elasticsearch (> 1)
|
||||
hashie
|
||||
elasticsearch-rails (7.0.0)
|
||||
elasticsearch-transport (7.4.0)
|
||||
faraday
|
||||
multi_json
|
||||
erubi (1.9.0)
|
||||
execjs (2.7.0)
|
||||
factory_bot (4.11.1)
|
||||
|
@ -476,6 +489,8 @@ DEPENDENCIES
|
|||
database_cleaner (~> 1.7)
|
||||
devise (~> 4.7)
|
||||
devise-i18n (~> 1.8)
|
||||
elasticsearch-model (~> 7.0)
|
||||
elasticsearch-rails (~> 7.0)
|
||||
factory_bot_rails (~> 4.10)
|
||||
faker (~> 1.8)
|
||||
font-awesome-sass (~> 5.5.0)
|
||||
|
|
10
app/models/concerns/searchable.rb
Normal file
10
app/models/concerns/searchable.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Searchable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
include Elasticsearch::Model
|
||||
include Elasticsearch::Model::Callbacks
|
||||
end
|
||||
end
|
|
@ -2,9 +2,19 @@
|
|||
|
||||
class Person < ApplicationRecord
|
||||
include Nameable
|
||||
include Searchable
|
||||
|
||||
ACCOUNT_CONNECTION_TOKEN_RE = /\A\w+\z/.freeze
|
||||
|
||||
settings index: { number_of_shards: 1 } do
|
||||
mapping dynamic: false do
|
||||
indexes :first_name, analyzer: 'russian'
|
||||
indexes :middle_name, analyzer: 'russian'
|
||||
indexes :last_name, analyzer: 'russian'
|
||||
indexes :place_of_birth, analyzer: 'russian'
|
||||
end
|
||||
end
|
||||
|
||||
################
|
||||
# Associations #
|
||||
################
|
||||
|
|
|
@ -17,6 +17,8 @@ require 'sprockets/railtie'
|
|||
# you've limited to :test, :development, :staging, or :production.
|
||||
Bundler.require(*Rails.groups)
|
||||
|
||||
require 'elasticsearch/rails/instrumentation'
|
||||
|
||||
require 'csv'
|
||||
|
||||
module Partynest
|
||||
|
|
16
config/initializers/elasticsearch.rb
Normal file
16
config/initializers/elasticsearch.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Be sure to restart your server when you modify this file.
|
||||
|
||||
Elasticsearch::Model.client = Elasticsearch::Client.new(
|
||||
scheme: :http,
|
||||
host: '127.0.0.1',
|
||||
port: 9200,
|
||||
user: 'elastic',
|
||||
password: 'changeme',
|
||||
|
||||
transport_options: {
|
||||
request: { timeout: 5 },
|
||||
ssl: { ca_file: nil },
|
||||
},
|
||||
)
|
6
lib/tasks/elasticsearch.rake
Normal file
6
lib/tasks/elasticsearch.rake
Normal file
|
@ -0,0 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'elasticsearch/rails/tasks/import'
|
||||
|
||||
Rake::Task['elasticsearch:import:all'].prerequisites << :environment
|
||||
Rake::Task['elasticsearch:import:model'].prerequisites << :environment
|
Reference in a new issue