簡要描述:
- 基于sql的全文檢索引擎,可以結合DB:MySQL、PostgreSQL,同時為MySQL設計了存儲引擎插件。
- 提供API搜索接口,支持多語言,如PHP、Python、Perl、Ruby等。
- 特點:高速索引 (在新款CPU上,近10 MB/秒); 高速搜索 (2-4G的文本量中平均查詢速度不到0.1秒); 高可用性 (單CPU上最大可支持100 GB的文本,100M文檔); 提供良好的相關性排名 支持分布式搜索; 提供文檔摘要生成; 提供從MySQL內部的插件式存儲引擎上搜索 支持布爾,短語, 和近義詞查詢; 支持每個文檔多個全文檢索域(默認最大32個); 支持每個文檔多屬性; 支持斷詞; 支持單字節編碼與UTF-8編碼
安裝步驟
- 參見備注資料
主要配置:
** sphinx.conf **
source mysql
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents
#sql_query第一列id需為整數
#title、content作為字符串/文本字段,被全文索引
sql_attr_uint = group_id #從SQL讀取到的值必須為整數
sql_attr_timestamp = date_added #從SQL讀取到的值必須為整數,作為時間屬性
sql_query_info_pre = SET NAMES utf8 #命令行查詢時,設置正確的字符集
sql_query_info = SELECT * FROM documents WHERE id=$id #命令行查詢時,從數據庫讀取原始數據信息
}
index mysql
{
source = mysql #對應的source名稱
path = var/data/mysql #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
#charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環境下設置,/符號結尾
charset_dictpath = etc/ #Windows環境下設置,/符號結尾,最好給出絕對路徑,例如:C:/usr/local/coreseek/etc/...
charset_type = zh_cn.utf-8
}
indexer
{
mem_limit = 128M
}
searchd
{
listen = 9312
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = var/log/searchd_mysql.pid #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
log = var/log/searchd_mysql.log #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
query_log = var/log/query_mysql.log #請修改為實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
binlog_path = #關閉binlog日志
}
配置項說明
參數名 | 說明 |
---|---|
source | 定義數據索引源(就是被搜索的數據啦),如果以mysql為索引源,那么source里的信息包含數據庫賬號、密碼、端口、獲取數據索引的sql語句等 |
index | 定義如何處理索引源,例如索引文件目錄、分詞單位、分詞配置文件、去除數據的html標簽等 |
indexer | 定義indexer服務設置,例如內存使用大小限制、文件索引大小限制 |
searchd | 定義searchd服務設置,用于搜索時的設置,例如服務端口、搜索最大數量限制、搜索超時時間等 |
接口調用示例
<?php
$s = new SphinxClient();
$s->setServer('127.0.0.1', 9312);
$result = $s->Query('max', 'in_bbs_test');
echo json_encode($result);
exit;
返回結果示例
{
error: "",
warning: "",
status: 0,
fields: [
"fid",
"tid",
"first",
"author",
"authorid",
"subject",
"dateline",
"message",
"useip",
"port",
"invisible",
"anonymous",
"usesig",
"htmlon",
"bbcodeoff",
"smileyoff",
"parseurloff",
"attachment",
"rate",
"ratetimes",
"status",
"tags",
"comment",
"replycredit",
"position"
],
attrs: [
],
matches: {
},
total: "1000",
total_found: "4139",
time: "0.001",
words: {
max: {
docs: "4139",
hits: "8928"
}
}
}
參考資料