The simplest PyQPL implementation could look like this
from pyqpl.parser import QPLParser from pyqpl.qpl import QPLOptions from pyqpl.translator import ElasticsearchTranslator options = QPLOptions(fields='content') parser = QPLParser(options=options) translator = ElasticsearchTranslator() qpl_query = parser.parse_query('This is PyQPL') engine_query = translator.to_engine_query(qpl_query) print(qpl_query) print(engine_query)
Copy that to a file main.py
.
Execute the file, and the output of qpl_query
, will be de text representation of the operand, returned by the parse_query
function.
or(terms(This,is,PyQPL))
While the engine_query
, will be the engine specific query, return by the translator (an Elasticsearch translator in this case), after processing the qpl_query
, ready to be sent to the search engine, or be modified later on
{ 'multi_match': { 'query': 'This is PyQPL', 'fields': [ 'content' ] } }
The output above was indented for explanatory proposes
Although the parse_query step can be replace with the manual creation of the query, the process will be the same
from pyqpl.qpl import QPL, QPLOptions from pyqpl.translator import ElasticsearchTranslator options = QPLOptions(fields='content') qpl = QPL(options=options) translator = ElasticsearchTranslator() qpl_query = qpl.or_(operands=qpl.terms(terms=['This', 'is', 'PyQPL'])) engine_query = translator.to_engine_query(qpl_query) print(qpl_query) print(engine_query)
Being the process:
QPLOptions will store the configuration on how we want our query to behave, the grammar to user and custom operator or synonym injection necessary.