Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Query Translation if where the qpl query, agnostic to this point, gets converted 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

Translators Available

and translate it to multiple search engines of different types without modifying the underlying code. The current available translators are:

  • ElasticsearchTranslator
    • Availability: PyQPL
    ElasticsearchTranslator (from
    • version 1.0.0
    )OpensearchTranslator (from
    • onwards.
    • Description: The ElasticsearchTranslator is a translator specifically designed to convert QPL queries into Elasticsearch queries.
  • OpensearchTranslator
    • Availability: PyQPL version 1.0.
    4)

Translation

    • 5 onwards.
    • Description: The OpensearchTranslator is a translator developed to transform QPL queries into Opensearch queries

Usage

To utilize the QPL translators, follow these steps:

  1. Import the desired translator module into your Python project.

  2. Create an instante of the translator 

  3. Pass your QPL query to the translator's translation function

  4. Obtain the engine-specific query generated by the translator

Example (ElasticsearchTranslator):

...

Code Block
languagepy
themeDJango
from pyqpl.translatortranslators 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)

Example (OpensearchTranslator):

Code Block
languagepy
themeDJango
from pyqpl.translators import OpensearchTranslator

# Create an instance of the OpensearchTranslator
translator = OpensearchTranslator()

# Define a QPL query
qpl_query = ElasticsearchTranslator()

...

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)

Custom Or Overwrites

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:

  1. Identify the operator type for which you want to add or overwrite the translation logic.
  2. Create a translation function that implements the desired transformation for the identified operator type.
  3. Initialize the translator and specify the custom translation functions using the custom_or_overwrites parameter.


Code Block
languagepy
themeDJango
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_engine_query(qpl_query)

...

 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