Searchkick - 多模型& 字段的自动完成。

问题描述 投票:0回答:1

Searchkick - 多模型& 字段的自动完成。

我正在努力实施 自动完成功能 对于与我的多个模型相关联的 Post 模型。搜索功能工作正常,并返回预期的数据。我的自动完成方法也能正常工作,如果我按照在 文件 (不过只针对帖子的标题)。

我还尝试了 本回答 而这 一个 换用 Post.index.namePost.searchkick_index.name 但自动完成功能不显示。

这是我在 posts_controller.rb:

def autocomplete
    render json: Post.search(params[:query],
                             index_name: [
                               Post.searchkick_index.name,
                               Tag.searchkick_index.name,
                               User.searchkick_index.name
                              ],
                             limit: 10,
                             load: false,
                             misspellings: { below: 5 })
end

我也试过了。

def autocomplete
  render json: Searchkick.search(params[:query],
                                 models: [Post, Tag, User],
                                 limit: 10,
                                 load: false,
                                 misspellings: { below: 5 })
end

上述代码没有出错,但自动完成功能也不工作。

post.rb:

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_many :posts_tags, dependent: :destroy
  has_many :tags, through: :posts_tags

  searchkick word_start: %i[title]

  def search_data
    {
      title: title,
      description: description,
      user: user.full_name
    }.merge(
      tag: tags.map(&:title),
      comments: comments.map(&:description)
    )
  end
end

按照答案部分的建议,我还尝试了以下方法。

def autocomplete
  posts = Post.search(params[:query], execute: false)
  tags = Tag.search(params[:query], execute: false)
  users = User.search(params[:query], execute: false)

  render json: Searchkick.multi_search([posts, tags, users])
end

这将返回以下错误。fatal - exception reentered.

我希望能够自动完成Post的 标题, Tag's 标题 &amp; 用户的 全名. 我应该如何修改我的代码?

先谢谢你

ruby-on-rails elasticsearch autocomplete typeahead.js searchkick
1个回答
0
投票

多重搜索 的searchkick。

posts = Post.search(params[:query], execute: false)
tags = Tag.search(params[:query], execute: false)
users = User.search(params[:query], execute: false)

Searchkick.multi_search([posts, tags, users])
© www.soinside.com 2019 - 2024. All rights reserved.