diff --git a/.gitignore b/.gitignore index 4f52644ff..09b9eab36 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ /config/secrets.yml /config/redis.yml +/config/elasticsearch.yml public/upload.html /config/configuration.yml diff --git a/Gemfile b/Gemfile index 3450c06e8..4cabd09b9 100644 --- a/Gemfile +++ b/Gemfile @@ -90,4 +90,4 @@ gem 'sinatra' gem 'bulk_insert' # elasticsearch -# gem 'searchkick' +gem 'searchkick' diff --git a/Gemfile.lock b/Gemfile.lock index 728bbc672..94893687b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,6 +90,14 @@ GEM connection_pool (2.2.2) crass (1.0.4) diff-lcs (1.3) + elasticsearch (7.2.0) + elasticsearch-api (= 7.2.0) + elasticsearch-transport (= 7.2.0) + elasticsearch-api (7.2.0) + multi_json + elasticsearch-transport (7.2.0) + faraday + multi_json erubi (1.7.1) execjs (2.7.0) faraday (0.15.4) @@ -100,6 +108,7 @@ GEM grape-entity (0.7.1) activesupport (>= 4.0) multi_json (>= 1.3.2) + hashie (3.6.0) htmlentities (4.3.4) httparty (0.16.2) multi_xml (>= 0.5.2) @@ -255,6 +264,10 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + searchkick (3.1.3) + activemodel (>= 4.2) + elasticsearch (>= 5) + hashie selenium-webdriver (3.14.0) childprocess (~> 0.5) rubyzip (~> 1.2) @@ -344,6 +357,7 @@ DEPENDENCIES ruby-ole rubyzip sass-rails (~> 5.0) + searchkick selenium-webdriver sidekiq simple_xlsx_reader diff --git a/app/models/challenge_tag.rb b/app/models/challenge_tag.rb index b68e1792c..acea39770 100644 --- a/app/models/challenge_tag.rb +++ b/app/models/challenge_tag.rb @@ -1,6 +1,5 @@ class ChallengeTag < ApplicationRecord - # TODO: ES feature - # include Searchable::Dependents::ChallengeTag + include Searchable::Dependents::ChallengeTag belongs_to :challenge, counter_cache: true belongs_to :challenge_choose, optional: true diff --git a/app/models/course.rb b/app/models/course.rb index 77ff6c46d..b2835fc21 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,6 +1,5 @@ class Course < ApplicationRecord - # TODO: ES feature - # include Searchable::Course + include Searchable::Course has_many :boards, dependent: :destroy diff --git a/app/models/memo.rb b/app/models/memo.rb index 3cb074a78..610a7684d 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -1,6 +1,5 @@ class Memo < ApplicationRecord - # TODO: ES feature - # include Searchable::Memo + include Searchable::Memo has_many :memo_tag_repertoires, :dependent => :destroy has_many :tag_repertoires, :through => :memo_tag_repertoires diff --git a/app/models/searchable/memo.rb b/app/models/searchable/memo.rb index 5aa9d5fe2..648f4671b 100644 --- a/app/models/searchable/memo.rb +++ b/app/models/searchable/memo.rb @@ -26,6 +26,7 @@ module Searchable::Memo { descendants_contents: Util.map_or_pluck(descendants, :content) .map { |content| Util.extract_content(content)[0..Searchable::MAXIMUM_LENGTH] } + .join('
') } end diff --git a/app/models/searchable/shixun.rb b/app/models/searchable/shixun.rb index 1f39dcd76..e4108024c 100644 --- a/app/models/searchable/shixun.rb +++ b/app/models/searchable/shixun.rb @@ -31,7 +31,7 @@ module Searchable::Shixun .each_with_index.map { |subject, index| "第#{index + 1}关 #{subject}" } { - challenge_names: challenge_names, + challenge_names: challenge_names.join(' '), challenge_tag_names: Util.map_or_pluck(challenge_tags, :name).uniq.join(' ') } end diff --git a/app/models/searchable/subject.rb b/app/models/searchable/subject.rb index feec41a3c..94a5c1383 100644 --- a/app/models/searchable/subject.rb +++ b/app/models/searchable/subject.rb @@ -31,15 +31,9 @@ module Searchable::Subject end def searchable_stages_data - subject_stages = - stages.map do |stage| - { - name: stage.name, - description: Util.extract_content(stage.description)[0..Searchable::MAXIMUM_LENGTH] - } - end - - { subject_stages: subject_stages} + subject_stages = stages.map { |stage| "#{stage.name} #{Util.extract_content(stage.description)}"[0..Searchable::MAXIMUM_LENGTH] } + + { subject_stages: subject_stages.join('
') } end def to_searchable_json diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 0925f4fa0..d274f0a6d 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -1,6 +1,5 @@ class Shixun < ApplicationRecord - # TODO: ES feature - # include Searchable::Shixun + include Searchable::Shixun # status: 0:编辑 1:申请发布 2:正式发布 3:关闭 -1:软删除 # hide_code: 隐藏代码窗口 diff --git a/app/models/stage.rb b/app/models/stage.rb index ee4e969dd..d255cddeb 100644 --- a/app/models/stage.rb +++ b/app/models/stage.rb @@ -1,6 +1,5 @@ class Stage < ApplicationRecord - # TODO: ES feature - # include Searchable::Dependents::Stage + include Searchable::Dependents::Stage belongs_to :subject, counter_cache: true diff --git a/app/models/subject.rb b/app/models/subject.rb index b9dd49f5a..064efb7a6 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -2,8 +2,7 @@ # 可以在初始创建的时候 class Subject < ApplicationRecord - # TODO: ES feature - # include Searchable::Subject + include Searchable::Subject #status :0 编辑中 1 审核中 2 发布 belongs_to :repertoire diff --git a/app/models/user.rb b/app/models/user.rb index 89918551e..fb1a47f2e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,6 @@ class User < ApplicationRecord include Watchable - # TODO: ES feature - # include Searchable::Dependents::User + include Searchable::Dependents::User # Account statuses STATUS_ANONYMOUS = 0 diff --git a/config/elasticsearch.yml.example b/config/elasticsearch.yml.example new file mode 100644 index 000000000..cbecb85d7 --- /dev/null +++ b/config/elasticsearch.yml.example @@ -0,0 +1,13 @@ +defaults: &defaults + url: http://localhost:9200 + +development: + <<: *defaults + +test: + <<: *defaults + +production: + <<: *defaults + url: 'http://elastic:Elas+ucloud123@106.75.27.125:59200/' + # url: 'http://elastic:TEST_elastickibana321@es-cn-0pp174wsj000iubdx.public.elasticsearch.aliyuncs.com' diff --git a/config/initializers/elasticsearch.rb b/config/initializers/elasticsearch.rb new file mode 100644 index 000000000..5ab7d3e62 --- /dev/null +++ b/config/initializers/elasticsearch.rb @@ -0,0 +1,2 @@ +redis_config = Rails.application.config_for(:elasticsearch) +ENV['ELASTICSEARCH_URL'] = redis_config['url']