Versions Compared

Key

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

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

Code Block
languagepy
theme

...

DJango
from pyqpl.qpl import QPL, QPLOptions

In QPLOptions we can set the default applicable for each operator

Code Block
languagepy
theme

...

DJango
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


Table of Contents

One the options are set, create a parser and pass the options as argument

Code Block
languagepy
theme

...

DJango
qpl = QPL(options=options)


With the instance created, create a query with any of the operator functions available

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

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

Terms

Multiple terms, a simplification of term

Code Block
languagepy
theme

...

DJango
qpl.terms(terms=['one', 'two'], fields=['title'], boost=1.0)
Tip

Make sure the end search engine(s) to which this query will be translated supports terms

Phrase

A literal phare

Code Block
languagepy
theme

...

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

Or

An "or" of one or more operands

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

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

Date Range

A range of dates with a start and an end

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

DJango
qpl.wildcard(wildcard='*.py', fields=['title'], boost=1.0)
Tip

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

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

DJango
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

Code Block
languagepy
theme

...

DJango
qpl.span_not(include=qpl.term(term='one'), exclude=qpl.term(term='two'), fields=['title'], slop=0)



Manually

...

Specified Operator 
Anchor
manualOp
manualOp

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

Code Block
languagepy
theme

...

DJango
qpl.op(type_='term', operands='one', fields=['title'], boost=1.0)