https://github.com/pimcore/search-query-parser
Search Query Parser community bundle parses a simplified SQL-like query string into a syntax tree which can be used to build search queries.
https://github.com/pimcore/search-query-parser
Last synced: about 1 year ago
JSON representation
Search Query Parser community bundle parses a simplified SQL-like query string into a syntax tree which can be used to build search queries.
- Host: GitHub
- URL: https://github.com/pimcore/search-query-parser
- Owner: pimcore
- License: other
- Created: 2017-07-11T08:43:07.000Z (almost 9 years ago)
- Default Branch: 2.x
- Last Pushed: 2025-04-30T08:30:17.000Z (about 1 year ago)
- Last Synced: 2025-04-30T09:44:00.653Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 98.6 KB
- Stars: 11
- Watchers: 12
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# SearchQueryParser
[](https://travis-ci.org/pimcore/search-query-parser)
A basic search query parsing library which can be used to transform free-text searches
into SQL (or other) queries. The analyzed search query can be used to build complex
(SQL) queries, searching for a term in multiple fields. Take a look at the
[`Zend_Db` end to end test](test/ZendDbEndToEndTest.php#L71) an example or have a
look into [examples/](examples/) for working examples.
## Syntax
Search terms can be AND/OR combined, negated, grouped with parentheses and modified to fuzzy exact search with the following syntax:
* `foo`
* `foo* AND bar`
* `*foo* OR *bar*`
* `*foo* OR !*bar*`
* `*foo* AND bar`
* `foo OR (bar AND baz)`
### Modifiers
Fuzzy search: `**`
Negation: `! !() !*`
## Usage
```php
lex($input);
dump($tokens);
// parses tokens into an abstract query
$parser = new \SearchQueryParser\Parser();
$query = $parser->parse($tokens);
dump($query);
// use the Zend_Db query builder to transform the Query into conditions
$db = new Zend_Db_Adapter_Pdo_Sqlite(array(
'dbname' => ':memory:'
));
// dummy query
$select = $db
->select()
->from('foo');
// build conditions for every passed field
$queryBuilder = new \SearchQueryParser\QueryBuilder\ZendDbSelect([
'foo',
'bar'
]);
$queryBuilder->processQuery($select, $query);
dump($select->__toString());
```
### Using the facade
There's a facade class which can be called if you want to use the standard lexer/parser.
```php