From 4e62e256ce258112a282c6a9bc1016ef7e6b91b8 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Tue, 26 Nov 2024 07:10:56 -0500 Subject: [PATCH] add searchkick and elasticsearch --- .env | 1 + Gemfile | 4 ++-- Gemfile.lock | 12 +++++------- app/models/course.rb | 2 ++ app/models/item.rb | 2 ++ app/models/location.rb | 2 ++ app/models/request.rb | 24 ++++++++---------------- app/models/user.rb | 5 ++--- config/application.rb | 8 ++++++++ config/initializers/z_elasticsearch.rb | 6 ------ docker-compose.yml | 6 ++++++ lib/tasks/elasticsearch.rake | 6 ------ 12 files changed, 38 insertions(+), 40 deletions(-) delete mode 100644 config/initializers/z_elasticsearch.rb delete mode 100644 lib/tasks/elasticsearch.rake diff --git a/.env b/.env index 7f37de0..c2750cf 100644 --- a/.env +++ b/.env @@ -2,3 +2,4 @@ PORT=3006 MAILCATCHER_HTTP=3086 SECRET_KEY_BASE=don't forget to set this to some thing strong in production NODE_OPTIONS=--openssl-legacy-provider +ELASTIC_SEARCH_URL=http://elasticsearch:9200 diff --git a/Gemfile b/Gemfile index 4092c17..449f33b 100644 --- a/Gemfile +++ b/Gemfile @@ -43,8 +43,8 @@ gem 'alma', '~> 0.3.3' gem 'primo', git: 'https://github.com/tulibraries/primo.git', branch: 'main' ## SEARCH - USING ELASTIC SEARCH -gem 'elasticsearch-model', '0.1.9' -gem 'elasticsearch-rails', '0.1.9' +gem 'searchkick' +gem 'elasticsearch' ## DUMP DATA INTO YML FILE ## # rake db:data:dump_dir dir=../tmp/db_june_2017 diff --git a/Gemfile.lock b/Gemfile.lock index c43d6f1..1f483ac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -136,11 +136,6 @@ GEM elasticsearch-api (= 8.16.0) elasticsearch-api (8.16.0) multi_json - elasticsearch-model (0.1.9) - activesupport (> 3) - elasticsearch (> 0.4) - hashie - elasticsearch-rails (0.1.9) em-websocket (0.5.3) eventmachine (>= 0.12.9) http_parser.rb (~> 0) @@ -344,6 +339,9 @@ GEM sprockets-rails tilt scrub_rb (1.0.1) + searchkick (5.4.0) + activemodel (>= 6.1) + hashie shellany (0.0.1) shoulda-context (2.0.0) shoulda-matchers (5.3.0) @@ -398,8 +396,7 @@ DEPENDENCIES caxlsx_rails (= 0.6.3) database_cleaner (~> 2.0, >= 2.0.1) devise - elasticsearch-model (= 0.1.9) - elasticsearch-rails (= 0.1.9) + elasticsearch email_validator (~> 2.2, >= 2.2.3) exception_notification (~> 4.5) factory_girl_rails (= 4.8.0) @@ -424,6 +421,7 @@ DEPENDENCIES rsolr (~> 2.5) rufus-scheduler (~> 3.8, >= 3.8.2) sass-rails (~> 6.0) + searchkick shoulda-context (~> 2.0) shoulda-matchers (~> 5.1) simple_form (~> 5.1) diff --git a/app/models/course.rb b/app/models/course.rb index edac856..495214e 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Course < ApplicationRecord + searchkick + # COURSE FORMAT # YEAR_FACULTY_SUBJECT_TERM_COURSEID__CREDITS_SECTION i.e. 2013_GL_ECON_S1_2500__3_A ignoring EN_A_LECT_01 diff --git a/app/models/item.rb b/app/models/item.rb index 1675e1f..497a501 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Item < ApplicationRecord + searchkick + ########################################### CONSTANTS ############################################ METADATA_MANUAL = 'MANUAL' METADATA_SOLR = 'SOLR' diff --git a/app/models/location.rb b/app/models/location.rb index 6899e94..ce3344a 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Location < ApplicationRecord + searchkick + # SETTING: setting_bcc_request_status_change # SERIALIZATION diff --git a/app/models/request.rb b/app/models/request.rb index ded3e4b..2202f79 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true class Request < ApplicationRecord - ## searchable Concern - include Searchable + searchkick ## CONSTANTS INCOMPLETE = 'incomplete' @@ -135,19 +134,12 @@ def self.remove_incomplete(remove = false) end ### ELASRTIC SEARCH HELPERS ## - def short_course_code - !course.nil? ? "#{course.subject} #{course.course_id}" : '' - end - - def instructor_name - !course.nil? ? course.instructor.to_s : '' - end - - def as_indexed_json(_options = {}) - as_json( - only: [:id], - include: %i[course reserve_location assigned_to items requester], - methods: %i[short_course_code instructor_name] - ) + def search_data + { + course_name: course.name, + course_code: course.code, + course_instructor: course.instructor, + requester_name: requester.name + } end end diff --git a/app/models/user.rb b/app/models/user.rb index 06587da..631ee44 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,12 +1,11 @@ # frozen_string_literal: true class User < ApplicationRecord + searchkick + # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :trackable, :lockable - - ## searchable Concern - include Searchable ## CONSTANTS FACULTY = 'FACULTY' diff --git a/config/application.rb b/config/application.rb index 6502576..d19b22a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -40,5 +40,13 @@ class Application < Rails::Application # in config/environments, which are processed later. # # config.eager_load_paths << Rails.root.join("extras") + + config.after_initialize do + if ENV['ELASTIC_SEARCH_URL'] + Searchkick.client = Elasticsearch::Client.new(url: ENV['ELASTIC_SEARCH_URL']) + elsif defined?(Setting) && Setting.search_elastic_server && Setting.search_elastic_enabled.to_s == 'true' + Searchkick.client = Elasticsearch::Client.new(url: Setting.search_elastic_server) + end + end end end diff --git a/config/initializers/z_elasticsearch.rb b/config/initializers/z_elasticsearch.rb deleted file mode 100644 index cea244c..0000000 --- a/config/initializers/z_elasticsearch.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -if defined?(Setting) && Setting.search_elastic_enabled.to_s == 'true' - Request.__elasticsearch__.client = Elasticsearch::Client.new host: Setting.search_elastic_server - User.__elasticsearch__.client = Elasticsearch::Client.new host: Setting.search_elastic_server -end diff --git a/docker-compose.yml b/docker-compose.yml index 937c23b..341d42e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,6 +22,7 @@ services: - RAILS_SERVE_STATIC_FILES=true - NODE_OPTIONS=${NODE_OPTIONS} - SELENIUM_REMOTE_URL=http://chrome:4444 + - ELASTIC_SEARCH_URL=http://elasticsearch:9200 env_file: - path: ./override.env required: false @@ -45,3 +46,8 @@ services: ports: - ${VNC_HTTP:-7904}:7900 + elasticsearch: + image: elasticsearch:8.16.1 + environment: + - discovery.type=single-node + - xpack.security.enabled=false diff --git a/lib/tasks/elasticsearch.rake b/lib/tasks/elasticsearch.rake deleted file mode 100644 index 5cee7c9..0000000 --- a/lib/tasks/elasticsearch.rake +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -require 'elasticsearch/rails/tasks/import' - -# bundle exec rake environment elasticsearch:import:model CLASS='MyModel' FORCE=y -# bundle exec rake -D elasticsearch