[groonga-dev,00317] Re: Ruby/groongaで転置インデックスを使ったクエリを実行したい

Back to archive index

Kouhei Sutou kou****@clear*****
2010年 1月 19日 (火) 11:00:26 JST


須藤です。

In <4B545****@razil*****>
  "[groonga-dev,00315] Ruby/groongaで転置インデックスを使ったクエリを実行したい" on Mon, 18 Jan 2010 22:00:31 +0900,
  Tasuku SUENAGA <a****@razil*****> wrote:

> Ruby/groongaで以下のような処理をしたいなー、と考えています。
> 
> 1. Communityには複数のUser(users)が属し、
>    最終更新時間(mtime)を持つ。
> 2. Userは主キーが数値(UInt32)である。
>    この主キーは外部から与えられたものである。
> といったスキーマのテーブルがあります。
> 
> ここで、指定されたUserが所属するCommunityのうち、
> 最終更新時間が新しいものを最新n件取得する、という処理をしたいです。
> 
> それっぽい、サンプルを書いてみたのですが、
> 現在は動作していません。
> このサンプルをなるべく変えずに動作するプログラムを得たいなー、と思います。
> 
> おそらく、Groonga::Table#define_index_columnを使わないといけないと思うの
> ですが、あんまり使い方が分かっていなかったりします…
> 助言いただけると幸いです。

groongaっぽくやるならこれで動くようになればいいのかなぁと思
います。あっていますか? > 森さん

  user_table = Groonga::Hash.create(:name => 'User',
                                    :key_type => 'UInt32', # 主キーは数値!
                                    :persistent => true)

  community_table = Groonga::Hash.create(:name => 'Community',
                                         :key_type => 'ShortText',
                                         :persistent => true)
  community_table.define_column('mtime', 'Time', {:persistent => true})
  community_table.define_column('users', user_table,
                                {:persistent => true, :type => :vector})

  user_table.define_index_column('community_index', community_table,
                                 {:persistent => true, :source => "users"})

ただ、実際は動かないのですが。。。

動かない理由は、↓のクエリ文字列「2」が数値の「2」ではなく文
字列の「2」としてパースされるからです。

  recs = community_table.select("users:@2")

これは、groonga本体側の話なのですが、どこでキャストするのが
いいんでしょう。scan_info_build()?


ということで、クエリ文字列を使わずに以下のようにすると動きま
す。

  recs = community_table.select {|r| r['users'] =~ user_table.find(2)}

-- 
須藤 功平 <kou****@clear*****>
株式会社クリアコード <http://www.clear-code.com/> (03-6231-7270)




groonga-dev メーリングリストの案内
Back to archive index