.../articles/
The Clearest Guide to Full-Text Search Migrations in Laravel

The Clearest Guide to Full-Text Search Migrations in Laravel

2022.02.24

This is Akagishi, a backend engineer.

I recently implemented full-text search for the first time, and I found that the articles I found online as reference were honestly kind of confusing not very beginner-friendly. So in this post, I'll explain how to implement it in a way that even beginners can pick up quickly and easily!

Environment

Laravel 8.x MySQL Docker

The Documentation Trap: A Setup That Doesn't Actually Work

Being the model beginner that I am, when I looked up how to write full-text search in the documentation, here's what it explained:

$table->fulltext('body'); | Add a fulltext index (MySQL/PostgreSQL)

https://readouble.com/laravel/8.x/ja/migrations.html

I thought, "wow, that's easy," and figured I was basically done, but with the setup written above, you run into the problem that partial-match records can't be retrieved. Goddammit!!! To retrieve records via partial match, you need to use the ngram parser or MeCab.

On top of that, by default it's configured to only search on words of 3 characters or more. That's inconvenient for Japanese. To make sure it can handle all sorts of compound words, you also need to change the configuration so it can search on words of 2 characters or more.

I'll go into detail in the next section.

The Final Version | How to Match Partially on Words of 2+ Characters

Configuring It to Search on 2+ Characters

Since we're using Docker, add the following to the MySQL config file (xxx.cnf):

[mysqld]
innodb_ft_min_token_size = 2

Using the ngram Parser in a Migration

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatexxxTable extends Migration
{
    public function up()
    {
        Schema::create('xxx', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hogehoge');
            $table->timestamps();
    });

    \DB::statement('ALTER TABLE テーブル名 ADD FULLTEXT INDEX インデックス名 (`該当カラム名`) with parser ngram');
}

    public function down()
    {
        Schema::dropIfExists('xxx');
    }
}

That's it. Now you're a full-text search master too.

See you again sometime. Take care, uhuhuhu~

written by

.../article/

Articles

All articles

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

We moved our corporate site's hosting and CMS, and made it bilingual along the way. The constraints we only found by running against real data were more useful than the migration itself, so this post focuses on where we got stuck.

Can't Read POST Data with Firebase Functions × Remix?

Can't Read POST Data with Firebase Functions × Remix?

How to read POST data from a Remix action when running on Firebase Functions.

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Building a structure where executives and leaders themselves can identify issues and evaluate solutions using generative AI. We introduce how combining this with our hands-on support dramatically improves both the quality and speed of digital transformation.

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Notes on the obstacles we hit while deploying a Next.js app managed in a monorepo to AWS Amplify.

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Many production companies have adopted remote work as a result of the pandemic, and we are one of them.

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

There is no single formula for e-commerce design that sells. Driving revenue requires a solid concept, and getting to that concept requires thorough research.

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

With remote work becoming the norm during the COVID-19 pandemic, our team now works from home most days of the week.

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We hope this helps web designers who work in Figma. Read on for how to use it.

How to Build an E-Commerce Site, and Which Platforms We Recommend

How to Build an E-Commerce Site, and Which Platforms We Recommend

Shopping online for fashion, appliances, and even groceries is now routine. With the pandemic accelerating the shift, we receive a steady stream of questions about which platform to use and how much it costs.

Generating FastAPI Schema Classes from OpenAPI

Generating FastAPI Schema Classes from OpenAPI

We chose FastAPI, a relatively modern framework, for a Python API project. FastAPI can generate an OpenAPI definition from your backend code, but here we do the opposite: generating FastAPI schema classes from an OpenAPI definition prepared in advance.

View all articles

Contact us