Versions Compared

Key

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

This functionality is only applicable when building a QPL query manually, if you want custom operators for the parser, you will need to generate a new grammar in which case, please contact the development team

PyQPL allows to include custom operators in your query and even add translation for these. There are 2 ways to make use of a custom operator, besides the translation process, one quick and simple and another one defining the operator with a class.

For both methods we need to make use of manual operator function (op)


Code Block
languagepy
themeDJango
qpl.op(type_='MY_OPERATOR', operands='one', fields=['title'], boost=1.0)

Simple Custom Operator

This method only consist on using the manual operator function (op), and adding the custom operator as the type, in this example GEO

Implementation

Code Block
qpl = QPL(options=options)

qpl_query = qpl.and_(operands=[
    qpl.or_(operands=[
        qpl.phrase('San Jose'),
        qpl.phrase('Costa Rica'),
        qpl.not_(qpl.phrase('California'))
    ]),
    qpl.op(type_='geo', operands=['9°54\'52.5"N', '84°05\'15.5"W'], fields='geo_point', boost=6)
])

print(qpl_query)


The resulting qpl query will give this

Code Block
and(or(phrase(San Jose),phrase(Costa Rica),not(phrase(California))),geo_point:geo(9°54'52.5"N,84°05'15.5"W)^6)
Note

From the important part is to know the operands(parameters) provided to the the function, since they could be anything we need to be extra careful when doing the translation

Custom Class Operator

This method is a more formal way to define the custom operator since it relays on a class to process, manipulate and optimize the query

Class Definition

The custom operator inherits from Operator Class, there for you can choose what to overwrite in order to fit your needs, in this case, we are validating that the operands are string, getting the cardinal coordinates and translating them into decimal coordinates

Tip

The operands are always expected to be Operators or string, so we need to transform the decimals into strings at the end

Code Block
import re

from pyqpl.qpl import QPLOptions, QPL, Operator
from pyqpl.qpl.operator import OperatorOrStr



class GeoOP(Operator):

    @staticmethod
    def adddms_to_operand(self, operand: OperatorOrStr):
decimal(dms_coordinate):
        # Use regular expressions to extract degrees, minutes, seconds, and cardinal direction
        oppattern = operand

r"(\d+)°(\d+)'([\d.]+)\"([NSWE])"
        match if= isinstancere.match(operandpattern, strdms_coordinate):

        if match:
   # I know this is not how you translate coordinates,degrees so shhh= int(match.group(1))
            opminutes = operandint(match.stripgroup(2).replace('\'', '').replace('"', '').replace('.', '')

)
            seconds = float(match.group(3))
            cardinal = match.group(4)

            # Convert degrees, minutes, and seconds to decimal degrees
            decimal = degrees if op.endswith('N') or op.endswith('n') or op.endswith('E') or op.endswith('E'):
+ minutes / 60 + seconds / 3600

            # Apply the cardinal direction to determine the sign of the decimal passcoordinate
            elif op.endswith('S') or op.endswith(if cardinal in ['S') or op.endswith(, 'W') or op.endswith('W'):
]:
                decimal = -decimal

      # we add the minus for thisreturn coordenatesdecimal
        else:
           op = '-' + opraise ValueError("Invalid coordinate format")

    def add_operand(self, operand: OperatorOrStr):

      # Remove the last letterif isinstance(operand, str):
            op = op[:-1]
 self.dms_to_decimal(operand)
        else:
            op = op.replace('°', '.raise ValueError('Invalid operand type, str expected')

        super().add_operand(str(op))

    def _optimize(self):
        super()._optimize()


Then when initializing the QPLOptions, we add the parameter custom_operators, this parameter accepts a dictionary, indicating the type of the operator and the class which will handle the type. 

The rest of the example is the same but check the result

Code Block
options = QPLOptions(fields='content', custom_operators={
    'geo': GeoOP
})

qpl = QPL(options=options)

qpl_query = qpl.and_(operands=[
    qpl.or_(operands=[
        qpl.phrase('San Jose'),
        qpl.phrase('Costa Rica'),
        qpl.not_(qpl.phrase('California'))
    ]),
    qpl.op(type_='geo', operands=['9°54\'52.5"N', '84°05\'15.5"W'], fields='geo_poit', boost=6)
])

print(qpl_query)


The result is the same qpl as with the simple method, but this time the coordinates where translated into decimal coordinates

Code Block
and(or(phrase(San Jose),phrase(Costa Rica),not(phrase(California))),geo_poit:geo(9.54525914583333333333,-84.0515508763888888889)^6)