Generating a Query
To start writing your own queries first we need to import QPLOptions and the QPL
from pyqpl.qpl import QPL, QPLOptions
In QPLOptions we can set the default applicable for each operator
options = QPLOptions(fields='content', range_fields='cost', date_fields='updatedAt', slop_adj=0, slop_near=2, slop_before=10, slop_span_not=0)
fields
will indicate in which fields the terms and phrases will applyrange_fields
will be used if a range
operator is specified, and no fields are assigneddate_fields
will be used if a date_range
operator is specified, and no fields are assignedslop_adj
sets the default slop for adjacent
operatorslop_near
sets the default slop for near
operatorslop_before
sets the default slop for before
operatorslop_span_not
sets the default slop for span_not
operatorOne the options are set, create a parser and pass the options as argument
qpl = QPL(options=options)
With the instance created, create a query with any of the operator functions available
qpl_query = qpl.or_(operands=qpl.terms(terms=['This', 'is', 'PyQPL']))
The qpl_query will be an object of type Operator, which will contain all the operators of the query. At this point the qpl query is truly engine agnostic, and it needs a translator to be an engine query
The operator functions are listed bellow, each one have 1 o more examples, but they may not exemplify all the variations of types and formats the function can accept. Don't worry, the python code for this functions has defines all the types and formats each parameter can accept.
The parameter operands
refers to an Operator, string, number or list, it depends of the function you are using what allows
The fields
, boost
and slop
parameters are optional, since they have a default in QPLOptions, but if you want to overwrite that default an specific value
One single term
qpl.term(term='one', fields=['title'], boost=1.0)
Multiple terms, a simplification of term
qpl.terms(terms=['one', 'two'], fields=['title'], boost=1.0)
Make sure the end search engine(s) to which this query will be translated supports terms
A literal phare
qpl.phrase(phrase='this is a phrase', fields=['title'], boost=1.0)
An "or" of one or more operands
qpl.or_(operands=qpl.term(term='one')) qpl.or_(operands=[qpl.term(term='one'), qpl.term(term='two')])
An "and" of one or more operands
qpl.and_(operands=qpl.term(term='one')) qpl.and_(operands=[qpl.term(term='one'), qpl.term(term='two')])
A not of one or more operands
qpl.not_(operands=qpl.term(term='one')) qpl.not_(operands=[qpl.term(term='one'), qpl.term(term='two')])
A range of number with a start and an end
qpl.range(start=0.5, end=20, fields=['title'], boost=1.0)
A range of dates with a start and an end
qpl.date_range(start='27/12/22', end='22/06/23', fields=['title'], boost=1.0)
A wildcard where * is zero or many and ? mean one character
qpl.wildcard(wildcard='*.py', fields=['title'], boost=1.0)
The wildcard operator function does not required the wildcard option of QPLOptions in true, this option only applies for the parser
A near clause between 2 or more operands
qpl.near(operands=[qpl.or_(operands=[qpl.term(term='one'), qpl.term(term='two')])], fields=['title'], slop=10)
A before clause between 2 or more operands
qpl.before(operands=[qpl.or_(operands=[qpl.term(term='one'), qpl.term(term='two')])], fields=['title'], slop=2)
A adjacent clause between 2 or more operands
qpl.adj(operands=[qpl.or_(operands=[qpl.term(term='one'), qpl.term(term='two')])], fields=['title'], slop=0)
A span not clause between a set of operands to include and a set of operands to exclude
qpl.span_not(include=qpl.term(term='one'), exclude=qpl.term(term='two'), fields=['title'], slop=0)
Manually specified
There's also a manual way to specified the operator to use with the op function. This function is also the way to use custom operators, which will be address in the Custom Operator page
qpl.op(type_='term', operands='one', fields=['title'], boost=1.0)