在PHP中如何在Apache Solr

时间:2015-07-09 11:54:11

标签: php solr

我正在使用PHP Apache Solr Search Engine Extensions,我正在尝试更新Apache Solr中的现有索引。我知道Solr中没有实际的更新,所以我要做的是以下(update_by_id):

    include "bootstrap.php";
    $options = array
        (
        'hostname' => SOLR_SERVER_HOSTNAME,
        'login' => SOLR_SERVER_USERNAME,
        'password' => SOLR_SERVER_PASSWORD,
        'port' => SOLR_SERVER_PORT,
    );

    $client = new SolrClient($options);

    $query = new SolrQuery();

    $query->setQuery('id:1000012');

    $query->setStart(0);

    $query->setRows(1);

//        $query->addField('cat')->addField('features')->addField('id')->addField('timestamp');

    $query_response = $client->query($query);

    $response = $query_response->getResponse();

    if ($response->response->numFound) {
        $second_doc = new SolrInputDocument();
        $second_doc->addField('cat', 'TESTCAT');
        $second_doc->merge($response, true);
        $updateResponse = $client->addDocument($second_doc);
        $client->commit();
    }

但是我收到以下错误:

  

可捕获致命错误:传递给SolrInputDocument :: merge()的参数1必须是SolrInputDocument的实例,第44行/var/www/html/solr-test.dev/update.php中给出的SolrObject实例

如果这不是正确的方法,那么它是什么?

Solr信息:

Solr Specification Version: 3.6.2.2014.11.01.05.22.12
Solr Implementation Version: 3.6.2 debian - buildd - 2014-11-01 05:22:12
Lucene Specification Version: 3.6.2
Lucene Implementation Version: 3.6.2 debian - buildd - 2014-11-01 05:19:47

1 个答案:

答案 0 :(得分:0)

在完成@Random的一些指示后,我最终得到了它。这就是我设法做到的方式:

include "bootstrap.php";
$options = array
(
    'hostname' => SOLR_SERVER_HOSTNAME,
    'login' => SOLR_SERVER_USERNAME,
    'password' => SOLR_SERVER_PASSWORD,
    'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('id:1000012');
$query->setStart(0);
$query->setRows(1);
$query_response = $client->query($query);
// I had to set the parsemode to PARSE_SOLR_DOC
$query_response->setParseMode(SolrQueryResponse::PARSE_SOLR_DOC);
$response = $query_response->getResponse();
$doc = new SolrInputDocument();
// used the getInputDocument() to get the old document from the query
$doc = $response->response->docs[0]->getInputDocument();
if ($response->response->numFound) {
    $second_doc = new SolrInputDocument();
    $second_doc->addField('cat', "TESTCAT");
// Notice I removed the second parameter from the merge()
    $second_doc->merge($doc);
    $updateResponse = $client->addDocument($second_doc);
    $client->commit();
}