[groonga-dev,00494] Re: rroongaでのタグ検索

Back to archive index

Kouhei Sutou kou****@clear*****
2011年 4月 10日 (日) 18:44:56 JST


須藤です。

In <0f503c18-2b04-3812-8297-547136fe5b4c @ api105>
  "[groonga-dev,00493] (無題)" on Sun, 10 Apr 2011 13:04:48 +0900,
  "Endo Akira" <endo4****@goo*****> wrote:

> referenceのvectorを作っての検索ですが、下記ではうまくいかない
> ようです。
> 
> Groonga::Database.create(path: "db1/db")
> Groonga::Schema.create_table("prods", type: :hash) do |table|
>   table.short_text("name")
> end
> Groonga::Schema.create_table("items", type: :hash) do |table|
>   table.reference("prods_ref", "prods", type: :vector)
> end
> prods = Groonga["prods"]
> items = Groonga["items"]
> p1 = prods.add('1',name: 'first')
> p2 = prods.add('2',name: 'second')
> items.add('1',prods_ref: [p1,p2])
> p items.select{|r| r.prods_ref.name =~ 'first'}.size #=> 1
> p items.select{|r| r.prods_ref.name =~ 'second'}.size #=> 0
> 
> 普通のvectorだと全部探してくれるのですが。

groongaのチュートリアルでいうタグ検索と同じパターンかと思い
ました。

  3.6. タグ検索・参照関係の逆引き
    http://groonga.org/docs/tutorial/tutorial06.html

> p items.select{|r| r.prods_ref.name =~ 'first'}.size #=> 1
> p items.select{|r| r.prods_ref.name =~ 'second'}.size #=> 0

もし、nameカラムではなくprodsのキーで検索してもよければこれ
で動くと思います。

  # key_type:を追加。
  Groonga::Schema.create_table("prods", type: :hash, key_type: :string) do |table|
    table.short_text("name")
  end
  # 変更なし
  Groonga::Schema.create_table("items", type: :hash) do |table|
    table.reference("prods_ref", "prods", type: :vector)
  end
  # インデックスを追加
  Groonga::Schema.change_table("prods") do |table|
    table.index("items.prods_ref")
  end

  # nameカラムの値ではなくキーとして登録。
  p1 = prods.add('first')
  p2 = prods.add('second')

  # r.prods_ref.nameではなくr.preds_refで検索。
  p items.select{|r| r.prods_ref =~ 'first'}.size #=> 1
  p items.select{|r| r.prods_ref =~ 'second'}.size #=> 1

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

プログラミングが好きなソフトウェア開発者を募集中:
  http://www.clear-code.com/recruitment/




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