PHPackages                             zk2/human-to-tsquery - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. zk2/human-to-tsquery

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

zk2/human-to-tsquery
====================

Convert human request to postgres ts\_query

v3.0.2(1y ago)24.0k↓25%1[1 PRs](https://github.com/zk2/human-to-tsquery/pulls)MITPHPPHP ^7.4|^8CI failing

Since Dec 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/zk2/human-to-tsquery)[ Packagist](https://packagist.org/packages/zk2/human-to-tsquery)[ Docs](https://github.com/zk2/human-to-tsquery)[ RSS](/packages/zk2-human-to-tsquery/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (17)Used By (0)

zk2\\human-to-tsquery
=====================

[](#zk2human-to-tsquery)

[![Build Status](https://camo.githubusercontent.com/a20a13f6117c57230dd61b810da58d26bcb3b6aa52a561b8df93e030b67fa71b/68747470733a2f2f7472617669732d63692e636f6d2f7a6b322f68756d616e2d746f2d747371756572792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/zk2/human-to-tsquery)

[![Latest Stable Version](https://camo.githubusercontent.com/0438cc3d0e2d89135206d59d7a4bea9c66ec6ebc1c026d80f8c80b02c157ba4b/68747470733a2f2f706f7365722e707567782e6f72672f7a6b322f68756d616e2d746f2d747371756572792f762f737461626c65)](https://packagist.org/packages/zk2/human-to-tsquery)[![Total Downloads](https://camo.githubusercontent.com/48b4dba24db369351f46d726a7424eb82e843ca7bbe1450a1e26082398724483/68747470733a2f2f706f7365722e707567782e6f72672f7a6b322f68756d616e2d746f2d747371756572792f646f776e6c6f616473)](https://packagist.org/packages/zk2/human-to-tsquery)[![Latest Unstable Version](https://camo.githubusercontent.com/d885b27f683d70bf3682975c1da6397b2d6f86e321f3b1b2d955f4f1d5a2af65/68747470733a2f2f706f7365722e707567782e6f72672f7a6b322f68756d616e2d746f2d747371756572792f762f756e737461626c65)](https://packagist.org/packages/zk2/human-to-tsquery)[![License](https://camo.githubusercontent.com/a21f47b8096dcdac39dad4ee193292540cda7e3f2a291d383ad1a26d9176d584/68747470733a2f2f706f7365722e707567782e6f72672f7a6b322f68756d616e2d746f2d747371756572792f6c6963656e7365)](https://packagist.org/packages/zk2/human-to-tsquery)[![composer.lock](https://camo.githubusercontent.com/7f7d2b16ea215e43d777c8e830371542f110353ebc9a8bb184956335748f49d0/68747470733a2f2f706f7365722e707567782e6f72672f7a6b322f68756d616e2d746f2d747371756572792f636f6d706f7365726c6f636b)](https://packagist.org/packages/zk2/human-to-tsquery)

Human query to ts\_query for PostgreSQL full text search (). 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
-------------

[](#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  ((debt  financ) | lalala ) | ((debt  financ) | lalala )  indigen | indigen  ((debt  financ) | lalala ) | ((debt  financ) | lalala )  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 )  ((debt  financ) | lalala ) | (indigen | texa )  ((debt  financ) | lalala )) & ((new  york) | boston )"

```

Running the Tests
-----------------

[](#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
-------

[](#license)

This bundle is released under the MIT license. See the complete license in the bundle:

```
LICENSE

```

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance44

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~140 days

Recently: every ~128 days

Total

14

Last Release

495d ago

Major Versions

v1.4.1 → v2.0.02023-08-03

v2.0.1 → v3.0.02024-02-07

PHP version history (3 changes)v1.0PHP &gt;=7.2,&lt;8.0

v1.2.0PHP &gt;=7.2

v1.4.0PHP ^7.4|^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/5300484274f8497c82b30539e2ed3393739956426fcc3b448781bec446a6babe?d=identicon)[zk2](/maintainers/zk2)

---

Top Contributors

[![zk2](https://avatars.githubusercontent.com/u/4775391?v=4)](https://github.com/zk2 "zk2 (18 commits)")[![Arkos13](https://avatars.githubusercontent.com/u/17023895?v=4)](https://github.com/Arkos13 "Arkos13 (2 commits)")

---

Tags

phppostgresqltsquery

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zk2-human-to-tsquery/health.svg)

```
[![Health](https://phpackages.com/badges/zk2-human-to-tsquery/health.svg)](https://phpackages.com/packages/zk2-human-to-tsquery)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M283](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M63](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M344](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
