woodfish / tntsearch-chinese

一个功能齐全的PHP全文搜索引擎

v1.0.0 2016-12-15 16:52 UTC

README

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

#TNTSearch

一个功能齐全的PHP全文搜索引擎

TNTSearch Banner

##演示

要查看TNTSearch的实际应用,请查看演示页面

##教程

##安装

安装TNTSearch最简单的方法是通过composer

composer require teamtnt/tntsearch

##要求

在继续之前,请确保您的服务器满足以下要求

  • PHP >= 5.5
  • PDO PHP扩展
  • SQLite PHP扩展
  • mbstring PHP扩展

##示例

创建索引

为了能够进行全文搜索查询,您必须创建一个索引。

用法

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

重要:"storage"设置标记了所有索引将保存的文件夹,确保您有权限写入此文件夹,否则您可能会遇到以下异常

  • [PDOException] SQLSTATE[HY000] [14] 无法打开数据库文件 *

注意:如果您的主键不同于id,请设置如下

$indexer->setPrimaryKey('article_id');

搜索

搜索短语或关键字是简单的

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

ORDER BY FIELD子句非常重要,否则数据库引擎不会按所需顺序返回结果

布尔搜索

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

模糊搜索

可以通过设置以下成员变量调整模糊度

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

更新索引

一旦创建了索引,您无需在每次更改文档集合时重新索引。TNTSearch支持动态索引更新。

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

驱动程序

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

鸣谢

许可协议

MIT许可(MIT)。有关更多信息,请参阅许可文件