Skip to content

benjamin-carl/tntsearch

 
 

Repository files navigation

Latest Version on Packagist Total Downloads Software License Build Status Slack Status

#TNTSearch

A fully featured full text search engine written in PHP

TNTSearch Banner

##Demo

To see TNTSearch in action take a look at the demo page

##Tutorials

##Installation

The easiest way to install TNTSearch is via composer. Create the following composer.json file and run the php composer.phar install command to install it.

{
    "require": {
        "teamtnt/tntsearch": "0.8.*"
    }
}

##Requirements

Before you proceed make sure your server meets the following requirements:

  • PHP >= 5.5
  • PDO PHP Extension
  • SQLite PHP Extension
  • mbstring PHP Extension

Laravel 5 (optional)

Add the service provider in app/config/app.php:

TeamTNT\TNTSearch\TNTSearchServiceProvider::class,

And add the TNTSearch alias to app/config/app.php:

'TNTSearch' => TeamTNT\TNTSearch\Facades\TNTSearch::class,

The configuration will automatically include your database conection and storage so no further setup is necesarry. However, if you want to overide the default settings, create an tntsearch config entry in app/config/services.php like:

'tntsearch' => [
    'driver'   => 'mysql',
    'host'     => env('DB_HOST', 'localhost'),
    'database' => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'password' => env('DB_PASSWORD'),
    'storage'  => './storage/',
]

Searching and indexing can now be done without loading the configuration like:

//don't forget to load the Facade
use TNTSearch;

//indexing
$indexer = TNTSearch::createIndex('articles.index');
$indexer->query('SELECT id, article FROM articles;');
$indexer->run();

//searching
TNTSearch::selectIndex("articles.index");
$res = TNTSearch::searchBoolean('search string', 10);

##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/'
]);

$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: Your select statment MUST contain an ID field.

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
//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 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 fuzzines 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);

Drivers

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Credits

License

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

About

A fully featured full text search engine written in PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%