Ruby
Solr 有一個可選的 Ruby 回應格式,它擴展了 JSON 回應寫入器,允許 Ruby 的直譯器安全地評估回應
此 Ruby 回應格式與 JSON 的不同之處如下
-
Ruby 的單引號字串用於防止可能的字串漏洞
-
\
和'
是唯二被跳脫的字元… -
未使用 Unicode 跳脫… 資料以原始 UTF-8 寫入
-
-
nil 用於 null
-
=>
用作對應中的鍵/值分隔符
以下是來自 Solr 的 Ruby 回應範例,適用於類似 https://127.0.0.1:8983/solr/techproducts/select?q=iPod&wt=ruby&indent=on
的請求(使用 bin/solr start -e techproducts
啟動 Solr)
{
'responseHeader'=>{
'status'=>0,
'QTime'=>0,
'params'=>{
'q'=>'iPod',
'indent'=>'on',
'wt'=>'ruby'}},
'response'=>{'numFound'=>3,'start'=>0,'docs'=>[
{
'id'=>'IW-02',
'name'=>'iPod & iPod Mini USB 2.0 Cable',
'manu'=>'Belkin',
'manu_id_s'=>'belkin',
'cat'=>['electronics',
'connector'],
'features'=>['car power adapter for iPod, white'],
'weight'=>2.0,
'price'=>11.5,
'price_c'=>'11.50,USD',
'popularity'=>1,
'inStock'=>false,
'store'=>'37.7752,-122.4232',
'manufacturedate_dt'=>'2006-02-14T23:55:59Z',
'_version_'=>1491038048794705920},
{
'id'=>'F8V7067-APL-KIT',
'name'=>'Belkin Mobile Power Cord for iPod w/ Dock',
'manu'=>'Belkin',
'manu_id_s'=>'belkin',
'cat'=>['electronics',
'connector'],
'features'=>['car power adapter, white'],
'weight'=>4.0,
'price'=>19.95,
'price_c'=>'19.95,USD',
'popularity'=>1,
'inStock'=>false,
'store'=>'45.18014,-93.87741',
'manufacturedate_dt'=>'2005-08-01T16:30:25Z',
'_version_'=>1491038048792608768},
{
'id'=>'MA147LL/A',
'name'=>'Apple 60 GB iPod with Video Playback Black',
'manu'=>'Apple Computer Inc.',
'manu_id_s'=>'apple',
'cat'=>['electronics',
'music'],
'features'=>['iTunes, Podcasts, Audiobooks',
'Stores up to 15,000 songs, 25,000 photos, or 150 hours of video',
'2.5-inch, 320x240 color TFT LCD display with LED backlight',
'Up to 20 hours of battery life',
'Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video',
'Notes, Calendar, Phone book, Hold button, Date display, Photo wallet, Built-in games, JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, Rechargeable capability, Battery level indication'],
'includes'=>'earbud headphones, USB cable',
'weight'=>5.5,
'price'=>399.0,
'price_c'=>'399.00,USD',
'popularity'=>10,
'inStock'=>true,
'store'=>'37.7752,-100.0232',
'manufacturedate_dt'=>'2005-10-12T08:00:00Z',
'_version_'=>1491038048799948800}]
}}
這是一個簡單範例,說明如何使用 Ruby 回應格式查詢 Solr
require 'net/http'
h = Net::HTTP.new('localhost', 8983)
http_response = h.get('/solr/techproducts/select?q=iPod&wt=ruby')
rsp = eval(http_response.body)
puts 'number of matches = ' + rsp['response']['numFound'].to_s
#print out the name field for each returned document
rsp['response']['docs'].each { |doc| puts 'name field = ' + doc['name'] }
對於與 Solr 的簡單互動,這可能就是您所需要的一切!如果您正在建立與 Solr 的複雜互動,請考慮 Solr Wiki 中提到的程式庫。