选用实训使用es搜索

dev_aliyun_beta
cxt 6 years ago
parent 2b6e101547
commit c1a916484e

@ -121,25 +121,20 @@ class ShixunsController < ApplicationController
@shixuns = status == "published" ? @shixuns.where(status: 2) : @shixuns.where(status: [0, 1]) @shixuns = status == "published" ? @shixuns.where(status: 2) : @shixuns.where(status: [0, 1])
end end
## 搜索关键字创建者、实训名称、院校名称
unless params[:search].blank?
keyword = params[:search].strip
@shixuns = @shixuns.joins(user: [user_extension: :school]).
where("schools.name like '%#{keyword}%'
or concat(lastname, firstname) like '%#{keyword}%'
or shixuns.name like '%#{keyword.split(" ").join("%")}%'").distinct
end
## 筛选 难度 ## 筛选 难度
if params[:diff].present? && params[:diff].to_i != 0 if params[:diff].present? && params[:diff].to_i != 0
@shixuns = @shixuns.where(trainee: params[:diff]) @shixuns = @shixuns.where(trainee: params[:diff])
end end
@total_count = @shixuns.count
page = params[:page] || 1 page = params[:page] || 1
limit = params[:limit] || 15 limit = params[:limit] || 10
offset = (page.to_i - 1) * limit
order = params[:order] || "desc"
## 搜索关键字创建者、实训名称、院校名称
keyword = params[:search]
@shixuns = Shixun.search keyword, where: {id: @shixuns.pluck(:id)}, order: {"myshixuns_count" => order}, limit: limit, offset: offset
@shixuns = @shixuns.order("myshixuns_count desc").page(page).per(limit).includes(:shixun_info, :subjects, user: [user_extension: :school]) @total_count = @shixuns.total_count
end end
## 获取顶部菜单 ## 获取顶部菜单

@ -14,7 +14,9 @@ module Searchable::Shixun
def search_data def search_data
{ {
name: name, name: name,
description: Util.extract_content(description)[0..Searchable::MAXIMUM_LENGTH] description: Util.extract_content(description)[0..Searchable::MAXIMUM_LENGTH],
status: status,
myshixuns_count: myshixuns_count
}.merge!(searchable_user_data) }.merge!(searchable_user_data)
.merge!(searchable_challenge_data) .merge!(searchable_challenge_data)
end end
@ -37,7 +39,7 @@ module Searchable::Shixun
end end
def should_index? def should_index?
status == 2 # published [0, 1, 2].include?(status) # published
end end
def to_searchable_json def to_searchable_json

@ -14,10 +14,12 @@ class SearchService < ApplicationService
private private
def search_options def search_options
{ model_options = {
index_name: index_names, index_name: index_names,
model_includes: model_includes model_includes: model_includes
}.merge(default_options) }
model_options.merge(where: { status: 2 }) if index_names == [Shixun]
model_options.merge(default_options)
end end
def index_names def index_names

@ -1,15 +1,17 @@
json.shixun_list @shixuns do |shixun| json.shixuns_count @total_count
json.shixun_id shixun.id
json.identifier shixun.identifier json.shixun_list do
json.shixun_name shixun.name json.array! @shixuns.with_highlights(multiple: true) do |obj, highlights|
json.description shixun.description puts obj
json.myshixuns_count shixun.myshixuns_count json.merge! obj.to_searchable_json
json.school shixun.user&.school_name json.challenge_names obj.challenges.pluck(:subject)
json.creator shixun.user&.full_name
json.level level_to_s(shixun.trainee)
json.subjects shixun.subjects do |subject|
json.(subject, :id, :name)
end
end
json.shixuns_count @total_count json.title highlights.delete(:name)&.join('...') || obj.searchable_title
# json.description highlights.values[0,5].each { |arr| arr.is_a?(Array) ? arr.join('...') : arr }.join('<br/>')
json.content highlights
json.level level_to_s(obj.trainee)
json.subjects obj.subjects do |subject|
json.(subject, :id, :name)
end
end
end
Loading…
Cancel
Save