zk2/human-to-tsquery

将人类请求转换为PostgreSQL ts_query

v3.0.0 2024-02-07 09:10 UTC

This package is auto-updated.

Last update: 2024-09-07 13:17:20 UTC


README

Build Status

Latest Stable Version Total Downloads Latest Unstable Version License composer.lock

为PostgreSQL全文搜索(https://postgresql.ac.cn/docs/12/textsearch.html)将人类查询转换为ts_query。提供了一种宽容的解析器和相关的工具,用于将人类查询转换为可以用于Postgres全文搜索的查询。宽容在于,它会为任何输入产生一个解析树,给定一个默认操作符,并且通常忽略任何不可解析的语法。

查询语言在高级上支持以下特性

  • 布尔运算符:AND(中缀),OR(中缀),"-"(前缀)具有隐含的默认操作符和优先级规则,例如:"boy OR girl -infant"

  • 邻近度近邻操作符(Nx) - television N2 violence - 在任意顺序中查找彼此之间距离x个单词的单词。

  • 邻近度在操作符(Wx) - Franklin W2 Roosevelt - 在搜索中输入的顺序中查找彼此之间距离x个单词的单词。

  • 可选括号以明确表示优先级。

  • 引号短语(用于邻近匹配)

文档

// Some function, which getting SQL query and return single string result
$closure = function (string $sql) use ($connection) {
    return $connection->fetchOne($sql);
};
$humanToTsQuery = new HumanToTsQuery('Opel AND (auto car (patrol OR diesel OR "electric car") AND -sale)');
$tsQuery = $humanToTsQuery->getQuery($closure);
var_dump($tsQuery); // "opel & (auto & car & (patrol | diesel | (electr <-> car)) & !sale)"

$humanToTsQuery = new HumanToTsQuery('indigenous N2 ("debt financing" OR lalala) AND ("New York" OR Boston)');
$tsQuery = $humanToTsQuery->getQuery($closure);
var_dump($tsQuery); // "(indigen <2> ((debt <-> financ) | lalala ) | ((debt <-> financ) | lalala ) <2> indigen | indigen <1> ((debt <-> financ) | lalala ) | ((debt <-> financ) | lalala ) <1> indigen) & ((new <-> york) | boston )"

$humanToTsQuery = new HumanToTsQuery('(indigenous OR texas) W2 ("debt financing" OR lalala) AND ("New York" OR Boston)');
$tsQuery = $humanToTsQuery->getQuery($closure);
var_dump($tsQuery); // "((indigen | texa ) <2> ((debt <-> financ) | lalala ) | (indigen | texa ) <1> ((debt <-> financ) | lalala )) & ((new <-> york) | boston )"

运行测试

安装Composer dev依赖

composer install --dev

然后,使用PHPUnit运行测试套件

vendor/bin/phpunit

许可证

此软件包在MIT许可证下发布。请参阅软件包中的完整许可证

LICENSE