LoginSignup
0
0

More than 3 years have passed since last update.

pgroongaで電話番号の表記ゆれ検索などを試す

Last updated at Posted at 2020-11-19

概要

前回の記事で、padrino+pgroongaの組み合わせで、既存サイトを全文検索対応した。
チュートリアルに載っているような、電話番号の表記ゆれにも対応して検索しようとしたところで失敗したので、その記録。

問題と対策

既存のサイトはpadrino/ActiveRecordで構築されていた。
結果から書いてしまうと、テーブルのカラムがtextになっていなかったのが原因で、migration部分を書き直すことでうまくいった。

001_create_db.rb
class CreateContacts < ActiveRecord::Migration[5.2]
  def self.up
    create_table :contacts do |t|
      t.string :name  # <= 手癖で短い文字列string 
      t.string :tel   # <= と軽い気持ちで設定していた。
      t.string :email
      t.text :desc
      t.timestamps null: false
    end
  end

  def self.down
    drop_table :contacts
  end
end

何の気なしに↑みたいなmigrationを書いていたが、PostgreSQL向けにはt.stringは character varying(n) として展開される様子(使っているのはActiveRecord 5.2)。
こちらを下記のようにした(手軽に対応できるうちに気づいていてよかった。あとからだとこちらの記事のようにchange_columnすることになったのかも)

001_create_db.rb
class CreateContacts < ActiveRecord::Migration[5.2]
  def self.up
    create_table :contacts do |t|
      t.text:telephone 
      t.text:tel
      t.string :email
      t.text :desc
      t.timestamps null: false
    end
  end

  def self.down
    drop_table :contacts
  end
end

上記のようにテーブル列の型を変更することで、チュートリアルにあるようなuse_readingを使った読み仮名検索、電話番号の表記ゆれに対応したあいまい検索ができるようになった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0