From abd11179d56da4f514f2c5623cf219048a2b1eaa Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Thu, 14 Nov 2019 14:47:41 +0800 Subject: [PATCH] FIX es bug --- app/models/attachment.rb | 10 ++++------ app/models/concerns/searchable.rb | 14 ++++++++++++++ app/models/course.rb | 16 +++++----------- app/models/memo.rb | 8 +++----- app/models/project.rb | 5 +---- app/models/syllabus.rb | 5 +---- app/models/user.rb | 5 +---- 7 files changed, 29 insertions(+), 34 deletions(-) create mode 100644 app/models/concerns/searchable.rb diff --git a/app/models/attachment.rb b/app/models/attachment.rb index cf1c60493..7bd098c5b 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -45,12 +45,10 @@ class Attachment < ActiveRecord::Base # validates :description, length: {maximum: 254} validate :validate_max_file_size - #elasticsearch - include Elasticsearch::Model + include ApplicationHelper - #elasticsearch kaminari init - Kaminari::Hooks.init - Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + #elasticsearch + include Searchable settings index: { number_of_shards: 5 } do mappings dynamic: 'false' do indexes :filename, analyzer: 'smartcn',index_options: 'offsets' @@ -145,7 +143,7 @@ class Attachment < ActiveRecord::Base def get_apply_resource_status attachment_id, author_id status = ApplyResource.where("attachment_id =? and user_id =?", attachment_id, author_id).first.try(:status) - status == 2 + status == 2 end # add by nwb diff --git a/app/models/concerns/searchable.rb b/app/models/concerns/searchable.rb new file mode 100644 index 000000000..8b9f691f1 --- /dev/null +++ b/app/models/concerns/searchable.rb @@ -0,0 +1,14 @@ +module Searchable + extend ActiveSupport::Concern + + included do + include Elasticsearch::Model + document_type 'doc' # 解决因Elasticsearch版本导致es语法错误的问题 + + #elasticsearch kaminari init + Kaminari::Hooks.init + Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + + end + +end diff --git a/app/models/course.rb b/app/models/course.rb index 868bd954c..714258295 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -11,11 +11,7 @@ class Course < ActiveRecord::Base STATUS_ARCHIVED = 9 #elasticsearch - include Elasticsearch::Model - - #elasticsearch kaminari init - Kaminari::Hooks.init - Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + include Searchable settings index: { number_of_shards: 5 } do mappings dynamic: 'false' do indexes :name, analyzer: 'smartcn',index_options: 'offsets' @@ -23,7 +19,7 @@ class Course < ActiveRecord::Base indexes :updated_at, index:"not_analyzed",type:"date" end end - + attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password,:is_public,:description,:class_period, :open_student, :enterprise_name, :is_delete, :syllabus_id, :end_time, :end_term, :publish_resource, :is_copy, :endup_time, :setup_time, :school_id @@ -72,7 +68,7 @@ class Course < ActiveRecord::Base acts_as_nested_set :order => 'name', :dependent => :destroy acts_as_attachable :view_permission => :view_course_files, :delete_permission => :manage_files - + validates_presence_of :term,:name validates_format_of :class_period, :with =>/^[1-9]\d*$/ validates_format_of :time, :with => /^\d{4}$/ @@ -309,7 +305,7 @@ class Course < ActiveRecord::Base def self_validate end - + def update_files_public unless self.is_public? self.attachments.each do |a| @@ -367,7 +363,7 @@ class Course < ActiveRecord::Base rescue Exception => e end_time = Time.parse("3000-01-01") Rails.logger.error "[Error] course endup_time error. ===> #{e}" - ensure + ensure return end_time end end @@ -537,5 +533,3 @@ end # Index all article records from the DB to Elasticsearch #Course.where('is_public = 1').import :force=>true - - diff --git a/app/models/memo.rb b/app/models/memo.rb index da84ffcda..4d16817b5 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -3,7 +3,6 @@ class Memo < ActiveRecord::Base include Redmine::SafeAttributes include UserScoreHelper include ApplicationHelper - include Elasticsearch::Model belongs_to :forum has_many_kindeditor_assets :assets, :dependent => :destroy belongs_to :author, :class_name => "User", :foreign_key => 'author_id' @@ -15,9 +14,8 @@ class Memo < ActiveRecord::Base validate :cannot_reply_to_locked_topic, :on => :create - #elasticsearch kaminari init - Kaminari::Hooks.init - Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + #elasticsearch + include Searchable settings index: { number_of_shards: 5 , analysis: { @@ -196,7 +194,7 @@ class Memo < ActiveRecord::Base def locked? self.lock end - + def editable_by? user # user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)) user.admin? || self.author == user diff --git a/app/models/project.rb b/app/models/project.rb index 8f23b5cde..e17729799 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -31,10 +31,7 @@ class Project < ActiveRecord::Base # Specific overidden Activities #elasticsearch - include Elasticsearch::Model - #elasticsearch kaminari init - Kaminari::Hooks.init - Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + include Searchable settings index: { number_of_shards: 5 } do mappings dynamic: 'false' do indexes :name, analyzer: 'smartcn',index_options: 'offsets' diff --git a/app/models/syllabus.rb b/app/models/syllabus.rb index ea5d44dfd..efbbe93aa 100644 --- a/app/models/syllabus.rb +++ b/app/models/syllabus.rb @@ -3,10 +3,7 @@ class Syllabus < ActiveRecord::Base include Redmine::SafeAttributes include ApplicationHelper #elasticsearch - include Elasticsearch::Model - #elasticsearch kaminari init - Kaminari::Hooks.init - Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + include Searchable settings index: { number_of_shards: 5 } do mappings dynamic: 'false' do indexes :title, analyzer: 'smartcn',index_options: 'offsets' diff --git a/app/models/user.rb b/app/models/user.rb index 35579ba72..41d0d7cc0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,10 +26,7 @@ class User < Principal include Redmine::SafeAttributes seems_rateable_rater #elasticsearch - include Elasticsearch::Model - #elasticsearch kaminari init - Kaminari::Hooks.init - Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + include Searchable settings index: { number_of_shards: 5 } do mappings dynamic: 'false' do indexes :login, analyzer: 'smartcn',index_options: 'offsets'