2017 © Pedro Peláez
 

library tntsearch

A fully featured full text search engine written in PHP

image

teamtnt/tntsearch

A fully featured full text search engine written in PHP

  • Monday, July 30, 2018
  • by nticaric
  • Repository
  • 76 Watchers
  • 1459 Stars
  • 167,692 Installations
  • PHP
  • 4 Dependents
  • 2 Suggesters
  • 117 Forks
  • 17 Open issues
  • 43 Versions
  • 17 % Grown

The README.md

Latest Version on Packagist Total Downloads ![Software License][ico-license] Build Status Slack Status, (*1)

TNTSearch, (*2)

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:, (*3)

  • 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!, (*4)

Unlike many other engines, the index can be easily updated without doing a reindex or using deltas., (*5)

View online demo  |  Follow us on Twitter, or Facebook  |  Visit our sponsors:, (*6)

, (*7)


Demo

Tutorials

Premium products

If you're using TNT Search and finding it useful, take a look at our premium analytics tool:, (*8)

, (*9)

Support us on Open Collective

Installation

The easiest way to install TNTSearch is via composer:, (*10)

composer require teamtnt/tntsearch

Requirements

Before you proceed, make sure your server meets the following requirements:, (*11)

  • 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., (*12)

Usage:, (*13)

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:, (*14)

  • [PDOException] SQLSTATE[HY000] [14] unable to open database file *

Note: If your primary key is different than id set it like:, (*15)

$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:, (*16)

$indexer->includePrimaryKey();

Searching

Searching for a phrase or keyword is trivial:, (*17)

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., (*18)

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)");

The fuzziness can be tweaked by setting the following member variables:, (*19)

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., (*20)

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:, (*21)

``` php, (*22)

use TeamTNT\TNTSearch\Support\AbstractTokenizer; use TeamTNT\TNTSearch\Support\TokenizerInterface;, (*23)

class SomeTokenizer extends AbstractTokenizer implements TokenizerInterface { static protected $pattern = '/[\s,.]+/';, (*24)

public function tokenize($text) {
    return preg_split($this->getPattern(), strtolower($text), -1, PREG_SPLIT_NO_EMPTY);
}

}, (*25)


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. ``` php $someTokenizer = new SomeTokenizer; $indexer = new TNTIndexer; $indexer->setTokenizer($someTokenizer);

Another way would be to pass the tokenizer via config:, (*26)

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();

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., (*27)

Our address is: TNT Studio, Sv. Mateja 19, 10010 Zagreb, Croatia., (*28)

We'll publish all received games here, (*29)

Support OpenCollective OpenCollective

Buy Me a Coffee at ko-fi.com, (*30)

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer], (*31)

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor], (*32)

Credits

License

The MIT License (MIT). Please see License File for more information., (*33)


From Croatia with ♥ by TNT Studio (@tntstudiohr, blog), (*34)

The Versions

30/07 2018

dev-master

9999999-dev https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

18/07 2018

v1.3.4

1.3.4.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

29/06 2018

v1.3.3

1.3.3.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

03/06 2018

v1.3.2

1.3.2.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

31/01 2018

v1.3.1

1.3.1.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

18/12 2017

v1.3.0

1.3.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

08/12 2017

v1.2.0

1.2.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

18/09 2017

v1.1.0

1.1.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

09/09 2017

v1.0.7

1.0.7.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

07/07 2017

v1.0.6

1.0.6.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

16/06 2017

dev-add-code-of-conduct-1

dev-add-code-of-conduct-1 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

12/06 2017

v1.0.5

1.0.5.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

12/06 2017

v1.0.4

1.0.4.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

11/05 2017

v1.0.3

1.0.3.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

10/04 2017

v1.0.2

1.0.2.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

09/03 2017

v1.0.1

1.0.1.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext geosearch tntsearch teamtnt fuzzy search

07/02 2017

v1.0.0

1.0.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext tntsearch teamtnt

09/11 2016

v0.9.0

0.9.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

search fulltext tntsearch teamtnt

24/10 2016

v0.8.9

0.8.9.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

01/10 2016

v0.8.8

0.8.8.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

30/09 2016

v0.8.7

0.8.7.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

21/09 2016

v0.8.6

0.8.6.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

25/08 2016

v0.8.5

0.8.5.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

23/08 2016

v0.8.4

0.8.4.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

19/08 2016

v0.8.3

0.8.3.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

19/08 2016

v0.8.2

0.8.2.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

19/08 2016

v0.8.1

0.8.1.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

20/07 2016

v0.8.0

0.8.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

20/07 2016

v0.7.1

0.7.1.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

13/07 2016

v0.7.0

0.7.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

03/07 2016

v0.6.6

0.6.6.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

25/06 2016

v0.6.5

0.6.5.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

21/06 2016

v0.6.4

0.6.4.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

03/06 2016

v0.6.3

0.6.3.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • ext-pdo_sqlite *
  • ext-sqlite3 *
  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

28/05 2016

v0.6.2

0.6.2.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

24/05 2016

v0.6.1

0.6.1.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

16/05 2016

v0.6.0

0.6.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

11/05 2016

v0.5.0

0.5.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

09/05 2016

v0.4.1

0.4.1.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

09/05 2016

v0.4.0

0.4.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

27/04 2016

v0.3.0

0.3.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

20/04 2016

v0.2.0

0.2.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt

06/04 2016

v0.1.0

0.1.0.0 https://github.com/teamtnt/tntsearch

A fully featured full text search engine written in PHP

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

tntsearch teamtnt