Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Created May 23, 2012 10:11
Show Gist options
  • Save hajimehoshi/2774404 to your computer and use it in GitHub Desktop.
Save hajimehoshi/2774404 to your computer and use it in GitHub Desktop.
Rroonga Test
# -*- coding: utf-8 -*-
require 'groonga'
Groonga::Database.create(path: '/tmp/test.db')
Groonga::Schema.define do |schema|
schema.create_table('ChannelNames',
type: :hash,
key_type: 'ShortText') do |table|
end
schema.create_table('Messages',
type: :hash,
key_type: 'UInt64') do |table|
table.reference('channel_name', 'ChannelNames')
table.text('body')
end
schema.create_table('Terms',
type: :patricia_trie,
default_tokenizer: 'TokenBigram',
key_normalize: true) do |table|
table.index('Messages.body', with_position: true)
end
schema.change_table('ChannelNames') do |table|
table.index('Messages.channel_name', with_position: true)
end
end
entries = Groonga['Messages']
entries.add(1, body: 'foo', channel_name: 'ぴよ')
entries.add(2, body: 'bar', channel_name: 'ぴよ')
entries.add(3, body: 'baz', channel_name: 'ぴよ')
entries.add(4, body: 'foo', channel_name: 'ほげ')
entries.add(5, body: 'bar', channel_name: 'ほげ')
entries.add(6, body: 'baz', channel_name: 'ぴよふが')
result = entries.select do |record|
in_channel = ['ぴよ', 'ほげ'].map do |channel_name|
record.channel_name == channel_name
end.inject do |result, query|
result | query
end
in_channel
end.select do |record|
record.body =~ 'ba'
end
=begin
# 次だとうまくいかないヨ
result = entries.select do |record|
include_keyword = record.body =~ 'ba'
in_channel = ['ぴよ', 'ほげ'].map do |channel_name|
record.channel_name == channel_name
end.inject do |result, query|
result | query
end
include_keyword & in_channel
end
=end
result.each do |record|
p [record.body, record.channel_name.key]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment