要验证 SphinxSE 是否已成功编译到 MySQL 中,启动新构建的服务器,运行 MySQL 客户端,并执行 SHOW ENGINES 查询。你应该能看到所有可用引擎的列表,其中 Manticore 应该会显示,并且“Support”列应显示“YES”:
mysql> show engines;
+------------+----------+-------------------------------------------------------------+
| Engine | Support | Comment |
+------------+----------+-------------------------------------------------------------+
| MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance |
...
| SPHINX | YES | Manticore storage engine |
...
+------------+----------+-------------------------------------------------------------+
13 rows in set (0.00 sec)
使用 SphinxSE
要使用 SphinxSE 进行搜索,你需要创建一个特殊的 ENGINE=SPHINX“搜索表”,然后使用 SELECT 语句,并将全文查询放置在查询列的 WHERE 子句中。
以下是一个创建语句和搜索查询的示例:
CREATE TABLE t1
(
id INTEGER UNSIGNED NOT NULL,
weight INTEGER NOT NULL,
query VARCHAR(3072) NOT NULL,
group_id INTEGER,
INDEX(query)
) ENGINE=SPHINX CONNECTION="sphinx://localhost:9312/test";
SELECT * FROM t1 WHERE query='test it;mode=any';
mysql> SELECT content, date_added FROM test.documents docs
-> JOIN t1 ON (docs.id=t1.id)
-> WHERE query="one document;mode=any";
mysql> SHOW ENGINE SPHINX STATUS;
+-------------------------------------+---------------------+
| content | docdate |
+-------------------------------------+---------------------+
| this is my test document number two | 2006-06-17 14:04:28 |
| this is my test document number one | 2006-06-17 14:04:28 |
+-------------------------------------+---------------------+
2 rows in set (0.00 sec)
+--------+-------+---------------------------------------------+
| Type | Name | Status |
+--------+-------+---------------------------------------------+
| SPHINX | stats | total: 2, total found: 2, time: 0, words: 2 |
| SPHINX | words | one:1:2 document:2:2 |
+--------+-------+---------------------------------------------+
2 rows in set (0.00 sec)
通过 MySQL 构建片段
SphinxSE 还具有一个 UDF 函数,允许您通过 MySQL 创建片段。此功能类似于 HIGHLIGHT(),但可以通过 MySQL+SphinxSE 访问。
提供 UDF 的二进制文件称为 sphinx.so,应与 SphinxSE 一起自动构建并安装到适当的位置。如果由于某种原因未能自动安装,请在构建目录中找到 sphinx.so,并将其复制到 MySQL 实例的插件目录中。完成后,使用以下语句注册 UDF:
CREATE FUNCTION sphinx_snippets RETURNS STRING SONAME 'sphinx.so';
函数名称必须是 sphinx_snippets,不能使用其他名称。函数参数如下:
**原型:**函数 sphinx_snippets ( document, table, words [, options] );
document 和 words 参数可以是字符串或表列。选项必须像这样指定:'value' AS option_name。有关支持的选项列表,请参阅 高亮部分。唯一的 UDF 特定附加选项称为 sphinx,允许您指定 searchd 的位置(主机和端口)。
使用示例:
SELECT sphinx_snippets('hello world doc', 'main', 'world',
'sphinx://192.168.1.1/' AS sphinx, true AS exact_phrase,
'[**]' AS before_match, '[/**]' AS after_match)
FROM documents;
SELECT title, sphinx_snippets(text, 'index', 'mysql php') AS text
FROM sphinx, documents
WHERE query='mysql php' AND sphinx.id=documents.id;