商务合作加微信:2230304070
学习与交流:PHP技术交流微信群
2025年 JetBrains全家桶通用激活码&账号 支持最新版本
https://web.52shizhan.cn
TNTSearch是一款完全用PHP编写的全文搜索(FTS)引擎,只需简单配置就能在几分钟内为您的应用添加出色的搜索体验。
composer require teamtnt/tntsearchuseTeamTNT\TNTSearch\TNTSearch;$tnt = new TNTSearch;$tnt->loadConfig(['driver' => 'mysql','host' => 'localhost','database' => 'dbname','username' => 'user','password' => 'pass','storage' => '/var/www/tntsearch/examples/','stemmer' => \TeamTNT\TNTSearch\Stemmer\PorterStemmer::class //可选]);$indexer = $tnt->createIndex('name.index');$indexer->query('SELECT id, article FROM articles;');注意事项:
storageid,可通过$indexer->setPrimaryKey()修改$indexer->includePrimaryKey()启用$tnt->selectIndex("name.index");$res = $tnt->search("This is a test search", 12);// 包含romeo但不包含juliet$res = $tnt->searchBoolean("romeo -juliet");// 包含romeo或hamlet$res = $tnt->searchBoolean("romeo or hamlet");// 复杂条件$res = $tnt->searchBoolean("(romeo juliet) or (prince hamlet)");$tnt->fuzziness(true);$tnt->fuzzy_distance = 2; // 莱文斯坦距离$res = $tnt->search("juleit"); // 匹配juliet$index = $tnt->getIndex();$index->insert(['id' => '11', 'title' => 'new title', 'article' => 'new article']);$index->update(11, ['id' => '11', 'title' => 'updated title', 'article' => 'updated article']);$index->delete(12);classSomeTokenizerextendsAbstractTokenizerimplementsTokenizerInterface{staticprotected $pattern = '/[\s,\.]+/';publicfunctiontokenize($text){return preg_split($this->getPattern(), strtolower($text), -1, PREG_SPLIT_NO_EMPTY); }}// 使用分词器$indexer = new TNTIndexer;$indexer->setTokenizer(new SomeTokenizer);创建索引:
$candyShopIndexer = new TNTGeoIndexer;$candyShopIndexer->loadConfig($config);$candyShopIndexer->createIndex('candyShops.index');$candyShopIndexer->query('SELECT id, longitude, latitude FROM candy_shops;');$candyShopIndexer->run();搜索附近位置:
$currentLocation = ['longitude' => 11.576124,'latitude' => 48.137154];$candyShopIndex = new TNTGeoSearch();$candyShopIndex->loadConfig($config);$candyShopIndex->selectIndex('candyShops.index');$candyShops = $candyShopIndex->findNearest($currentLocation, 2, 10); // 2公里范围内10个结果$classifier = new TNTClassifier();$classifier->learn("A great game", "Sports");$classifier->learn("The election was over", "Not sports");// 更多训练数据...$guess = $classifier->predict("It was a close election");var_dump($guess['label']); // 输出 "Not sports"// 保存和加载分类器$classifier->save('sports.cls');$classifier->load('sports.cls');TNTSearch为PHP开发者提供了强大而灵活的全文搜索解决方案,从基本搜索到高级功能如地理搜索和文本分类一应俱全。其纯PHP实现和简单的集成方式使其成为PHP项目中实现搜索功能的优秀选择。

