- blogs:
- cles::blog
2009/10/13
[ by hsur at 22:36]
Solr-rubyでmultiValuedなフィールドにデータを入れる
SolrのRubyクライアントであるSolr-rubyはちゃんとドキュメンテーションがされていないので、サンプルに載っていない事をやろうとすると結局ソースを読まなくてはいけないのが難点です。今回はスキーマでmultiValued="true"と指定されているフィールドに複数の値しようと思ったのですが、やり方が分からないので適当にArrayに入れて突っ込んでみたところ、これが偶然通ってしまいました。
#!/bin/ruby
require 'solr'
data = {
:id => 1,
:name => "foo",
:category => ["bar","baz"],
}
doc = Solr::Document.new(data)
conn = Solr::Connection.new('http://localhost:8983/solr', :autocommit => :on)
conn.add(doc)
conn.close
require 'solr'
data = {
:id => 1,
:name => "foo",
:category => ["bar","baz"],
}
doc = Solr::Document.new(data)
conn = Solr::Connection.new('http://localhost:8983/solr', :autocommit => :on)
conn.add(doc)
conn.close
ひとまずやりたい事は達成できたものの、ちょっとこのままでは気持ち悪いので調べてみます。
Solr::Documentクラスにちょうど下記のような部分があり、Hashのvalueになっているオブジェクトがeachメソッドを持っていて、String以外のクラスであるという条件を満たしていれば、eachを使って要素を取り出して追加するようになっていました。
/usr/lib64/ruby/gems/1.8/gems/solr-ruby-0.0.7/lib/solr/document.rb
# Append a Solr::Field
#
# doc << Solr::Field.new(:creator => 'Jorge Luis Borges')
#
# If you are truly lazy you can simply pass in a hash:
#
# doc << {:creator => 'Jorge Luis Borges'}
def <<(fields)
case fields
when Hash
fields.each_pair do |name,value|
if value.respond_to?(:each) && !value.is_a?(String)
value.each {|v| @fields << Solr::Field.new(name => v)}
else
@fields << Solr::Field.new(name => value)
end
end
when Solr::Field
@fields << fields
else
raise "must pass in Solr::Field or Hash"
end
end
#
# doc << Solr::Field.new(:creator => 'Jorge Luis Borges')
#
# If you are truly lazy you can simply pass in a hash:
#
# doc << {:creator => 'Jorge Luis Borges'}
def <<(fields)
case fields
when Hash
fields.each_pair do |name,value|
if value.respond_to?(:each) && !value.is_a?(String)
value.each {|v| @fields << Solr::Field.new(name => v)}
else
@fields << Solr::Field.new(name => value)
end
end
when Solr::Field
@fields << fields
else
raise "must pass in Solr::Field or Hash"
end
end
このエントリは役に立ちましたか?
トラックバックについて
Trackback URL:
お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
このエントリへのTrackbackにはこのURLが必要です→http://blog.cles.jp/item/3250
Trackbacks
このエントリにトラックバックはありません
Comments
愛のあるツッコミをお気軽にどうぞ。[policy]
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
コメントはありません
Comments Form
コメントは承認後の表示となります。
OpenIDでログインすると、即時に公開されます。
OpenID を使ってログインすることができます。
Copyright © 2004-2010 by CLES All Rights Reserved.
sp-20100319043238644595554@cles.net
sp-20100319043238644595554@cles.net
サイト内検索
おしらせ
検索ワード
- 年前 ワード フィールド office
- Yahoo! - 10/03/09 13:19:52
Tags [All Tags]
google | php | ruby | windows | firefox | linux | SoftwareEngineering | java | spam | ranking | curry | camera | seo | FireEmblem | thunderbird | server | marketing | mobile | Doctoral | docomo
へぇが多いエントリ [Top 100]
- おめでとうございます (4)
- 知恵の輪 サターン編 (3)
- SourceForge.JPのSubversion... (3)
- サーバセットアップ (3)
- Thanks ! 10000 Hit ! (3)
- 和食 小錦 (3)
- 散髪しました (3)
- .inはインドのccTLDなのか (3)
- やっと髪をきりました (3)
- 大雪でした (3)
閲覧数が多いエントリ [Top 100]
1 . ドラゴンクエストVIの影響力 [8928x]
2 . やっぱりあった!パクれる読書感想文! [7572x]
3 . Echofon for Firefox [6347x]
4 . 急性胃腸炎 [5705x]
5 . OpenIDで自分のサイトのURLを使う [5704x]
2 . やっぱりあった!パクれる読書感想文! [7572x]
3 . Echofon for Firefox [6347x]
4 . 急性胃腸炎 [5705x]
5 . OpenIDで自分のサイトのURLを使う [5704x]
最新のエントリ [archives]
最新のコメント [Latest 100]
- CD-ROM起動で、HDDを完全消去
- NP_Moblog v1.16
- pinzoro 01/15
- hsur 12/29
- and more...
- 耳がおかしいと思ったら突発..
- baca 01/13
- hsur 01/13
- and more...
カテゴリ別エントリ
cles::blogについて
Syndicate
Calendar
Referrers
Blog People
Admin
★はてな認証APIをつかってログインすることができます。
Powered by NP_Paint




