gpt4 book ai didi

ruby-on-rails - 使用searchkick设置高级搜索,处理与多个模型的关联(Rails)

转载 作者:行者123 更新时间:2023-12-02 23:28:10 26 4
gpt4 key购买 nike

对于我的Rails应用程序,我正在尝试使用Searckick(elasticsearch)进行高级搜索。我想做的是:

  • 使得可以搜索“用户”,“位置”和“能力”,并始终将用户配置文件作为结果返回。

  • 到目前为止,我已经对其进行了修复,可以在User上进行搜索,但是我不确定如何在这些其他模型上进行搜索。

    我的路线:
    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。

    谁能告诉我如何设置此搜索?

    我的个人资料 Controller :
    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),但这不起作用。

    我试过的
      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

    最佳答案

    TL; DR 定义search_data方法来自定义搜索索引

    您的代码中当前定义的 self.search不正确
    您的self.search方法将是一种通过ActiveRecord和正在使用的数据库进行搜索的方法。这不是您通过Searchkick进行搜索的方式。

    对于Searchkick 如果要返回与用户属性,能力和位置搜索相匹配的用户配置文件,则应使用search_data方法(已记录为here)通过自定义映射定义要搜索的用户属性,能力和位置:

    class User < ActiveRecord::Base
    searchkick locations: ["location"]
    # this line will eager load your assocations
    scope :search_import, -> { includes(:location, :competences) }

    def search_data
    {
    first_name: first_name,
    last_name: last_name,
    competences: competences.map(&:name),
    # from your code above it looks like you want to search location.name
    location_name: location.name
    # the following merged hash would allow you to search by location latitude and
    # longitude coordinates if you have them
    ## if you don't use latitude and longitude then you don't need to include
    ## `locations: ["location"] when you call searchkick in the User model on line 2
    ## above or the merged location hash below
    }.merge(location: {lat: location.latitude, lon: location.longitude})
    end
    end

    现在,您在 Profiles#index操作中的搜索查询将起作用:
    @users = User.search(query, suggest: true, operator: "or")

    不需要您定义的 self.search方法。

    其他说明
  • 我假设由于您在查询中包括了suggest: true,因此您已在用户模型上正确定义了建议:
    class User < ActiveRecord::Base
    searchkick locations: ["location"], suggest: [:first_name, :last_name, :location_name] # or whatever fields you want
    end
  • 请确保reindex
  • ,因为您只想返回User对象,所以您无需为LocationCompetence模型建立索引(根据上面的代码,我无法确定您之前是否做过此操作)
  • 关于ruby-on-rails - 使用searchkick设置高级搜索,处理与多个模型的关联(Rails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40236106/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com