Blog

検索機能アプデ

Cover Image for 検索機能アプデ

検索機能はとりあえず作って放置してたんだけど、ちょっと検索キーワードの処理を修正したよ

新機能

今回リリースされた機能はこちらです。

  • スペース区切りで複数文字をOR検索できるよ(スペースは全角でもOKだよ)
  • 全角「Next.js」、半角の「Next.js」、小文字の「next.js」全部同じ結果になるよ

Kuromoji

検索さわってたらいろいろ欲が出てきて、形態素解析がしたくなったよ。

いろいろWeb API提供してるサービスあるけど、あれこれ外部サービス使ってたらアカウントとかお金管理するの大変だなぁって思ってたら、Kuromojiはnpm installするだけでNext.js上で使えるっていうことなので導入してみたよ。

むずかしそうなイメージだったけど、GPTのいう通りにしたら、パッケージインストールして、ルート書いて、ちょこちょこエラー修正するだけで、テキストをKuromojiで解析できるAPIルートが作成できたよ。

たとえば、そのルートに「Next.jsとSvelteどっちもたのしいTypeScriptファミリー」って送信すると、こんなレスポンスが帰ってくるよ。

{
  "tokens": [
    {
      "word_id": 120,
      "word_type": "UNKNOWN",
      "word_position": 1,
      "surface_form": "Next",
      "pos": "名詞",
      "pos_detail_1": "固有名詞",
      "pos_detail_2": "組織",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "*"
    },
    {
      "word_id": 80,
      "word_type": "UNKNOWN",
      "word_position": 5,
      "surface_form": ".",
      "pos": "名詞",
      "pos_detail_1": "サ変接続",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "*"
    },
    {
      "word_id": 100,
      "word_type": "UNKNOWN",
      "word_position": 6,
      "surface_form": "js",
      "pos": "名詞",
      "pos_detail_1": "一般",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "*"
    },
    {
      "word_id": 93120,
      "word_type": "KNOWN",
      "word_position": 8,
      "surface_form": "と",
      "pos": "助詞",
      "pos_detail_1": "並立助詞",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "と",
      "reading": "ト",
      "pronunciation": "ト"
    },
    {
      "word_id": 120,
      "word_type": "UNKNOWN",
      "word_position": 9,
      "surface_form": "Svelte",
      "pos": "名詞",
      "pos_detail_1": "固有名詞",
      "pos_detail_2": "組織",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "*"
    },
    {
      "word_id": 43560,
      "word_type": "KNOWN",
      "word_position": 15,
      "surface_form": "どっち",
      "pos": "名詞",
      "pos_detail_1": "代名詞",
      "pos_detail_2": "一般",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "どっち",
      "reading": "ドッチ",
      "pronunciation": "ドッチ"
    },
    {
      "word_id": 93220,
      "word_type": "KNOWN",
      "word_position": 18,
      "surface_form": "も",
      "pos": "助詞",
      "pos_detail_1": "係助詞",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "も",
      "reading": "モ",
      "pronunciation": "モ"
    },
    {
      "word_id": 1546890,
      "word_type": "KNOWN",
      "word_position": 19,
      "surface_form": "たのしい",
      "pos": "形容詞",
      "pos_detail_1": "自立",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugated_type": "形容詞・イ段",
      "conjugated_form": "基本形",
      "basic_form": "たのしい",
      "reading": "タノシイ",
      "pronunciation": "タノシイ"
    },
    {
      "word_id": 100,
      "word_type": "UNKNOWN",
      "word_position": 23,
      "surface_form": "TypeScript",
      "pos": "名詞",
      "pos_detail_1": "一般",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "*"
    },
    {
      "word_id": 618020,
      "word_type": "KNOWN",
      "word_position": 33,
      "surface_form": "ファミリー",
      "pos": "名詞",
      "pos_detail_1": "一般",
      "pos_detail_2": "*",
      "pos_detail_3": "*",
      "conjugated_type": "*",
      "conjugated_form": "*",
      "basic_form": "ファミリー",
      "reading": "ファミリー",
      "pronunciation": "ファミリー"
    }
  ]
}

Kuromojiの特徴で、新語に弱いっていうのがあるから、「Next.js」とかは辞書登録しないとだめみたい。

でも、思った以上にちゃんと解析できててびっくらぽん!

このAPIを利用して、とりあえずVercel KV(Redis)に検索用インデックスを作っていこうかなと思ってるよ

今日作った全角、半角とかのキーワードの処理は、Kuromoji使う場合でも役に立ちそう

今日は眠いからまた明日〜。