2017-03-16 62 views
0

TNTSearch如何添加查詢,顯示的結果?/ TNTSearch

搜索

首先,我做了索引。

然後我試着下面的代碼。

我得到了預期的結果。

use TeamTNT\TNTSearch\TNTSearch; 

$tnt = new TNTSearch; 

$tnt->loadConfig($config); 
$tnt->selectIndex("name.index"); 

$res = $tnt->search("This is a test search", 12); 

print_r($res); //returns an array of 12 document ids that best match your query 

顯示結果

我不明白如何添加查詢。

//to display the results you need an additional query 
//SELECT * FROM articles WHERE id IN $res ORDER BY FIELD(id, $res); 

草案1

$result = $tnt->query(SELECT * FROM articles WHERE id IN $res ORDER BY FIELD(id, $res)); 
print_r($result); 

草案2

·是否有必要重新創建索引?

$indexer = $tnt->createIndex('name.index'); 
$indexer->query('SELECT * FROM articles WHERE id IN $res ORDER BY FIELD(id, $res)'); 

回答

1

好吧,讓我試着解釋。當您執行這條線

$res = $tnt->search("This is a test search", 12); 

$res變量將是一個數組,它看起來是這樣的:

[ 
    'ids'   => [1,2,3,4,5], 
    'hits'   => 5, 
    'execution time' => "4ms" 
]; 

的「IDS」代表符合您查詢的文檔ID。 現在由您來查詢您的數據庫,以實際獲得這些文件與ids [1,2,3,4,5]。如果您使用的ORM一樣口若懸河,你會查詢數據庫一樣

$ids = implode(",", $res['ids']); 
$posts = Post::whereIn('id', $res['ids'])->orderByRaw("FIELD (ID, $ids)")->get(); 

現在$posts變量包含您的實際崗位

+0

Thanks.I能理解。 – re1