Skip to content

Commit

Permalink
Add index to the title, details and instructions
Browse files Browse the repository at this point in the history
This allows us to search the fields using MySQL’s fulltext search
functions. Because we can’t index JSON fields using fulltext, we have
to create a virtual column which casts the JSON as text. I tried some
MySQL-fu to try and extract all the JSON values, but we can’t do it
recursively. This should be enough anyway, as it’s what we’re only
searching across the JSON at the moment.
  • Loading branch information
pezholio committed Feb 21, 2025
1 parent 1bf50ab commit 593df2c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddFulltextIndexToContentBlockEditions < ActiveRecord::Migration[8.0]
INDEX_NAME = "title_details_instructions_to_publishers".freeze

def up
change_table :content_block_editions, bulk: true do |t|
t.virtual :details_for_indexing, type: :text, as: "JSON_UNQUOTE(details)", stored: true
t.index %i[title details_for_indexing instructions_to_publishers], name: INDEX_NAME, type: :fulltext
end
end

def down
change_table :content_block_editions, bulk: true do |t|
t.remove :details_for_indexing
end
remove_index :content_block_editions, name: INDEX_NAME
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2025_01_29_095918) do
ActiveRecord::Schema[8.0].define(version: 2025_02_20_131003) do
create_table "assets", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "asset_manager_id", null: false
t.string "variant", null: false
Expand Down Expand Up @@ -236,7 +236,9 @@
t.text "internal_change_note"
t.text "change_note"
t.boolean "major_change"
t.virtual "details_for_indexing", type: :text, as: "json_unquote(`details`)", stored: true
t.index ["document_id"], name: "index_content_block_editions_on_document_id"
t.index ["title", "details_for_indexing", "instructions_to_publishers"], name: "title_details_instructions_to_publishers", type: :fulltext
t.index ["user_id"], name: "index_content_block_editions_on_user_id"
end

Expand Down

0 comments on commit 593df2c

Please sign in to comment.