Sphinx is a powerful documentation generator that has many great features for writing technical documentation
Sphinx 3
Sphinx is a free, dual-licensed search server. Sphinx is written in C++, and focuses on query performance and search relevance.
The primary client API is currently SphinxQL, a dialect of SQL. Almost any MySQL connector should work. Additionally, basic HTTP/JSON API and native APIs for a number of languages (PHP, Python, Ruby, C, Java) are provided.
This document is an effort to build a better documentation for Sphinx v.3.x and up. Think of it as a book or a tutorial which you could actually read; think of the previous “reference manual” as of a “dictionary” where you look up specific syntax features. The two might (and should) eventually converge.
Getting started on Linux (and MacOS)
Versions and file names will vary, and you most likely will want to configure Sphinx at least a little, but for an immediate quickstart:
$ wget -q http://sphinxsearch.com/files/sphinx-3.3.1-b72d67b-linux-amd64.tar.gz
$ tar zxf sphinx-3.3.1-b72d67b-linux-amd64.tar.gz
$ cd sphinx-3.3.1-b72d67b-linux-amd64/bin
$ ./searchd
Sphinx 3.3.1 (commit b72d67b)
Copyright (c) 2001-2020, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
listening on all interfaces, port=9312
listening on all interfaces, port=9306
WARNING: No extra index definitions found in data folder
$
That’s it; the daemon should now be running and accepting connections on port 9306. And you can connect to it using MySQL CLI (see below for more details, or just try mysql -P9306
right away).
Running queries via MySQL shell
mysql -h 127.0.0.1 -P 9306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 3.3.1 (commit b72d67b)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Configuration
This extension interacts with Sphinx search daemon using MySQL protocol and SphinxQL query language. In order to setup Sphinx “searchd” to support MySQL protocol following configuration should be added:
create a sphinx.conf like this:
#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
source base
{
type = mysql
sql_host = sql_host
sql_user = sql_user
sql_pass = sql_pass
sql_port = 3306 # optional, default is 3306
}
source source_test : base
{
sql_db = test
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT t.id, t.status, UNIX_TIMESTAMP(t.date) as date, t.subject, t.content \
FROM tb_test t
# attr
sql_attr_uint = status
sql_attr_bigint = date
# fields
sql_field_string = subject
sql_field_string = content
}
index idx_test
{
source = source_test
path = ./sphinxdata/indexes/idx_test
# Strip html
html_strip = 1
# Supoort CJK (Chinese, Japanese, Korean)
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 127.0.0.1:9312
listen = 127.0.0.1:9306:mysql41
log = ./sphinxdata/logs/searchd.log
query_log = ./sphinxdata/logs/query.log
read_timeout = 5
max_children = 30
pid_file = ./sphinxdata/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
binlog_path = ./sphinxdata/binlogs
}
Start searchd with configuration
by default searchd load sphinx.conf in current directory, you can speficy config file by using -c or –config
# Start searchd
./searchd --config /path/to/sphinx.conf
# Using indexer
./indexer --all --rotate
Sphinx Extension for Yii 2
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yiisoft/yii2-sphinx
or add
"yiisoft/yii2-sphinx": "~2.0.0"
To use this extension, simply add the following code in your application configuration:
return [
//....
'components' => [
'sphinx' => [
'class' => 'yii\sphinx\Connection',
'dsn' => 'mysql:host=127.0.0.1;port=9306;',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'tablePrefix' => 'idx_',
],
],
];
Search by sphinx
<?php
use yii\sphinx\Query as SphinxQuery;
public function actionSearch() {
$q = 'test';
$st = time() * 1000;
$results = (new SphinxQuery())
->select('*')
->from('{{%test}}')
->match(
(new MatchExpression())
->match(['subject' => $q])
->orMatch(['content' => $q])
)
->facets([
'subject',
'content',
])
->search();
print_r($results);
}
?>
Search engines comparison
Sphinx | Solr | Elasticsearch | Xapian | |
---|---|---|---|---|
Indexing speed (Mb/s) | 4.5 | 2.75 | 3.8 | 1.36 |
Search speed (ms) | 7/75 | 25/212 | 10/212 | 14/135 |
Index size (%) | 30 | 20 | 20 | 200 |
Realization | Server | Server | Library | Library |
Interface | API, SQL | Web-service | API | API |
Search operators | Boolean, prefix search, exact phrase, words near, ranges, word order, zones | Boolean, prefix search (+ wildcards), exact phrase, words near, ranges, approximate search | Boolean, prefix search (+ wildcards), exact phrase, words near, ranges, approximate search | Boolean, prefix search, exact phrase, words near, ranges, approximate search |
Table 1 – Search engines comparison
结束!