From 5473c1c5797ba5b5f48cf12536c8a371f98d5fa7 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Wed, 5 Jul 2023 18:46:04 -0700 Subject: [PATCH] Add method to Search controller This will search with trigram first, and then if there aren't enough results, search with prefixed text search --- app/controllers/api/v1/search_controller.rb | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index b60c3d0..adb9ae3 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -3,6 +3,32 @@ module Api module V1 class SearchController < Api::V1::ApiController + def all + trigram = { + trigram: { + threshold: 0.3 + } + } + + tsearch = { + tsearch: { + prefix: true, + dictionary: 'simple' + } + } + + + PgSearch.multisearch_options = { using: trigram } + results = PgSearch.multisearch(search_params[:query]).limit(10) + + if (results.length < 5) && (search_params[:query].length >= 2) + PgSearch.multisearch_options = { using: tsearch } + results = PgSearch.multisearch(search_params[:query], { using: tsearch }).limit(10) + end + + render json: SearchBlueprint.render(results, root: :results) + end + def characters filters = search_params[:filters] locale = search_params[:locale] || 'en'