...
...
...
...
...
To utilize the QPL translators, follow these steps:
Import the EngineTypes enum and the TranslatorFactory module from PyQPL
Create an instance of the translator using the TranslatorFactory
Pass your QPL query to the translator's translation function
Obtain the engine-specific query generated by the translator
Code Block | ||||
---|---|---|---|---|
| ||||
from pyqpl.translator.translator_factory import TranslatorFactory, EngineTypes
# Create an instance of the translator
translator = TranslatorFactory.get_translator(EngineTypes.ELASTIC)
# Define a QPL query
qpl_query = QPL.term(operands=['python'], fields=['title'])
# Translate the QPL query to Elasticsearch query
elasticsearch_query = translator.translate(qpl_query)
# Use the generated Elasticsearch query for further processing or execution
print(elasticsearch_query) |
...
Code Block | |||
---|---|---|---|
|
...
| |
from pyqpl.translator.translator_factory import |
...
TranslatorFactory, EngineTypes
# Create an instance of the translator
translator = TranslatorFactory.get_translator(EngineTypes.OPENSEARCH)
# Define a QPL query
qpl_query = QPL.term(operands=['python'], fields=['title'])
# Translate the QPL query to OpenSearch query
opensearch_query = translator.translate(qpl_query)
# Use the generated Elasticsearch query for further processing or execution
print(opensearch_query) |
Code Block | |||
---|---|---|---|
|
...
translator = ElasticsearchTranslator()
| |
from pyqpl.translator.translator_factory import TranslatorFactory, EngineTypes
# Create an instance of the translator
translator = TranslatorFactory.get_translator(EngineTypes.AZURE_AI_SEARCH)
# Define a QPL query
qpl_query = QPL.term(operands=['python'], fields=['title'])
# Translate the QPL query to Elasticsearch query
azure_query = translator.translate(qpl_query)
# Use the generated Elasticsearch query for further processing or execution
print(azure_query) |
PyQPL translators provide the flexibility to add or overwrite the transformation of an operator by utilizing the custom_or_overwrites parameter of the translator. This parameter allows developers to specify a translation function for a particular operator type. By passing a dictionary with the operator type as the key and the corresponding translation function as the value, custom translation logic can be seamlessly integrated into the translator.
To customize the operator transformation in PyQPL translators, follow these steps:
...
Code Block | |||
---|---|---|---|
|
...
engine_query = translator.to_engine_query(qpl_query)
| |
from pyqpl.translators import ElasticsearchTranslator
from pyqpl.qpl import OperatorType, Operator
# Define the custom translation function for the "custom_operator" type
def translate_overwrite_operator(operand: Operator):
# Custom logic to transform the operator into an engine-specific query
field = operand.fields_or_defaults[0]
data = {
"term": {
field.name: {
"value": operand.operands[0]
}
}
}
boost = _query_boost_or_field_boost(operand.boost, field.boost)
# Check if it has a boost, to be added
if boost != 1:
data["term"][field.name]["boost"] = boost
return data
# Create an instance of the ElasticsearchTranslator
translator = ElasticsearchTranslator(custom_or_overwrites={
'term': translate_overwrite_operator
})
# Define a QPL query containing the custom_operator
qpl_query = QPL.term(operands=['python'], fields=['title'])
# Translate the QPL query to Elasticsearch query using the customized translation function
elasticsearch_query = translator.translate(qpl_query)
# Use the generated Elasticsearch query for further processing or execution
print(elasticsearch_query) |
Warning |
---|
If using this functionality for a custom operator, it will only work with manual queries, and not with the PyQPL Parser, in order to work with a parser you need a custom grammar, and if that is the case we recommend to contact the development team |
...