Parsing a query with QPL is as simple as it gets, we start by importing the QPLOptions and the QPLParser
Code Block | ||||
---|---|---|---|---|
| ||||
from pyqpl.qpl import QPLOptions from pyqpl.parser import QPLParser |
QPLOptions is the one which will said the grammar to use and the rules to apply,
Code Block | ||||
---|---|---|---|---|
| ||||
options = QPLOptions(fields='content', implicit_operator='or', wildcard=False) |
implicit_operator
option, accepts either 'or' or 'and', this option select grammars with a slight variation between them, to set the operator (or / and) as the implicit operation.wildcard
options, accepts a boolean, in case of True, a new rule to identify wildcards will be addedOne the options are set, create a parser and pass the options as argument
Code Block | ||||
---|---|---|---|---|
| ||||
parser = QPLParser(options=options) |
With the parser created, parse the query with the line bellow
Code Block | ||||
---|---|---|---|---|
| ||||
qpl_query = parser.parse_query('This is PyQPL') |
The qpl_query will be an object of type Operator, which will contain all the operators of the query. At this point the qpl query is truly engine agnostic, and it needs a translator to be an engine query
Warning |
---|
We do not recommend the use of a custom grammar. This functionality exist for grammar, fixes, enhancements or additions to projects which can not wait for an official release. A wrong grammar could break the entire functionality. That said, you require to modify the existen grammar, or require a new one, please contact the development team. |
You can use your own grammar to parse a query, with either a well made lark file, or a raw string
Code Block | ||||
---|---|---|---|---|
| ||||
options = QPLOptions(fields='content', grammar='/grammar/mygrammar.lark') |
For more detail on how to build a grammar please check Grammar Composition from the Lark documentation