rikiless/sphinx-search

用于查询 Sphinx 搜索的简单处理程序

0.9.4 2014-10-06 11:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:44:24 UTC


README

Sphinx API 客户端的处理程序。它包含设置字段权重、过滤、排序和多查询的大部分常用功能。

在 Sphinx 服务器 2.1.9 / 2.2.4 和 sphinx PECL 包 1.3.2 上进行了测试。

要求

此包需要 PHP 5.4。

安装

安装此包的最佳方式是使用Composer

$ composer require "rikiless/sphinx-search:@dev"

Nette 框架

如果您使用 Nette 框架,可以简单地注册服务

sphinx:
		host: localhost
		port: 9312

services:
	- Rikiless\Sphinx\Search(%sphinx%)

在演示者中

class Presenter ...
{

	/** @var Rikiless\Sphinx\Search @inject */
	public $fulltextSearch;

}

使用

示例

简单查询

$fulltext = new Rikiless\Sphinx\Search([
    'host' => 'localhost',
    'port' => 9312
]);

try {
 	/** @var Rikiless\Sphinx\Data $results */
    $results = $fulltext->query('search something');
	var_dump($results->getMatchesList());
	
} catch (Rikiless\Sphinx\Exception $e) {
    print $e->getMessage();
}

具有基本设置的多个查询

$search = 'search something';

$this->fulltextSearch->setIndex('myindex');
$this->fulltextSearch->setLogComment(sprintf('Fulltext query on %s', $this->domain));
$this->fulltextSearch->setFieldWeights([
    'name' => 10,
	'content' => 5,
	'subject_name' => 3,
	'city' => 2,
	'contact_person' => 2
]);

$this->fulltextSearch->resetFilters();
$this->fulltextSearch->setFilterRange('position', 0, 19999999);
$this->fulltextSearch->addQuery($search);

$this->fulltextSearch->resetFilters();
$this->fulltextSearch->setFilterRange('position', 20000000, 29999999);
$this->fulltextSearch->addQuery($search);

try {
    $results = $this->fulltextSearch->runQueries();
} catch (Rikiless\Sphinx\Exception $e) {
    print $e->getMessage();
}

foreach ($results as $row) {
	/** @var Rikiless\Sphinx\Data $row */
	var_dump($row->getMatches());
}

来源