Skip to main content

Build Riak search queries safely and easily.

Project description

rkQuery is a library for programmatically building Riak search queries. It aims to be easy to use, powerful, and protect from injection attacks that would be possible with simple string interpolation.

Example

Just start playing around with the q object:

>>> from rkquery import q
>>> q("some literal")
<q: "some literal">
>>> q(field="literal value")
<q: field:"literal value">
>>> q.not_(blocked="yes")
<q: NOT blocked:"yes">

You can provide multiple arguments, too. The default query combinator is AND:

>>> q("word1", "word2")
<q: "word1" AND "word2">
>>> q(username='foo', password='s3cr3t')
<q: username:"foo" AND password:"s3cr3t">

This is just a synonym for q.all():

>>> q.all("word1", "word2")
<q: "word1" AND "word2">
>>> q.all(username='foo', password='s3cr3t')
<q: username:"foo" AND password:"s3cr3t">

Of course you can construct OR queries, using q.any():

>>> q.any("word1", "word2")
<q: "word1" OR "word2">
>>> q.any(username='foo', email='foo@example.com')
<q: username:"foo" OR email:"foo@example.com">
>>> q(field=q.any("string1", "string2"))
<q: field:("string1" OR "string2")>

Or by combining existing q objects:

>>> q.any("word1", "word2") & q("word3")
<q: ("word1" OR "word2") AND "word3">
>>> q.any(email="foo@example.com", username="foo") & q(password="s3cr3t")
<q: (email:"foo@example.com" OR username:"foo") AND password:"s3cr3t">

There are helpers for negation as well (note that ‘none’ means ‘not any’):

>>> q.none(blocked="yes", cheque_bounced="yes")
<q: NOT (blocked:"yes" OR cheque_bounced:"yes")>
>>> ~q.any(blocked="yes", cheque_bounced="yes")
<q: NOT (blocked:"yes" OR cheque_bounced:"yes")>

You can do range queries with q.range():

>>> q.range("red", "rum")
<q: ["red" TO "rum"]>
>>> q(field=q.range("red", "rum"))
<q: field:["red" TO "rum"]>

Note that the default is an inclusive range (square brackets). The full set of range queries:

>>> q.range_inclusive("red", "rum")
<q: ["red" TO "rum"]>
>>> q.range_exclusive("red", "rum")
<q: {"red" TO "rum"}>
>>> q.between("red", "rum")
<q: {"red" TO "rum"}>

Term boosting is a simple unary operation:

>>> q("red").boost(5)
<q: "red"^5>

As is proximity:

>>> q("See spot run").proximity(20)
<q: "See spot run"~20>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rkquery-0.0.1.tar.gz (5.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page