[groonga-dev,00949] Re: rroonga での scoreの設定方法

Back to archive index

Shinya Kawaji kawaj****@gmail*****
2012年 6月 11日 (月) 15:56:04 JST


かわじです。



> あ、これはですね、↓のようにするのが正しいです。
> 
>   class ActiveGroonga::ResultSet
>     def sort_by_shuffle(opts = {})
>       @records.select("_score = rand() && false", :syntax => :script)
>       sort(["_score"], opts)
>     end
>   end

詳しく教えてくださり、ありがとうございます。

ただちょっと、上の書き方では以下の点が上手くいかないように思うのですが、
一般的にも再現する話でしょうか。


■繰り返し行ってもランダムな値を返さない

(irbでテストしています)

> Item.select{|t| t.name_key =~ 'サンプル' }.n_records
=> 300
> Item.select{|t| t.name_key =~ 'サンプル' }.sort_by_shuffle({limit: 5}).collect(&:key)
=> [1006, 177811, 56518, 56078, 56457]
> Item.select{|t| t.name_key =~ 'サンプル' }.sort_by_shuffle({limit: 5}).collect(&:key)
=> [1006, 177811, 56518, 56078, 56457]
> Item.select{|t| t.name_key =~ 'サンプル' }.sort_by_shuffle({limit: 5}).collect(&:key)
=> [1006, 177811, 56518, 56078, 56457]

私の意図するところでは、同じ検索キーで検索しても、その都度「keyの配列」
は新しい値を返すはずなのですが、同じ配列ばかり帰ってきます。


 ■Item.all.sort_by_shuffle と続けられない

> Item.all.sort_by_shuffle({limit: 5}).collect(&:key)
Groonga::SyntaxError: syntax error: Syntax error! (_score = rand() && false): [#<Groonga::Expression noname($1:""){}>, ["_score = rand() && false", {:syntax=>:script, :allow_pragma=>nil, :allow_column=>nil, :allow_update=>nil, :default_column=>nil}]]
ecmascript.y:19: yy_syntax_error()
  from /path/to/lib/ruby/gems/1.9.1/gems/rroonga-2.0.4/lib/groonga/expression-builder.rb:396:in `parse'
  from /path/to/lib/ruby/gems/1.9.1/gems/rroonga-2.0.4/lib/groonga/expression-builder.rb:396:in `build'
  from /path/to/lib/ruby/gems/1.9.1/gems/rroonga-2.0.4/lib/groonga/expression-builder.rb:91:in `build_expression'
  from /path/to/lib/ruby/gems/1.9.1/gems/rroonga-2.0.4/lib/groonga/expression-builder.rb:45:in `build'
  from (irb):3:in `select'
  from (irb):3:in `sort_by_shuffle'
  from (irb):14
  from /path/to/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
  from /path/to/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
  from /path/to/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'


all と select の両メソッドは、「絞り込みをするかしないか」くらいの違いし
か無いという認識だったのですが、なぜか all のほうは動作しませんでした。


何度もお手数ですが、どうぞよろしくお願いします。




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