LoginSignup
15
15

More than 5 years have passed since last update.

RubyとDockerを使って簡単に全文検索エンジンを始めてみよう

Posted at

Groonga Advent Calendar 2015の7日目の記事です。前日は@ktouさんのGroonga Meatup 2015:Groonga族2015でした!

Groongaには公式でDockerコンテナが用意されています。今回はgroonga/rroongaを使ってRuby経由で簡単に全文検索を試してみましょう。

rroonga-logo.png
small_h.png

インストール

Dockerを使えるようにしてください。その後で以下のコマンドを実行します。Dockerを使うとインストールの失敗がないのがいいところですね!

$ docker run --name rroonga -it groonga/rroonga

チュートリアル

File: tutorial — rroonga - ラングバにRroongaの分かりやすいチュートリアルがあります。
docker run するとirbが立ち上げるのでrequire 'groonga'してライブラリを読み込みます。(-r groongaと同じ効果があります)

irb(main):001:0> require 'groonga'
=> true

チュートリアルに沿ってコマンドを入力していきます。

irb(main):002:0> Groonga::Context.default_options = {:encoding => :utf8}
=> {:encoding=>:utf8}
irb(main):003:0> Groonga::Database.create(:path => "/tmp/bookmark.db")
=> #<Groonga::Database ...>
.
.

簡単なブックマークアプリケーションを作成しながら全文検索ライブラリの基本的な使い方を学ぶことができます。

>> items.add("http://en.wikipedia.org/wiki/Ruby")
=> #<Groonga::Record ...>
>> items.add("http://www.ruby-lang.org/")
=> #<Groonga::Record ...>

ブックマークが10〜100件くらいだと配列から検索してもいいですが、Rroongaでデータベースを構築しておくと数万〜数十万件とデータが増えてもとても高速に検索できます。素晴らしいですね。

>> records = comments.select do |record|
?>   record["content"] =~ "Ruby"
>> end

全文検索エンジンってどんな風に使うんだろう?と興味を持った方はぜひ一度お試しください。Groonga系は日本語の情報がとても多いので最初に始める人にもオススメです。

15
15
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
15
15