July 2023 Feature Release: Tagging objects (#118)

* Remove ap call

* Fix remix render method

* Downcase username on db end

There was a bug where users with capital letters in their name could not access their profiles after we tried to make things case insensitive.

* Remove ap call and unused code

* Add granblue.team to cors

This works now!

* Implement all-entity search to support tagging objects (#117)

* Add table for multisearch

* Add new route for searching all entities

* Make models multisearchable

We're going to start with Character, Summon, Weapon and Jobs

* Add method to Search controller

This will search with trigram first, and then if there aren't enough results, search with prefixed text search

* Add support for Japanese all-entity search
This commit is contained in:
Justin Edmund 2023-07-05 21:51:36 -07:00 committed by GitHub
parent 10d222fddc
commit 39a0d33bee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 131 additions and 1 deletions

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
module Api
module V1
class SearchBlueprint < Blueprinter::Base
identifier :searchable_id
fields :searchable_type, :granblue_id, :name_en, :name_jp, :element
end
end
end

View file

@ -3,6 +3,49 @@
module Api module Api
module V1 module V1
class SearchController < Api::V1::ApiController class SearchController < Api::V1::ApiController
TRIGRAM = {
trigram: {
threshold: 0.3
}
}.freeze
TSEARCH_WITH_PREFIX = {
tsearch: {
prefix: true,
dictionary: 'simple'
}
}.freeze
def all
locale = search_params[:locale] || 'en'
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 def characters
filters = search_params[:filters] filters = search_params[:filters]
locale = search_params[:locale] || 'en' locale = search_params[:locale] || 'en'

View file

@ -3,6 +3,16 @@
class Character < ApplicationRecord class Character < ApplicationRecord
include PgSearch::Model include PgSearch::Model
multisearchable against: %i[name_en name_jp],
additional_attributes: lambda { |character|
{
name_en: character.name_en,
name_jp: character.name_jp,
granblue_id: character.granblue_id,
element: character.element
}
}
pg_search_scope :en_search, pg_search_scope :en_search,
against: :name_en, against: :name_en,
using: { using: {

View file

@ -1,9 +1,21 @@
# frozen_string_literal: true # frozen_string_literal: true
class Job < ApplicationRecord class Job < ApplicationRecord
include PgSearch::Model
belongs_to :party belongs_to :party
has_many :skills, class_name: 'JobSkill' has_many :skills, class_name: 'JobSkill'
multisearchable against: %i[name_en name_jp],
additional_attributes: lambda { |job|
{
name_en: job.name_en,
name_jp: job.name_jp,
granblue_id: job.granblue_id,
element: 0
}
}
belongs_to :base_job, belongs_to :base_job,
foreign_key: 'base_job_id', foreign_key: 'base_job_id',
class_name: 'Job', class_name: 'Job',

View file

@ -3,6 +3,16 @@
class Summon < ApplicationRecord class Summon < ApplicationRecord
include PgSearch::Model include PgSearch::Model
multisearchable against: %i[name_en name_jp],
additional_attributes: lambda { |summon|
{
name_en: summon.name_en,
name_jp: summon.name_jp,
granblue_id: summon.granblue_id,
element: summon.element
}
}
pg_search_scope :en_search, pg_search_scope :en_search,
against: :name_en, against: :name_en,
using: { using: {

View file

@ -3,6 +3,16 @@
class Weapon < ApplicationRecord class Weapon < ApplicationRecord
include PgSearch::Model include PgSearch::Model
multisearchable against: %i[name_en name_jp],
additional_attributes: lambda { |weapon|
{
name_en: weapon.name_en,
name_jp: weapon.name_jp,
granblue_id: weapon.granblue_id,
element: weapon.element
}
}
pg_search_scope :en_search, pg_search_scope :en_search,
against: :name_en, against: :name_en,
using: { using: {

View file

@ -31,6 +31,7 @@ Rails.application.routes.draw do
post 'check/email', to: 'users#check_email' post 'check/email', to: 'users#check_email'
post 'check/username', to: 'users#check_username' post 'check/username', to: 'users#check_username'
post 'search', to: 'search#all'
post 'search/characters', to: 'search#characters' post 'search/characters', to: 'search#characters'
post 'search/weapons', to: 'search#weapons' post 'search/weapons', to: 'search#weapons'
post 'search/summons', to: 'search#summons' post 'search/summons', to: 'search#summons'

View file

@ -0,0 +1,21 @@
class CreatePgSearchDocuments < ActiveRecord::Migration[7.0]
def up
say_with_time('Creating table for pg_search multisearch') do
create_table :pg_search_documents do |t|
t.text :content
t.string :granblue_id
t.string :name_en
t.string :name_jp
t.integer :element
t.belongs_to :searchable, type: :uuid, polymorphic: true, index: true
t.timestamps null: false
end
end
end
def down
say_with_time('Dropping table for pg_search multisearch') do
drop_table :pg_search_documents
end
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_07_02_035508) do ActiveRecord::Schema[7.0].define(version: 2023_07_05_065015) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "btree_gin" enable_extension "btree_gin"
enable_extension "pg_trgm" enable_extension "pg_trgm"
@ -361,6 +361,19 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_02_035508) do
t.index ["user_id"], name: "index_parties_on_user_id" t.index ["user_id"], name: "index_parties_on_user_id"
end end
create_table "pg_search_documents", force: :cascade do |t|
t.text "content"
t.string "granblue_id"
t.string "name_en"
t.string "name_jp"
t.integer "element"
t.string "searchable_type"
t.uuid "searchable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable"
end
create_table "raid_groups", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| create_table "raid_groups", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name_en", null: false t.string "name_en", null: false
t.string "name_jp", null: false t.string "name_jp", null: false