This can be an alternative or a complement to parsing a query, you can either built an entire qpl query on your own or, parse a query and use the operator generated by the parsing into your manual qpl query, since parsing a query will return an Operator

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 apply
  • range_fields will be used if a range operator is specified, and no fields are assigned
  • date_fields will be used if a date_range operator is specified, and no fields are assigned
  • slop_adj sets the default slop for adjacent operator
  • slop_near sets the default slop for near operator
  • slop_before sets the default slop for before operator
  • slop_span_not sets the default slop for span_not operator


One 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

Operator Functions

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

Term

One single term

qpl.term(term='one', fields=['title'], boost=1.0)

Terms

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

Phrase

A literal phare

qpl.phrase(phrase='this is a phrase', fields=['title'], boost=1.0)

Or

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')])

And

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')])

Not

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')])

Range

A range of number with a start and an end

qpl.range(start=0.5, end=20, fields=['title'], boost=1.0)

Date Range

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)

Wildcard

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

Near

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)

Before

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)

Adjacent

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)

Span Not

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 Operator 

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)
  • No labels