diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 4b75a3297..815a44839 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -160,7 +160,8 @@ class WelcomeController < ApplicationController multi_match: { query: @name, type:"most_fields", - fields: ['login^5', 'firstname','lastname','name^5','description','filename^5'] + operator: "or", + fields: ['login', 'firstname','lastname','name','description^0.5','filename'] } }, highlight: { @@ -190,7 +191,8 @@ class WelcomeController < ApplicationController multi_match: { query: @name, type:"most_fields", - fields: ['login^5', 'firstname','lastname','name^5','description','filename^5'] + operator: "or", + fields: ['login', 'firstname','lastname','name','description^0.5','filename'] } }, highlight: { @@ -219,7 +221,8 @@ class WelcomeController < ApplicationController multi_match: { query: @name, type:"most_fields", - fields: ['login^5', 'firstname','lastname','name^5','description','filename^5'] + operator: "or", + fields: ['login', 'firstname','lastname','name','description^0.5','filename'] } }, highlight: { diff --git a/app/models/attachment.rb b/app/models/attachment.rb index a98111b4f..3cb2d9057 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -46,6 +46,7 @@ class Attachment < ActiveRecord::Base settings index: { number_of_shards: 1 } do mappings dynamic: 'false' do indexes :filename, analyzer: 'smartcn',index_options: 'offsets' + indexes :downloads, analyzer: 'smartcn',index_options: 'offsets' end end @@ -109,9 +110,15 @@ class Attachment < ActiveRecord::Base query: { multi_match: { query: query, + type:"most_fields", + operator: "or", fields: ['filename'] } }, + sort:{ + downloads: {order:"desc"}, + _score:{order:"desc"} + }, highlight: { pre_tags: [''], post_tags: [''], @@ -625,4 +632,4 @@ end #暂时只做公开课程/项目里的公开资源 和其他的公开资源 Attachment.where('is_public = 1 and ((container_type in ("Principal")) ' + 'or (container_type = "Course" and container_id in( SELECT `courses`.id FROM `courses` WHERE (courses.status <> 9 AND courses.is_public = 1)) )'+ - 'or (container_type = "Project" and container_id in(SELECT `projects`.id FROM `projects` WHERE (projects.status <> 9 AND projects.is_public = 1) ))' +')').import + 'or (container_type = "Project" and container_id in(SELECT `projects`.id FROM `projects` WHERE (projects.status <> 9 AND projects.is_public = 1) ))' +')').import :force=>true diff --git a/app/models/board.rb b/app/models/board.rb index edcbe0c9d..35a7a72d1 100644 --- a/app/models/board.rb +++ b/app/models/board.rb @@ -17,7 +17,7 @@ class Board < ActiveRecord::Base include Redmine::SafeAttributes - belongs_to :project + belongs_to :project,:touch => true belongs_to :course has_many :topics, :class_name => 'Message', :conditions => "#{Message.table_name}.parent_id IS NULL", :order => "#{Message.table_name}.created_on DESC" has_many :messages, :dependent => :destroy, :order => "#{Message.table_name}.created_on DESC" diff --git a/app/models/course.rb b/app/models/course.rb index 1991c00cb..71cd74633 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -16,6 +16,7 @@ class Course < ActiveRecord::Base mappings dynamic: 'false' do indexes :name, analyzer: 'smartcn',index_options: 'offsets' indexes :description, analyzer: 'smartcn',index_options: 'offsets' + indexes :updated_at, analyzer: 'smartcn',index_options: 'offsets' end end @@ -115,9 +116,15 @@ class Course < ActiveRecord::Base query: { multi_match: { query: query, - fields: ['name', 'description'] + type:"most_fields", + operator: "or", + fields: ['name', 'description^0.5'] } }, + sort: { + updated_at:{order:"desc"}, + _score:{order: "desc" } + }, highlight: { pre_tags: [''], post_tags: [''], @@ -405,6 +412,6 @@ end # body: { settings: Course.settings.to_hash, mappings: Course.mappings.to_hash } # Index all article records from the DB to Elasticsearch -Course.import +Course.where('is_public = 1').import :force=>true diff --git a/app/models/document.rb b/app/models/document.rb index 37983d6d4..b45a74775 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -17,7 +17,7 @@ class Document < ActiveRecord::Base include Redmine::SafeAttributes - belongs_to :project + belongs_to :project,:touch=>true belongs_to :user belongs_to :category, :class_name => "DocumentCategory", :foreign_key => "category_id" include UserScoreHelper diff --git a/app/models/issue.rb b/app/models/issue.rb index 2a6da44c5..edce3310a 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -19,7 +19,7 @@ class Issue < ActiveRecord::Base include Redmine::SafeAttributes include Redmine::Utils::DateCalculation include UserScoreHelper - belongs_to :project + belongs_to :project,:touch=> true belongs_to :tracker belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index 5e40267cb..14760a631 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -21,7 +21,8 @@ class JournalsForMessage < ActiveRecord::Base after_destroy :delete_kindeditor_assets belongs_to :project, :foreign_key => 'jour_id', - :conditions => "#{self.table_name}.jour_type = 'Project' " + :conditions => "#{self.table_name}.jour_type = 'Project' ",:touch => true + belongs_to :course, :foreign_key => 'jour_id' diff --git a/app/models/message.rb b/app/models/message.rb index 7af59815b..c5371097a 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -20,7 +20,7 @@ class Message < ActiveRecord::Base include UserScoreHelper include ApplicationHelper has_many_kindeditor_assets :assets, :dependent => :destroy - belongs_to :board + belongs_to :board,:touch => true belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' has_many :praise_tread, as: :praise_tread_object, dependent: :destroy diff --git a/app/models/news.rb b/app/models/news.rb index e9b8b5314..aa5d6df1f 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -17,7 +17,7 @@ class News < ActiveRecord::Base include Redmine::SafeAttributes - belongs_to :project + belongs_to :project,:touch => true include ApplicationHelper has_many_kindeditor_assets :assets, :dependent => :destroy #added by nwb diff --git a/app/models/project.rb b/app/models/project.rb index 53e909776..92e6a4d0b 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -39,6 +39,7 @@ class Project < ActiveRecord::Base mappings dynamic: 'false' do indexes :name, analyzer: 'smartcn',index_options: 'offsets' indexes :description, analyzer: 'smartcn',index_options: 'offsets' + indexes :updated_on, analyzer: 'smartcn',index_options: 'offsets' end end @@ -192,9 +193,15 @@ class Project < ActiveRecord::Base query: { multi_match: { query: query, - fields: ['name','description'] + type:"most_fields", + operator: "and", + fields: ['name','description^0.5'] } }, + sort: { + updated_on:{order: "desc" }, + _score:{order: "desc" } + }, highlight: { pre_tags: [''], post_tags: [''], @@ -1222,5 +1229,5 @@ class Project < ActiveRecord::Base end -Project.import +Project.where('is_public = 1').import :force=>true diff --git a/app/models/project_tags.rb b/app/models/project_tags.rb index 16de1ea45..bcf666fb5 100644 --- a/app/models/project_tags.rb +++ b/app/models/project_tags.rb @@ -2,7 +2,7 @@ class ProjectTags < ActiveRecord::Base attr_accessible :description, :project_id, :tag_id, :user_id ####################################################################################################添加代码 - belongs_to :project + belongs_to :project,:touch => true belongs_to :tag belongs_to :user diff --git a/app/models/user.rb b/app/models/user.rb index 3818e5d47..fc18313a0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,6 +35,7 @@ class User < Principal indexes :login, analyzer: 'smartcn',index_options: 'offsets' indexes :firstname, analyzer: 'smartcn',index_options: 'offsets' indexes :lastname, analyzer: 'smartcn',index_options: 'offsets' + indexes :last_login_on, analyzer: 'smartcn',index_options: 'offsets' end end @@ -271,9 +272,15 @@ class User < Principal query: { multi_match: { query: query, + type:"most_fields", + operator: "or", fields: ['login', 'firstname','lastname'] } }, + sort:{ + last_login_on: {order:"desc"}, + _score:{order:"desc"} + }, highlight: { pre_tags: [''], post_tags: [''], @@ -1208,4 +1215,4 @@ end # Index all article records from the DB to Elasticsearch # 匿名用户 角色 和 管理员角色不能被索引 -User.where('id not in (2,4)').import +User.where('id not in (2,4)').import :force=>true diff --git a/config/application.rb b/config/application.rb index 6e4a2983a..14793fcac 100644 --- a/config/application.rb +++ b/config/application.rb @@ -2,7 +2,7 @@ require File.expand_path('../boot', __FILE__) require 'rails/all' require 'sprockets/railtie' - +require 'elasticsearch/model' if defined?(Bundler) # If you precompile assets before deploying to production, use this line Bundler.require(*Rails.groups(:assets => %w(development test))) @@ -73,6 +73,7 @@ module RedmineApp end config.after_initialize do + Elasticsearch::Client.new hosts: ['localhost:9200', 'localhost:9201'], retry_on_failure: true,log:true end if File.exists?(File.join(File.dirname(__FILE__), 'additional_environment.rb'))