PHP Sphinx search tool (An external solution for database full-text search). Support Composer & Laravel Framework.
PHP 5.4 is required.
The PHP Sphinx Search Service Provider can be installed via Composer by requiring the
euclid1990/php-sphinx-search
package in your
project's composer.json
.
{
"require": {
"euclid1990/php-sphinx-search": "~1.0"
},
"minimum-stability": "stable"
}
or
Require this package with composer:
composer require euclid1990/php-sphinx-search
Update your packages with composer update
or install with composer install
.
Add boostrap autoload file:
require_once __DIR__ . '/../vendor/autoload.php';
use euclid1990\PhpSphinxSearch\SphinxSearch;
- To use the Sphinx Search Service, you must register the provider when bootstrapping your Laravel application. There are essentially two ways to do this.
Find the providers
key in config/app.php
and register the Sphinx Search Service Provider.
'providers' => [
// ...
'euclid1990\PhpSphinxSearch\Providers\SphinxSearchServiceProvider',
]
for Laravel 5.1+
'providers' => [
// ...
euclid1990\PhpSphinxSearch\Providers\SphinxSearchServiceProvider::class,
]
Find the aliases
key in config/app.php
.
'aliases' => [
// ...
'SphinxSearch' => 'euclid1990\PhpSphinxSearch\Facades\SphinxSearch',
]
for Laravel 5.1+
'aliases' => [
// ...
'SphinxSearch' => euclid1990\PhpSphinxSearch\Facades\SphinxSearch::class,
]
- If you need to override the default configuration options (server/port), please use the config publish command
php artisan config:publish euclid1990/php-sphinx-search
- For Laravel 5.4 config:publish does not work. Use the below command instead.
php artisan vendor:publish
Please refer to demo/run.php or you can execute this command line:
# 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());
$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);
How To Install and Configure Sphinx on Ubuntu 14.04
mysql -u{username} -p{password} {database} < php-sphinx-search/demo/blog.sql
- Release Repository
- In this tutorial, I've used Ubuntu 12.04 LTS x86_64 DEB.
sudo dpkg -i sphinxsearch_2.2.10-release-0ubuntu12-precise_amd64.deb
- If have errors:
Package libodbc1 is not installed.
Package unixodbc is not installed.
Package libpq5 is not installed.
Run below command:
sudo apt-get install mysql-client unixodbc libpq5 libltdl7 odbcinst1debian2 libodbc1 odbcinst
- After install completed, We have directory:
/etc/sphinxsearch
. Please modifyphp-sphinx-search/demo/sphinx.conf
before running followings command:
sudo cp php-sphinx-search/demo/sphinx.conf /etc/sphinxsearch/sphinx.conf
cd /etc/sphinxsearch
sudo mkdir data
sudo chmod -R 777 data
- Must learn about config params:
- For indexing and re-indexing full-text indexes. By default, Sphinx reads the configuration file at /etc/sphinxsearch/sphinx.conf.
sudo indexer --all
Or
sudo indexer --all --rotate
- Start sphinx service
sudo vim /etc/default/sphinxsearch
Change "START=yes"
.
Finally, start the Sphinx daemon:
sudo service sphinxsearch start
Or
sudo searchd
- Check Sphinx is listening port 9306:
netstat -anp | grep LIST
- Connect with mysql client:
mysql -h0 -P9306