file_get_contentsとかでlocalhostを叩いても可能ではあるが、Apache Solrを使ってみることにする。
Apache Solr のバージョン 1.3 および 1.4 の両方と互換性があります。
上述のように記載されているが3.6でもとりあえずドキュメントの追加はできる。
■php-pecl-solrのインストール
面倒なのでremiを使う。
yum install --enablerepo=remi,epel php-pecl-solr
mac
peclでインストールする。
pecl install solr
windowsは知らん。
■ドキュメントの追加
以下のようにして、ドキュメントを追加する事ができる。
$client = new SolrClient(array( 'hostname' => 'localhost', 'port' => 8983, )); var_dump($client); $doc = new SolrInputDocument(); $doc->addField('id', '1'); $doc->addField('name', '梅雨'); $doc->addField('value', '蒸し暑い,湿気'); $doc->addField('is_public', true); $updateResponse = $client->addDocument($doc); $updateResponse = $client->commit();// commitしないとindexが更新されない var_dump($updateResponse);
■ドキュメントの取り出し
以下のようにしてクエリを発行する事ができる。
$client = new SolrClient(array( 'hostname' => 'localhost', 'port' => 8983, )); $query = new SolrQuery('*:*'); $query->setStart(0); $query->setRows(300); $query_response = $client->query($query); $response = $query_response->getResponse(); var_dump($response); /* object(SolrObject)#4 (2) { ["responseHeader"]=> object(SolrObject)#5 (3) { ["status"]=> int(0) ["QTime"]=> int(0) ["params"]=> object(SolrObject)#6 (4) { ["indent"]=> string(2) "on" ["wt"]=> string(3) "xml" ["q"]=> string(3) "*:*" ["version"]=> string(3) "2.2" } } ["response"]=> object(SolrObject)#7 (3) { ["numFound"]=> int(2) ["start"]=> int(0) ["docs"]=> array(2) { [0]=> object(SolrObject)#8 (8) { ["created"]=>int(123456789) ["face"]=>string(5) "梅雨" ["id"]=>string(1) "1" ["is_public"]=>bool(true) ["tag"]=>string(15) "蒸し暑い,湿気" } [1]=> object(SolrObject)#9 (8) { ["created"]=>int(123456789) ["face"]=>string(12) "秋雨" ["id"]=>string(1) "2" ["is_public"]=>bool(true) ["tag"]=>string(15) "蒸し暑い,湿気" } } } } */
wt=phpとクエリを投げるとphpの配列で返ってくるのだが以下のメソッドがどうも上手く機能しない。
$client->setResponseWriter("phpnative"); //SolrResponse::getResponse(): Error unserializing raw response. //Uncaught exception 'SolrException' with message 'Error un-serializing response'