Python
Solrには、Python応答ライター用の出力フォーマットが特別な形式で含まれていますが、JSON応答ライターは少し堅牢です。
シンプルなPython
クエリを行うのは難しいことではありません。最初に、HTTP接続を行う必要があることをPythonに知らせます。
from urllib2 import *
サーバーへの接続を開き、応答を取得します。wt
クエリパラメーターは、Pythonが理解できる形式の結果をSolrに返却するよう指示します。
connection = urlopen('https://127.0.0.1:8983/solr/collection_name/select?q=cheese&wt=python')
response = eval(connection.read())
応答を解釈する場合は、必要な情報を引き出すだけです。
print response['response']['numFound'], "documents found."
# Print the name of each document.
for document in response['response']['docs']:
print " Name =", document['name']
JSONを使用したPython
JSONはより堅牢な応答フォーマットであり、Pythonはバージョン2.6以降の標準ライブラリでそれに対してサポートしています。
クエリを実行する方法はほぼ一緒です。ただし、wt
クエリパラメーターが現在json
(wt
パラメーターが指定されていない場合もデフォルト)であり、応答がjson.load()
で処理されていることに注意してください。
from urllib2 import *
import json
connection = urlopen('https://127.0.0.1:8983/solr/collection_name/select?q=cheese&wt=json')
response = json.load(connection)
print response['response']['numFound'], "documents found."
# Print the name of each document.
for document in response['response']['docs']:
print " Name =", document['name']