查詢

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

「 向Solr伺服器新增多個文件 」


函式名:SolrClient::addDocuments()

適用版本:PHP 5 >= 5.2.0, PHP 7, PECL solr >= 0.9.2

函式說明:SolrClient::addDocuments() 方法用於向Solr伺服器新增多個文件。

用法:

SolrClient::addDocuments(array $documents [, bool $allowDups = false [, int $commitWithin = 0 ]])

引數:

  • $documents:一個包含多個文件的陣列。每個文件是一個關聯陣列,包含欄位名和對應的值。
  • $allowDups(可選):是否允許重複文件,預設為 false
  • $commitWithin(可選):在指定的時間(以毫秒為單位)內提交更改,預設為 0,表示不進行提交。

示例:

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

// 新增多個文件
$documents = array(
    array('id' => '1', 'title' => 'Document 1', 'content' => 'This is the content of document 1.'),
    array('id' => '2', 'title' => 'Document 2', 'content' => 'This is the content of document 2.'),
    array('id' => '3', 'title' => 'Document 3', 'content' => 'This is the content of document 3.'),
);

$response = $client->addDocuments($documents);

// 檢查新增是否成功
if ($response->success()) {
    echo 'Documents added successfully.';
} else {
    echo 'Failed to add documents. Error: ' . $response->getHttpStatusMessage();
}

// 提交更改
$response = $client->commit();

// 檢查提交是否成功
if ($response->success()) {
    echo 'Changes committed successfully.';
} else {
    echo 'Failed to commit changes. Error: ' . $response->getHttpStatusMessage();
}

注意事項:

  • 在呼叫 addDocuments() 後,需要呼叫 commit() 方法提交更改才能生效。
  • 如果 $allowDups 引數設定為 true,則可以新增重複的文件,否則將會覆蓋已存在的文件。
  • $commitWithin 引數用於設定提交更改的時間限制,如果在指定時間內沒有提交,則更改將被忽略。
補充糾錯
上一個函式: SolrClient::commit()函式
下一個函式: SolrClient::addDocument()函式
熱門PHP函式
分享連結