euclid1990 / php-sphinx-search
PHP Sphinx搜索工具。
1.0.1
2016-03-29 15:54 UTC
Requires
- php: >=5.4
- foolz/sphinxql-query-builder: ~1.0
- illuminate/config: ~5.0
- illuminate/support: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.1
This package is not auto-updated.
Last update: 2024-09-14 17:48:28 UTC
README
PHP Sphinx搜索工具(数据库全文搜索的外部解决方案)。支持Composer和Laravel框架。
安装
需要PHP 5.4。
可以通过在项目的composer.json中要求euclid1990/php-sphinx-search包来通过Composer安装PHP Sphinx搜索服务提供者。
{
"require": {
"euclid1990/php-sphinx-search": "~1.0"
},
"minimum-stability": "stable"
}
或
使用Composer要求此包
composer require euclid1990/php-sphinx-search
使用composer update更新包或使用composer install安装。
设置
1. 常规PHP
添加boostrap自动加载文件
require_once __DIR__ . '/../vendor/autoload.php'; use euclid1990\PhpSphinxSearch\SphinxSearch;
2. Laravel
- 要使用Sphinx搜索服务,必须在启动Laravel应用程序时注册提供者。这主要有两种方法。
在config/app.php中找到providers键并注册Sphinx搜索服务提供者。
'providers' => [ // ... 'euclid1990\PhpSphinxSearch\Providers\SphinxSearchServiceProvider', ]
对于Laravel 5.1+
'providers' => [ // ... euclid1990\PhpSphinxSearch\Providers\SphinxSearchServiceProvider::class, ]
在config/app.php中找到aliases键。
'aliases' => [ // ... 'SphinxSearch' => 'euclid1990\PhpSphinxSearch\Facades\SphinxSearch', ]
对于Laravel 5.1+
'aliases' => [ // ... 'SphinxSearch' => euclid1990\PhpSphinxSearch\Facades\SphinxSearch::class, ]
- 如果您需要覆盖默认的配置选项(服务器/端口),请使用config publish命令
php artisan config:publish euclid1990/php-sphinx-search
- 对于Laravel 5.4配置:publish不工作。请使用以下命令代替。
php artisan vendor:publish
用法
1. 常规PHP
请参考demo/run.php或您可以通过以下命令行执行
# php demo/run.php
require_once __DIR__ . '/../vendor/autoload.php'; use euclid1990\PhpSphinxSearch\SphinxSearch; $configArr = require __DIR__.'/../config/sphinx_search.php'; $config = ['sphinx_search' => $configArr]; $sphinxSearch = new SphinxSearch(new Illuminate\Config\Repository($config)); $keyword = 'source code'; $matchs = ['title', 'content']; $table = 'posts'; $columns = ['id', 'title', 'content']; $offset = 0; $limit = 10; $result = $sphinxSearch->search($keyword, $matchs, $table, $columns, $offset, $limit); echo "Search results for keyword: [$keyword].\n"; print_r($result->toArray());
2. Laravel
$keywordEnglish = 'source code'; $keywordVietnamse = 'web nhanh mạnh'; $keywordJapanese = '私は です か'; $matchs = ['title', 'content']; $table = 'posts'; $columns = ['id', 'user_id', 'title', 'content', 'created_at']; // Facade class method $resultEnglish = \SphinxSearch::search($keywordEnglish, $matchs, $table); echo "Search results for English keyword: [$keywordEnglish].\n"; dump($resultEnglish->toArray()); $resultVietnamse = \SphinxSearch::search($keywordVietnamse, $matchs, $table); echo "Search results for Vietnamese keyword: [$keywordVietnamse].\n"; dump($resultVietnamse->toArray()); $resultJapanese = \SphinxSearch::search($keywordJapanese, $matchs, $table); echo "Search results for Japanese keyword: [$keywordJapanese].\n"; dump($resultJapanese->toArray()); // Helper sphinx_search($keyword, $matchs, $table, $columns); // Raw Query $query = "SELECT id FROM posts WHERE MATCH('(@(title,content) source code)') LIMIT 1, 2"; sphinx_raw($query); // Get sphinxQL $sphinxQL = sphinx_ql(); $sphinxQL->select()->from($table)->execute(); // Get sphinxApi $keyword = 'the source'; $tables = 'posts' $resultApi = sphinx_api()->setMatchMode(SPH_MATCH_ANY) ->setFilter('category', [3]) ->query($keyword, $table);
参考
文档
数据库
仅用于测试目的
mysql -u{username} -p{password} {database} < php-sphinx-search/demo/blog.sql
Sphinx
下载
- 发布仓库
- 在本教程中,我使用了Ubuntu 12.04 LTS x86_64 DEB。
sudo dpkg -i sphinxsearch_2.2.10-release-0ubuntu12-precise_amd64.deb
- 如果出错
Package libodbc1 is not installed.
Package unixodbc is not installed.
Package libpq5 is not installed.
运行以下命令
sudo apt-get install mysql-client unixodbc libpq5 libltdl7 odbcinst1debian2 libodbc1 odbcinst
配置
- 安装完成后,我们有了目录:
/etc/sphinxsearch。在运行以下命令之前,请修改php-sphinx-search/demo/sphinx.conf
sudo cp php-sphinx-search/demo/sphinx.conf /etc/sphinxsearch/sphinx.conf
cd /etc/sphinxsearch
sudo mkdir data
sudo chmod -R 777 data
- 必须了解配置参数
- 用于索引和重新索引全文索引。默认情况下,Sphinx读取位于
/etc/sphinxsearch/sphinx.conf的配置文件。
sudo indexer --all
或
sudo indexer --all --rotate
- 启动sphinx服务
sudo vim /etc/default/sphinxsearch
将"START=yes"更改。最后,启动Sphinx守护进程
sudo service sphinxsearch start
或
sudo searchd
- 检查Sphinx是否在端口9306上监听
netstat -anp | grep LIST
- 使用mysql客户端连接
mysql -h0 -P9306