2011-06-03 42 views
2

我有一個問題,當我運行PHP的例子的問題,代碼如下:當我嘗試Solr的PHP中

<?php 
    require_once('SolrPhpClient/Apache/Solr/Service.php'); 

    // 
    // 
    // Try to connect to the named server, port, and url 
    // 
    $solr = new Apache_Solr_Service('localhost', '8983', '/solr/'); 

    if (! $solr->ping()) { 
    echo 'Solr service not responding.'; 
    exit; 
    } 

    // 
    // 
    // Create two documents to represent two auto parts. 
    // In practice, documents would likely be assembled from a 
    // database query. 
    // 
    $parts = array(
    'spark_plug' => array(
     'partno' => 1, 
     'name' => 'Spark plug', 
     'model' => array('Boxster', '924'), 
     'year' => array(1999, 2000), 
     'price' => 25.00, 
     'inStock' => true, 
    ), 
    'windshield' => array(
     'partno' => 2, 
     'name' => 'Windshield', 
     'model' => '911', 
     'year' => array(1999, 2000), 
     'price' => 15.00, 
     'inStock' => false, 
    ) 
); 

    $documents = array(); 

    foreach ($parts as $item => $fields) { 
    $part = new Apache_Solr_Document(); 

    foreach ($fields as $key => $value) { 
     if (is_array($value)) { 
     foreach ($value as $datum) { 
      $part->setMultiValue($key, $datum); 
     } 
     } 
     else { 
     $part->$key = $value; 
     } 
    } 

    $documents[] = $part; 
    } 

    // 
    // 
    // Load the documents into the index 
    // 
    try { 
    $solr->addDocuments($documents); 
    $solr->commit(); 
    $solr->optimize(); 
    } 
    catch (Exception $e) { 
    echo $e->getMessage(); 
    } 

    // 
    // 
    // Run some queries. Provide the raw path, a starting offset 
    // for result documents, and the maximum number of result 
    // documents to return. You can also use a fourth parameter 
    // to control how results are sorted and highlighted, 
    // among other options. 
    // 
    $offset = 0; 
    $limit = 10; 

    $queries = array(
    'partno: 1 OR partno: 2', 
    'model: Boxster', 
    'name: plug' 
); 

    foreach ($queries as $query) { 
    $response = $solr->search($query, $offset, $limit); 

    if ($response->getHttpStatus() == 200) { 
     // print_r($response->getRawResponse()); 

     if ($response->response->numFound > 0) { 
     echo "$query <br />"; 

     foreach ($response->response->docs as $doc) { 
      echo "$doc->partno $doc->name <br />"; 
     } 

     echo '<br />'; 
     } 
    } 
    else { 
     echo $response->getHttpStatusMessage(); 
    } 
    } 
?> 

但是一旦運行顯示以下錯誤:

'400' Status: Bad Request 
Fatal error: Uncaught exception 'Apache_Solr_HttpTransportException' with message ''400' Status: Bad Request' in C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php:338 Stack trace: #0 C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php(1170): Apache_Solr_Service->_sendRawGet('http://localhos...') #1 C:\software\study\php\xampp\xampp\htdocs\solr1.php(98): Apache_Solr_Service->search('partno: 1 OR pa...', 0, 10) #2 {main} thrown in C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php on line 338 

可能是什麼問題。

所有幫助表示讚賞。

回答

6

您可能有(a)錯誤的查詢語法,或(b)錯誤配置的索引。

檢查你的索引

看起來你對PARTNO,型號等你需要確保這些都是在你的schema.xml配置的領域。我可能猜測你沒有在那裏創建它們,所以你的文檔提交失敗,或者你有一個模型領域,但它沒有多值。

如果它越來越近提交文件,那麼你可能有一個查詢問題...

調試查詢

調試您的查詢的最簡單的方法是在發送前以呼應查詢。在這種情況下:

echo $query 

以輸出從命中:

http://localhost:8983/solr/select?q=<query> 

(其中是回聲語句的查詢)。

您可能會收到400錯誤。我的猜測是你有一個壞的字段名稱在那裏或在你的查詢中使用無效的語法。如果您可以發佈$ query,它可以更容易地解決問題。

+0

非常感謝!問題是錯誤配置的索引,我是Solr的新手。 – user763344 2011-06-05 02:56:21

+1

同樣在這裏,謝謝。 Solr也是新手,看起來我試圖添加默認「演示」索引中不存在的字段。 – fernandojmartin 2011-08-05 17:32:29