

Github GitHub - teamtnt/tntsearch: A fully featured full text search engine writ...
source link: https://github.com/teamtnt/tntsearch
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

TNTSearch
TNTSearch is a full-text search (FTS) engine written entirely in PHP. A simple configuration allows you to add an amazing search experience in just minutes. Features include:
- Fuzzy search
- Search as you type
- Geo-search
- Text classification
- Stemming
- Custom tokenizers
- Bm25 ranking algorithm
- Boolean search
- Result highlighting
- Dynamic index updates (no need to reindex each time)
- Easily deployable via Packagist.org
We also created some demo pages that show tolerant retrieval with n-grams in action. The package has a bunch of helper functions like Jaro-Winkler and Cosine similarity for distance calculations. It supports stemming for English, Croatian, Arabic, Italian, Russian, Portuguese and Ukrainian. If the built-in stemmers aren't enough, the engine lets you easily plugin any compatible snowball stemmer. Some forks of the package even support Chinese. And please contribute other languages!
Unlike many other engines, the index can be easily updated without doing a reindex or using deltas.
View online demo | Follow us on Twitter, or Facebook | Visit our sponsors:
Tutorials
Premium products
If you're using TNT Search and finding it useful, take a look at our premium analytics tool:
Support us on Open Collective
Installation
The easiest way to install TNTSearch is via composer:
composer require teamtnt/tntsearch
Requirements
Before you proceed, make sure your server meets the following requirements:
- PHP >= 7.1
- PDO PHP Extension
- SQLite PHP Extension
- mbstring PHP Extension
Examples
Creating an index
In order to be able to make full text search queries, you have to create an index.
Usage:
use TeamTNT\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//optional ]); $indexer = $tnt->createIndex('name.index'); $indexer->query('SELECT id, article FROM articles;'); //$indexer->setLanguage('german'); $indexer->run();
Important: "storage" settings marks the folder where all of your indexes will be saved so make sure to have permission to write to this folder otherwise you might expect the following exception thrown:
- [PDOException] SQLSTATE[HY000] [14] unable to open database file *
Note: If your primary key is different than id
set it like:
$indexer->setPrimaryKey('article_id');
Making the primary key searchable
By default, the primary key isn't searchable. If you want to make it searchable, simply run:
$indexer->includePrimaryKey();
Searching
Searching for a phrase or keyword is trivial:
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 against your application database // SELECT * FROM articles WHERE id IN $res ORDER BY FIELD(id, $res);
The ORDER BY FIELD clause is important, otherwise the database engine will not return the results in the required order.
Boolean Search
use TeamTNT\TNTSearch\TNTSearch; $tnt = new TNTSearch; $tnt->loadConfig($config); $tnt->selectIndex("name.index"); //this will return all documents that have romeo in it but not juliet $res = $tnt->searchBoolean("romeo -juliet"); //returns all documents that have romeo or hamlet in it $res = $tnt->searchBoolean("romeo or hamlet"); //returns all documents that have either romeo AND juliet or prince AND hamlet $res = $tnt->searchBoolean("(romeo juliet) or (prince hamlet)");
Fuzzy Search
The fuzziness can be tweaked by setting the following member variables:
public $fuzzy_prefix_length = 2; public $fuzzy_max_expansions = 50; public $fuzzy_distance = 2; //represents the Levenshtein distance;
use TeamTNT\TNTSearch\TNTSearch; $tnt = new TNTSearch; $tnt->loadConfig($config); $tnt->selectIndex("name.index"); $tnt->fuzziness = true; //when the fuzziness flag is set to true, the keyword juleit will return //documents that match the word juliet, the default Levenshtein distance is 2 $res = $tnt->search("juleit");
Updating the index
Once you created an index, you don't need to reindex it each time you make some changes to your document collection. TNTSearch supports dynamic index updates.
use TeamTNT\TNTSearch\TNTSearch; $tnt = new TNTSearch; $tnt->loadConfig($config); $tnt->selectIndex("name.index"); $index = $tnt->getIndex(); //to insert a new document to the index $index->insert(['id' => '11', 'title' => 'new title', 'article' => 'new article']); //to update an existing document $index->update(11, ['id' => '11', 'title' => 'updated title', 'article' => 'updated article']); //to delete the document from index $index->delete(12);
Custom Tokenizer
First, create your own Tokenizer class. It should extend AbstractTokenizer class, define word split $pattern value and must implement TokenizerInterface:
use TeamTNT\TNTSearch\Support\AbstractTokenizer; use TeamTNT\TNTSearch\Support\TokenizerInterface; class SomeTokenizer extends AbstractTokenizer implements TokenizerInterface { static protected $pattern = '/[\s,\.]+/'; public function tokenize($text) { return preg_split($this->getPattern(), strtolower($text), -1, PREG_SPLIT_NO_EMPTY); } }
This tokenizer will split words using spaces, commas and periods.
After you have the tokenizer ready, you should pass it to TNTIndexer
via setTokenizer
method.
$someTokenizer = new SomeTokenizer; $indexer = new TNTIndexer; $indexer->setTokenizer($someTokenizer);
Another way would be to pass the tokenizer via config:
use TeamTNT\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//optional, 'tokenizer' => \TeamTNT\TNTSearch\Support\SomeTokenizer::class ]); $indexer = $tnt->createIndex('name.index'); $indexer->query('SELECT id, article FROM articles;'); $indexer->run();
Geo Search
Indexing
$candyShopIndexer = new TNTGeoIndexer; $candyShopIndexer->loadConfig($config); $candyShopIndexer->createIndex('candyShops.index'); $candyShopIndexer->query('SELECT id, longitude, latitude FROM candy_shops;'); $candyShopIndexer->run();
Searching
$currentLocation = [ 'longitude' => 11.576124, 'latitude' => 48.137154 ]; $distance = 2; //km $candyShopIndex = new TNTGeoSearch(); $candyShopIndex->loadConfig($config); $candyShopIndex->selectIndex('candyShops.index'); $candyShops = $candyShopIndex->findNearest($currentLocation, $distance, 10);
Classification
use TeamTNT\TNTSearch\Classifier\TNTClassifier; $classifier = new TNTClassifier(); $classifier->learn("A great game", "Sports"); $classifier->learn("The election was over", "Not sports"); $classifier->learn("Very clean match", "Sports"); $classifier->learn("A clean but forgettable game", "Sports"); $guess = $classifier->predict("It was a close election"); var_dump($guess['label']); //returns "Not sports"
Saving the classifier
$classifier->save('sports.cls');
Loading the classifier
$classifier = new TNTClassifier(); $classifier->load('sports.cls');
Drivers
PS4Ware
You're free to use this package, but if it makes it to your production environment, we would highly appreciate you sending us a PS4 game of your choice. This way you support us to further develop and add new features.
Our address is: TNT Studio, Sv. Mateja 19, 10010 Zagreb, Croatia.
We'll publish all received games here
Support
Backers
Support us with a monthly donation and help us continue our activities. [Become a backer]
Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]
Credits
License
The MIT License (MIT). Please see License File for more information.
From Croatia with by TNT Studio (@tntstudiohr, blog)
Recommend
-
7
card.io component for React Native A fully featured implementation of card.io for iOS and Android. Installation iOS Run npm install react-native-card-io --save in your project directory. Ope...
-
25
Updates to this readme July 06, 2021 Switch licenses to APACHE-2.0 OR MIT May 14, 2021 Removed instructions for the 3rdParty zip file and downloader URL. This is no longer a requirement. ...
-
21
6 August 2021威胁快讯:TeamTNT新变种通过ELF打包bash脚本,正通过Hadoop ResourceManager RCE 传播 TeamTNT是一个比较活跃的挖矿家族,曾被腾讯和PAN等国内外安全厂商多...
-
6
The Ultimate BRRRT Simulator: Fully Featured A-10 Warthog Cockpit The Fairchild Republic A-10 “Warthog” with its 30 mm rotary cannon has captured the imagination of friendly soldiers and milit...
-
4
[APP][2.2+] MiXplorer v6.x Released (fully-featured file manager) IronTechmon...
-
6
配置不当的 Docker 服务器已成为TeamTNT的热门攻击目标-InfoQ
-
11
[APP][5.0+] FairEmail - Fully featured, open source, privacy oriented email app M
-
17
RP2040 Doom This is a port of Doom for RP2040 devices, derived from Chocolate Doom. Significant changes have been made to support running on the RP2040 device, but...
-
9
GraphQL YogaThe fully-featured GraphQL Server
-
2
Ranked #13 for todayZumvieFully featured product demos for SaaSFree OptionsWith Zumvie, you can turn y...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK