SolrClient
SolrClient
是 Apache Solr PHP 扩展中的一个类,用于与 Solr 服务器进行交互。它提供了一套方法来执行搜索、添加、更新和删除文档等操作。
以下是如何创建 SolrClient
并使用它执行查询的示例:
$options = array(
'hostname' => 'localhost',
'port' => 8983,
'path' => 'solr/mycore'
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('test');
$query->setStart(0);
$query->setRows(50);
$query->addField('title')->addField('id');
$query_response = $client->query($query);
$response = $query_response->getResponse();
print_r($response);
在这个例子中,首先创建了一个 SolrClient
实例,并设置了 Solr 服务器的主机名、端口和核心路径。然后,创建了一个 SolrQuery
对象并设置了查询参数,包括查询字符串、结果起始位置、返回的行数以及需要返回的字段。最后,通过调用 SolrClient
的 query
方法执行查询,并获取响应。
请注意,你需要根据你的 Solr 服务器设置修改主机名、端口和核心路径。