查詢

SolrClient::addDocument()函式—用法及示例

「 向 Solr 伺服器新增文件 」


SolrClient::addDocument() 函式用於向 Solr 伺服器新增文件。

用法:

public SolrUpdateResponse SolrClient::addDocument(SolrInputDocument $doc [, bool $overwrite = true [, int $commitWithin = 0 ]])

引數:

  • $doc:一個 SolrInputDocument 物件,表示要新增到 Solr 伺服器的文件。
  • $overwrite(可選):一個布林值,指定是否覆蓋現有的文件。預設為 true,表示覆蓋。
  • $commitWithin(可選):一個整數,表示在多少毫秒內對新增的文件進行提交。預設為 0,表示立即提交。

返回值:

  • SolrUpdateResponse 物件,表示新增文件的響應。

示例:

// 建立 Solr 客戶端連線
$options = array(
    'hostname' => 'localhost',
    'port' => 8983,
    'path' => '/solr/'
);
$client = new SolrClient($options);

// 建立一個 SolrInputDocument 物件,並設定欄位值
$doc = new SolrInputDocument();
$doc->addField('id', '1');
$doc->addField('title', 'Example Document');
$doc->addField('content', 'This is an example document for testing purposes.');

// 新增文件到 Solr 伺服器
$response = $client->addDocument($doc);

// 檢查新增文件的響應
if ($response->success()) {
    echo 'Document added successfully.';
} else {
    echo 'Failed to add document. Error: ' . $response->getHttpStatusMessage();
}

在上面的示例中,首先建立了一個 SolrClient 物件來連線 Solr 伺服器。然後,建立一個 SolrInputDocument 物件,並使用 addField() 方法設定欄位值。最後,呼叫 addDocument() 函式將文件新增到 Solr 伺服器。根據新增文件的響應,可以確定是否成功新增了文件。

補充糾錯
下一個函式: sodium_unpad()函式
熱門PHP函式
分享連結