PyQPL provides translators for search engines, allowing the transformation of a QPL query into an engine-specific query. This feature enables developers to use a single QPL query and translate it to multiple search engines of different types without modifying the underlying code. The current available translators are:
To utilize the QPL translators, follow these steps:
Import the desired translator module into your Python project.
Create an instante of the translatorÂ
Pass your QPL query to the translator's translation function
Obtain the engine-specific query generated by the translator
from pyqpl.translators import ElasticsearchTranslator # Create an instance of the ElasticsearchTranslator translator = ElasticsearchTranslator() # 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)
from pyqpl.translators import OpensearchTranslator # Create an instance of the OpensearchTranslator translator = OpensearchTranslator() # Define a QPL query qpl_query = QPL.term(operands=['python'], fields=['title']) # Translate the QPL query to Elasticsearch query opensearch_query = translator.translate(qpl_query) # Use the generated Elasticsearch query for further processing or execution print(opensearch_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:
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)
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