Stars: 192
Forks: 9
Pull Requests: 15
Issues: 8
Watchers: 12
Last Updated: 2022-01-13 09:52:46
Query Translator is a search query translator with AST representation
License: MIT License
Languages: PHP
Query Translator takes a search string as user input and converts it into something a search backend can understand. Technically, it's a search query translator with abstract syntax tree representation. From the produced syntax tree, translation target can be anything you need. Usually it's a search backend, like Solr and Elasticsearch, or a database abstraction layer.
A set of interfaces for implementing a language processor is provided, with a single implemented language named Galach. Galach implements a syntax that is based on what seems to be the unofficial standard for search query as user input. Quick cheat sheet:
word
"phrase"
(group)
+mandatory
-prohibited
AND
&&
OR
||
NOT
!
#tag
@user
domain:term
User input means you have to expect errors and handle them gracefully. Because of that, the parser is completely resistant to errors. Syntax tree will contain detailed information about corrections applied to make sense of the user input. This can be useful to clean up the input or implement rich input interface, with features like suggestions, syntax highlighting and error feedback.
The implementation was made with customization in mind. You can change the special characters which will be used as part of the syntax, pick out elements of the language you want to use, implement your own term clauses, or change how the syntax tree is converted to the target output.
Note: This implementation is intended as a library, meaning it doesn't try to solve specific use cases for query translation. Instead, it's meant to be a base that you can use in implementing such a use case.
First add the library to your project:
composer require netgen/query-translator:^1.0
After that, make use of the features provided out of the box. If those are not enough, use extension points to customize various parts of the translator to fit your needs. See Galach documentation to find out more.
Demo is available as a separate repository at netgen/query-translator-demo.
Steps for running the demo:
composer create-project netgen/query-translator-demo
cd query-translator-demo
src
as the document root php -S localhost:8005 -t src