對於我的Rails應用程序,我試圖使用Searckick(elasticsearch)設置高級搜索。他們想要做的事情是:使用searchkick設置高級搜索,應對與多個模型(Rails)的關聯
- 可以在用戶,位置和能力上進行搜索,並始終將用戶配置文件恢復爲結果。
到目前爲止我已經修復了它,我可以在用戶上搜索,但我不確定如何能夠搜索這些其他模型。
我的路線:
Rails.application.routes.draw do
ActiveAdmin.routes(self)
devise_for :users, controllers: {sessions: "sessions", registrations:
"registrations"}
# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html
root 'pages#home'
get "/news", to: 'pages#news'
get "welcome_back", to: 'pages#welcome_back'
get "/profile", to: "profile#show"
resources :profiles do
collection do
get :autocomplete
end
end
namespace :profile do
resources :locations
resources :positions
resources :competences
end
end
一個用戶屬於一個位置,必須通過一個連接表多種才能。換句話說:用戶有一個location_id,您可以調用用戶的.competences,以查看用戶擁有哪些users_competences。
任何人都可以告訴我如何設置此搜索?
我的配置控制器:
class ProfilesController < ApplicationController
def index
query = params[:search].presence || "*"
@users = User.search(query, suggest: true, operator: "or")
end
def autocomplete
render json: ["Test"]
end
end
我試圖在我的模型高清自我(搜索)的工作,但是這是行不通的。
我試了一下:
def self.search(search)
where(["User.first_name LIKE ?","%#{search}%"])
where(["User.last_name LIKE ?","%#{search}%"])
where(["User.competences.collect{&:name} IN ?","%#{search}%"])
joins(:location).where("location.name LIKE ?", "%#{search}%")
else
all
end
end