Add method to Search controller
This will search with trigram first, and then if there aren't enough results, search with prefixed text search
This commit is contained in:
parent
cca316bb57
commit
5473c1c579
1 changed files with 26 additions and 0 deletions
|
|
@ -3,6 +3,32 @@
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class SearchController < Api::V1::ApiController
|
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
|
def characters
|
||||||
filters = search_params[:filters]
|
filters = search_params[:filters]
|
||||||
locale = search_params[:locale] || 'en'
|
locale = search_params[:locale] || 'en'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue