https://github.com/zk2/human-to-tsquery
Convert human request to postgres ts_query
https://github.com/zk2/human-to-tsquery
php postgresql tsquery
Last synced: 3 months ago
JSON representation
Convert human request to postgres ts_query
- Host: GitHub
- URL: https://github.com/zk2/human-to-tsquery
- Owner: zk2
- License: mit
- Created: 2019-12-26T15:03:47.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-03T19:19:15.000Z (over 1 year ago)
- Last Synced: 2025-12-14T14:43:46.020Z (6 months ago)
- Topics: php, postgresql, tsquery
- Language: PHP
- Size: 68.4 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
zk2\human-to-tsquery
================
[](https://travis-ci.org/zk2/human-to-tsquery)
[](https://packagist.org/packages/zk2/human-to-tsquery)
[](https://packagist.org/packages/zk2/human-to-tsquery)
[](https://packagist.org/packages/zk2/human-to-tsquery)
[](https://packagist.org/packages/zk2/human-to-tsquery)
[](https://packagist.org/packages/zk2/human-to-tsquery)
Human query to ts_query for PostgreSQL full text search (https://www.postgresql.org/docs/12/textsearch.html). Provides a clement parser and associated tools for a convert human query to query which can be used ih Postgres Full Text Search. Lenient in that is will produce a parse tree for any input, given a default operator and by generally ignoring any unparsable syntax.
The query language supports the following features at a high level:
- Boolean operators: AND (infix), OR (infix), "-" (prefix) with an implied default operator and precedence rules, e.g. "boy OR girl -infant"
- Proximity Near Operator (Nx) - `television N2 violence` - Finds words within x number of words from each other, regardless of the order in which they occur.
- Proximity Within Operator (Wx) - `Franklin W2 Roosevelt` - Finds words within x number of words from each other, in the order they are entered in the search.
- Optional parenthesis for explicitly denoting precedence.
- Quoted phrases (for proximity matching)
Documentation
-------------
// 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 )"
Running the Tests
-----------------
Install the [Composer](http://getcomposer.org/) `dev` dependencies:
composer install --dev
Then, run the test suite using
[PHPUnit](https://github.com/sebastianbergmann/phpunit/):
vendor/bin/phpunit
License
-------
This bundle is released under the MIT license. See the complete license in the bundle:
LICENSE