Searchkick - Autocomplete with multiple models & fields
I am struggling to implement the autocomplete functionality for multiple models associated to my Post
model. The search functionality works fine and returns the expected data. My autocomplete method also works fine if I implement it the way it is done in the documentation (only for posts' title however).
I also tried this answer and this one by switching Post.index.name
to Post.searchkick_index.name
but the autocomplete functionality does not display.
This is the code I wrote in 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
I also tried:
def autocomplete
render json: Searchkick.search(params[:query],
models: [Post, Tag, User],
limit: 10,
load: false,
misspellings: { below: 5 })
end
I get no errors with the above code, but the autocomplete functionality does not work either.
In 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
As suggested in the answer section, I also tried the following:
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
This returns the following error: fatal - exception reentered
.
I want to be able to autocomplete Post's title , Tag's title & User's full_name . How should I change my code?
Thank you in advance!
From multi_search of 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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.