English | 简体中文 | 繁體中文
查询

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函数
分享链接