Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ongaeshi
Created April 10, 2012 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ongaeshi/2352381 to your computer and use it in GitHub Desktop.
Save ongaeshi/2352381 to your computer and use it in GitHub Desktop.
rroonga-test.rb
# -*- coding: utf-8 -*-
#
# @file
# @brief
# @author ongaeshi
# @date 2012/04/11
# @url http://groonga.rubyforge.org/rroonga/ja/file.tutorial.html
# exec:
# $ ruby rroonga-test.rb ; rm bookmark.db*
# output:
# 0
# 2
# #<Groonga::Record:0x00000101182520 @table=#<Groonga::Hash id: <256>, name: <Items>, path: <bookmark.db.0000100>, domain: <ShortText>, range: (nil), flags: <>, encoding: <:utf8>, size: <2>>, @id=1, @added=false>
# #<Groonga::VariableSizeColumn id: <257>, name: <Items.title>, path: <bookmark.db.0000101>, domain: <Items>, range: <Text>, flags: <>>
# #<Groonga::Hash id: <2147483649>, name: (anonymous), path: (temporary), domain: <Items>, range: (nil), flags: <WITH_SUBREC>, encoding: <:utf8>, size: <2>>
# ["http://ja.wikipedia.org/wiki/Ruby", "http://www.ruby-lang.org/ja/"]
# ["http://ja.wikipedia.org/wiki/Ruby", "http://www.ruby-lang.org/ja/"]
# false
# [1, 2010-11-20 18:01:22 +0900, "Matzにっき", "モリタン", "Ruby Matz"]
# [2, 2010-10-07 14:18:28 +0900, "Rubyist Magazine - るびま", "モリタン", "Ruby 記事"]
# [3, 2010-11-11 12:39:59 +0900, "Rubyでgroonga使って全文検索 - ラングバ", "タポロボ", "Ruby groonga 全文検索"]
# [4, 2010-07-28 20:46:23 +0900, "Matzにっき", "タポロボ", "Ruby 日記"]
# [1, 2010-11-20 18:01:22 +0900, "Matzにっき", "モリタン", "Ruby Matz"]
# [3, 2010-11-11 12:39:59 +0900, "Rubyでgroonga使って全文検索 - ラングバ", "タポロボ", "Ruby groonga 全文検索"]
# [2, 2010-10-07 14:18:28 +0900, "Rubyist Magazine - るびま", "モリタン", "Ruby 記事"]
# [4, 2010-07-28 20:46:23 +0900, "Matzにっき", "タポロボ", "Ruby 日記"]
# [2, "http://www.rubyist.net/~matz/", "Matzにっき"]
# [1, "http://jp.rubyist.net/magazine/", "Rubyist Magazine - るびま"]
# [1, "http://groonga.rubyforge.org/", "Rubyでgroonga使って全文検索 - ラングバ"]
# [10, "Rubyでgroonga使って全文検索 - ラングバ"]
# [10, "Ruby"]
# [10, "オブジェクトスクリプト言語Ruby"]
# [2, "Matzにっき"]
# [1, "Rubyist Magazine - るびま"]
require 'rubygems'
require 'groonga'
### データベースの作成
Groonga::Context.default_options = {:encoding => :utf8}
Groonga::Database.create(:path => "bookmark.db")
### テーブルの定義
Groonga::Schema.create_table("Items", :type => :hash)
items = Groonga["Items"]
p items.size
### レコードを追加する
items.add("http://ja.wikipedia.org/wiki/Ruby")
items.add("http://www.ruby-lang.org/ja/")
p items.size
p items["http://ja.wikipedia.org/wiki/Ruby"]
Groonga::Schema.change_table("Items") do |table|
table.text("title")
end
title_column = Groonga["Items.title"]
p title_column
Groonga::Schema.create_table("Terms",
:type => :patricia_trie,
:key_normalize => true,
:default_tokenizer => "TokenBigram")
### 全文検索を行う
Groonga::Schema.change_table("Terms") do |table|
table.index("Items.title")
end
items["http://ja.wikipedia.org/wiki/Ruby"].title = "Ruby"
items["http://www.ruby-lang.org/ja/"].title = "オブジェクトスクリプト言語Ruby"
ruby_items = items.select {|record| record.title =~ "Ruby"}
p ruby_items
p ruby_items.collect {|record| record.key.key}
p ruby_items.collect {|record| record["_key"]}
# マルチユーザ向けのブックマークアプリケーション
Groonga::Schema.create_table("Users", :type => :hash) do |table|
table.text("name")
end
Groonga::Schema.create_table("Comments") do |table|
table.reference("item")
table.reference("author", "Users")
table.text("content")
table.time("issued")
end
Groonga::Schema.change_table("Terms") do |table|
table.index("Comments.content")
end
users = Groonga["Users"]
users.add("moritan", :name => "モリタン")
users.add("taporobo", :name => "タポロボ")
p items.has_key?("http://www.rubyist.net/~matz/")
items.add("http://www.rubyist.net/~matz/", :title => "Matzにっき")
require "time"
comments = Groonga["Comments"]
comments.add(:item => "http://www.rubyist.net/~matz/",
:author => "moritan",
:content => "Ruby Matz",
:issued => Time.parse("2010-11-20T18:01:22+09:00"))
### メソッド化
@items = items
@comments = comments
def add_bookmark(url, title, author, content, issued)
item = @items[url] || @items.add(url, :title => title)
@comments.add(:item => item,
:author => author,
:content => content,
:issued => issued)
end
add_bookmark("http://jp.rubyist.net/magazine/",
"Rubyist Magazine - るびま", "moritan", "Ruby 記事",
Time.parse("2010-10-07T14:18:28+09:00"))
add_bookmark("http://groonga.rubyforge.org/",
"Rubyでgroonga使って全文検索 - ラングバ", "taporobo",
"Ruby groonga 全文検索",
Time.parse("2010-11-11T12:39:59+09:00"))
add_bookmark("http://www.rubyist.net/~matz/",
"Matz日記", "taporobo", "Ruby 日記",
Time.parse("2010-07-28T20:46:23+09:00"))
### 全文検索その2
records = comments.select do |record|
record["content"] =~ "Ruby"
end
records.each do |record|
comment = record.key
p [comment.id,
comment.issued,
comment.item.title,
comment.author.name,
comment.content]
end
# =>
# [1, Sat Nov 20 18:01:22 +0900 2010, "Matzにっき", "モリタン", "Ruby Matz"]
# [2, Thu Oct 07 14:18:28 +0900 2010, "Rubyist Magazine - るびま", "モリタン", "Ruby 記事"]
# [3, Thu Nov 11 12:39:59 +0900 2010, "Rubyでgroonga使って全文検索 - ラングバ", "タポロボ", "Ruby groonga 全文検索検"]
# [4, Wed Jul 28 20:46:23 +0900 2010, "Matzにっき", "タポロボ", "Ruby 日記"]
records.sort([{:key => "issued", :order => "descending"}]).each do |record|
comment = record.key
p [comment.id,
comment.issued,
comment.item.title,
comment.author.name,
comment.content]
end
#=>
# [1, Sat Nov 20 18:01:22 +0900 2010, "Matzにっき", "モリタン", "Ruby Matz"]
# [3, Thu Nov 11 12:39:59 +0900 2010, "Rubyでgroonga使って全文検索 - ラングバ", "タポロボ", "Ruby groonga 全文検索"]
# [2, Thu Oct 07 14:18:28 +0900 2010, "Rubyist Magazine - るびま", "モリタン", "Ruby 記事"]
# [4, Wed Jul 28 20:46:23 +0900 2010, "Matzにっき, "タポロボ", "Ruby 日記"]
records.group("item").each do |record|
item = record.key
p [record.n_sub_records,
item.key,
item.title]
end
# =>
# [2, "http://www.rubyist.net/~matz/", "Matzにっき"]
# [1, "http://jp.rubyist.net/magazine/", "Rubyist Magazine - るびま"]
# [1, "http://groonga.rubyforge.org/", "Rubyでgroonga使って全文検索 - ラングバ"]
### 少し複雑な検索
ruby_comments = @comments.select {|record| record.content =~ "Ruby"}
ruby_items = @items.select do |record|
target = record.match_target do |match_record|
match_record.title * 10
end
target =~ "Ruby"
end
ruby_items = ruby_comments.group("item").union!(ruby_items)
ruby_items.sort([{:key => "_score", :order => "descending"}]).each do |record|
p [record.score, record.title]
end
#=>
# [10, "Rubyist Magazine - るびま"]
# [10, "Ruby"]
# [10, "Rubyでgroonga使って全文検索 - ラングバ"]
# [10, "オブジェクトスクリプト言語Ruby"]
# [2, "Matzにっき"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment