How to use with autocomplete
apuntovanini opened this issue · comments
Hello @ankane, thanks as usual for your amazing work
A question: how would you suggest using searchjoy with autocomplete-as-you-type feature? Tracking each character is too much, maybe with a timeout? Something like
if user.searches.where('created_at > ?', 5.seconds.ago)
# update search
else
# search and track
end
Hey @apuntovanini, I haven't used search-as-you-type in a production environment, but that sounds like a good place to start. Let me know how it goes.
For our specific needs in this case we decided to overwrite current session's last search
if Searchjoy::Search.where(session_id: context[:session].id.to_s).where('created_at >= ?', Time.now - 30.seconds).any?
search = Searchjoy::Search.where(session_id: context[:session].id.to_s).last
else
search = Searchjoy::Search.new(
session_id: context[:session].id.to_s,
search_type: 'JobName',
user_id: context[:current_user]&.id
)
end
search.query = term
search.normalized_query = term.downcase
search.results_count = r.count
search.save
We return search.id
to the frontend to store the conversion when user clicks to one of the suggestions we provide with Searchkick. We'll surely refine this, but right now it's already giving some good insights
Awesome, makes sense. Great to hear it's working.
Cleaning up issues. If there are any more learnings (either now or in the future), would love to hear them (just fyi).