您好,登錄后才能下訂單哦!
這篇文章主要講解了“sphinx怎么實(shí)現(xiàn)多表查詢”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“sphinx怎么實(shí)現(xiàn)多表查詢”吧!
sql_query = SELECT `id`, 2 AS table_id, `username`, `password`, `email`, `salt`, `from` FROM table_2
["matches"]=>
array(16) {
[0]=>
array(3) {
["id"]=>
string(2) "68"
["weight"]=>
string(1) "2"
["attrs"]=>
array(1) {
["table_id"]=>
string(1) "2"
好吧,我又無(wú)聊了 Orz
之前搭建的褲子庫(kù)是單表的,建索引的時(shí)候也沒(méi)考慮什么后續(xù)擴(kuò)展,有小伙伴表示要玩多表查詢,于是研究了下……
為嘛不用增量索引呢?本來(lái)一個(gè)表就10G多夠大了,而且增量索引還得不時(shí)添加&合并索引……只是本機(jī)搭著玩玩,還是算了吧。
翻到一篇文章,里面說(shuō)到了配置文件里的繼承和重載,對(duì)于添加多個(gè)數(shù)據(jù)源還是挺有幫助的,摘抄下:
定義父類email
source email { .... }
定義子類subemail繼承email類的所有設(shè)置:
source subemail : email { #除了source,index也可以使用繼承 .... }
子類中可以重載email中的設(shè)置
source subemail : email { sql_host = www.ibos.com.cn #重載主機(jī) sql_query = SELECT * FROM subemail #重載sql_query語(yǔ)句 }
其實(shí)繼承很少被使用到,但有一個(gè)很實(shí)用的例子就是有很多數(shù)據(jù)源使用同一數(shù)據(jù)庫(kù)的時(shí)候,繼承就派上用場(chǎng)了
source setdb { #setdb類只實(shí)現(xiàn)連接數(shù)據(jù)庫(kù) sql_host = localhost sql_user = root sql_pass = root sql_db = ibos sql_port = 3306 } souce email : setdb{ #繼承setdb類 sql_query = ... #直接寫查詢語(yǔ)句,而不用再寫數(shù)據(jù)庫(kù)信息 } souce diary : setdb { sql_query = ... } souce article : setdb { sql_query = ... } souce forum : setdb { sql_query = ... }
然后我在上一次的配置文件基礎(chǔ)上改了改,加上了另一個(gè)表做數(shù)據(jù)源。
但是在改 php 文件時(shí)發(fā)現(xiàn)個(gè)問(wèn)題:之前的源碼里用的是 $sql = "select * from table_1 where id in($ids)"
,現(xiàn)在加了另一個(gè)表以后就不好搞了。
因?yàn)閮蓚€(gè)表的 ID 字段都是從 1 開(kāi)始自增的,如果用多表 Union 的話可能把兩個(gè)表中的同一個(gè) ID 的行取出來(lái)。有個(gè)解決辦法就是把第二個(gè)表 ID 的自增起始數(shù)值改成第一個(gè)表 ID 的結(jié)束數(shù)值—— 不過(guò)這個(gè)方法只適用極少的情況……
接著百度了很久無(wú)果,谷歌也搜不到…… 后來(lái)把搜索關(guān)鍵詞換為 coreseek indexes in different tables
就搜到一大堆了 Orz
根據(jù)這個(gè) Using Sphinx with PHP with multiple indexes 的回答,把配置文件重新改了下:
#源定義 source table_1 { type = mysql sql_host = localhost sql_user = test sql_pass = test sql_db = sed sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT `id`, 1 AS table_id, `username`, `password`, `email`, `salt`, `from` FROM table_1 sql_attr_uint = table_id #從SQL讀取到的值必須為整數(shù) #sql_attr_timestamp = date_added #從SQL讀取到的值必須為整數(shù),作為時(shí)間屬性 sql_query_info_pre = SET NAMES utf8 #命令行查詢時(shí),設(shè)置正確的字符集 sql_query_info = SELECT * WHERE ID=$id #命令行查詢時(shí),從數(shù)據(jù)庫(kù)讀取原始數(shù)據(jù)信息 } source table_2 : table_1 { sql_query = SELECT `id`, 2 AS table_id, `username`, `password`, `email`, `salt`, `from` FROM table_2 } #index定義 index table_1 { source = table_1 #對(duì)應(yīng)的source名稱 path = E:/SQL_DATA/coreseek/var/data/table_1 #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑,例如:/usr/local/coreseek/var/... docinfo = extern mlock = 0 morphology = none min_word_len = 1 ondisk_dict = 1 html_strip = 0 #中文分詞配置,詳情請(qǐng)查看:http://www.coreseek.cn/products-install/coreseek_mmseg/ #charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環(huán)境下設(shè)置,/符號(hào)結(jié)尾 charset_dictpath = E:/SQL_DATA/coreseek/etc/ #Windows環(huán)境下設(shè)置,/符號(hào)結(jié)尾,最好給出絕對(duì)路徑,例如:C:/usr/local/coreseek/etc/... charset_type = zh_cn.utf-8 } index table_2 : table_1 { source = table_2 path = E:/SQL_DATA/coreseek/var/data/table_2 } #全局index定義 indexer { mem_limit = 1024M } #searchd服務(wù)定義 searchd { listen = 9000 read_timeout = 5 max_children = 30 max_matches = 1000 seamless_rotate = 0 preopen_indexes = 0 unlink_old = 1 pid_file = E:/SQL_DATA/coreseek/var/log/searchd_mysql.pid #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑,例如:/usr/local/coreseek/var/... log = E:/SQL_DATA/coreseek/var/log/searchd_mysql.log #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑,例如:/usr/local/coreseek/var/... query_log = E:/SQL_DATA/coreseek/var/log/query_mysql.log #請(qǐng)修改為實(shí)際使用的絕對(duì)路徑,例如:/usr/local/coreseek/var/... binlog_path = #關(guān)閉binlog日志 }
所以給返回的 matches 加個(gè) table_id 的屬性就好了,建好索引后查詢時(shí) matches 返回值類似這樣:
["matches"]=> array(16) { [0]=> array(3) { ["id"]=> string(2) "68" ["weight"]=> string(1) "2" ["attrs"]=> array(1) { ["table_id"]=> string(1) "2" } } [1]=> array(3) { ["id"]=> string(3) "350" ["weight"]=> string(1) "2" ["attrs"]=> array(1) { ["table_id"]=> string(1) "1" } }
需要注意的是如果之前有把 searchd 注冊(cè)成服務(wù)的話要記得換個(gè)端口……
最后改一下用于搜索的 PHP 文件(渣代碼勿怪…):
<?php // 引用sphinxapi類 require "sphinxapi.php"; //關(guān)閉錯(cuò)誤提示 error_reporting(E_ALL & ~E_NOTICE); $num = 0; if (!empty($_GET) && !empty($_GET['q'])) { $Keywords = strip_tags(trim($_GET['q'])); if (!empty($_GET['m']) && 1 == $_GET['m']) { $Keywords = substr(md5($Keywords), 8, 16); } if (!empty($_GET['m']) && 2 == $_GET['m']) { $Keywords = md5($Keywords); } $cl = new SphinxClient(); // 返回結(jié)果設(shè)置 $cl->SetServer('127.0.0.1', 9000); $cl->SetConnectTimeout(3); $cl->SetArrayResult(true); // 設(shè)置是否全文匹配 if (!empty($_GET) && !empty($_GET['f'])) { $cl->SetMatchMode(SPH_MATCH_ALL); } else { $cl->SetMatchMode(SPH_MATCH_ANY); } if (!empty($_GET) && !empty($_GET['p'])) { $p = !intval(trim($_GET['p'])) == 0 ? intval(trim($_GET['p'])) - 1 : 0; $p = $p * 20; // 我在sed.conf 設(shè)置了最大返回結(jié)果數(shù)1000。但是我在生成頁(yè)碼的時(shí)候最多生成20頁(yè),我想能滿足大部分搜索需求了。 // 以下語(yǔ)句表示從P參數(shù)偏移開(kāi)始每次返回20條。 $cl->setLimits($p, 20); } else { $cl->setLimits(0, 20); } $res = $cl->Query("$Keywords", "*"); //var_dump($res); @mysql_connect("localhost", "test", "test"); //數(shù)據(jù)庫(kù)賬號(hào)密碼 mysql_select_db("sed"); //數(shù)據(jù)庫(kù)庫(kù)名名 mysql_query("set names utf8"); $tables = ['table_1', 'table_2']; //把表名放入數(shù)組 function getResult($id, $table) { $sql = "select * from {$table} where id = " . $id; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['username'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['password'] . "</td>"; echo "<td>" . $row['salt'] . "</td>"; echo "<td>" . $row['from'] . "</td></tr>"; } } if ($res["total_found"]) { $num = $res["total_found"]; } else { $num = 0; } } ?> <!DOCTYPE html> <html> <head> <title>The Web of Answers</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-with,initial-scal=1"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/jquery.js"></script> <script src="js/bootstrap.min.js"></script> <script> function check(form){ if(form.q.value==""){ alert("Not null !"); form.q.focus(); return false; } } </script> <style> h2 { font-family: Times New Roman, Lucida Handwriting; } body { background-image: url(img/bg.jpg); } </style> </head> <body> <div class="container" id="container"> <div id="page-header"> <h2 class="text-center"> The Web of Answers </h2> </div> <div class="row"> <form action="" method="get" class="form-horizontal" role="form"> <div id="checkbox" class="col-md-6 col-md-offset-3"> <label class="checkbox-inline"> <input type="checkbox" id="full" name="f" value="1"> 完整匹配 </label> <label class="checkbox-inline"> <input type="checkbox" id="md5_16" name="m" value="1"> MD5匹配(16位) </label> <label class="checkbox-inline"> <input type="checkbox" id="md5_32" name="m" value="2"> MD5匹配(32位) </label> </div> <div class="input-group col-md-6 col-md-offset-3"> <input type="text" class="form-control" name="q" placeholder="請(qǐng)輸入" value="<?php echo strip_tags(trim($_GET['q']));?>"> <div class="input-group-btn"> <button type="submit" class="btn btn-primary" onclick="check(form)">Search</button> </div> </div> </form> </div> <br> <?php if (0 != $num) { echo "<div class=\"row\"> <div class=\"alert alert-success alert-dismissible col-md-10 col-md-offset-1\" role=\"alert\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\"><span aria-hidden=\"true\">×</span><span class=\"sr-only\">Close</span></button> 找到與<b> {$Keywords} </b>相關(guān)的結(jié)果 {$num} 個(gè)。用時(shí) {$res['time']} 秒。</div>"; echo "<div class=\"table-responsive col-md-10 col-md-offset-1\"> <table class=\"table table-striped table-hover\"> <tr> <th>Username</th> <th>Email</th> <th>Password</th> <th>Salt</th> <th>From</th> </tr>"; if (is_array($res["matches"])) { foreach ($res["matches"] as $docinfo) { $table_id = $docinfo['attrs']['table_id']; getResult($docinfo['id'], $tables[$table_id - 1]); } } echo "</table></div></div>"; } else { if (!empty($_GET) && !empty($_GET['q'])) { echo "<div class=\"alert alert-warning alert-dismissible col-md-10 col-md-offset-1\" role=\"alert\"> <button type=\"button\" class=\"close\" data-dismiss=\"alert\"><span aria-hidden=\"true\">×</span><span class=\"sr-only\">Close</span></button> 找不到與<b> {$Keywords} </b>相關(guān)的結(jié)果。請(qǐng)更換其他關(guān)鍵詞試試。</div></div>"; } } ?> <div id="pages"> <center> <nav> <ul class="pagination"> <?php if ($num !== 0) { $pagecount = (int) ($num / 20); if (!($num % 20) == 0) { $pagecount = $pagecount + 1; } if ($pagecount > 20) { $pagecount = 20; } $highlightid = !intval(trim($_GET['p'])) == 0 ? intval(trim($_GET['p'])) : 1; for ($i = 1; $i <= $pagecount; $i++) { if ($highlightid == $i) { echo "<li class=\"active\"><a href=\"#\">{$i}<span class=\"sr-only\">(current)</span></a></li>"; } else { echo "<li><a href=\"index.php?q={$Keywords}&p={$i}\">{$i}</a></li>"; } } } ?> </ul> </nav> </center> </div> <div id="footer"> <p class="text-center"> The Web of Answers ©2010-2015 | Powered by b0rg </p> </div> </div> </body> </html>
感謝各位的閱讀,以上就是“sphinx怎么實(shí)現(xiàn)多表查詢”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)sphinx怎么實(shí)現(xiàn)多表查詢這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。