From bd6cdc8650fea0a98721e1769645589ffdc6615a Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Wed, 5 Jul 2023 21:12:51 -0700 Subject: [PATCH] Add support for Japanese all-entity search --- app/controllers/api/v1/search_controller.rb | 55 ++++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index adb9ae3..842863b 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -3,32 +3,49 @@ module Api module V1 class SearchController < Api::V1::ApiController + TRIGRAM = { + trigram: { + threshold: 0.3 + } + }.freeze + + TSEARCH_WITH_PREFIX = { + tsearch: { + prefix: true, + dictionary: 'simple' + } + }.freeze + def all - trigram = { - trigram: { - threshold: 0.3 - } - } + locale = search_params[:locale] || 'en' - 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) + case locale + when 'en' + results = search_all_en + when 'ja' + results = search_all_ja end render json: SearchBlueprint.render(results, root: :results) end + def search_all_en + 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_WITH_PREFIX } + results = PgSearch.multisearch(search_params[:query]).limit(10) + end + + results + end + + def search_all_ja + PgSearch.multisearch_options = { using: TSEARCH_WITH_PREFIX } + PgSearch.multisearch(search_params[:query]).limit(10) + end + def characters filters = search_params[:filters] locale = search_params[:locale] || 'en'