Skip to main content

Build Status Coverage Status Documentation Status

solrq

solrq is a Python Solr query utility. It helps making query strings for Solr and also helps with escaping reserved characters. solrq is has no external dependencies and is compatibile with python3.7, python3,8, python3.9, python3.10, python3.11, pypy and pypy3. It might be compatibile with other python releases/implentations but this has not been tested yet or is no longer tested (e.g python3.2 or python2.7).

pip install solrq

And you're ready to go!

usage

Everything in solrq is about Q() object. Drop into python repl and just feed it with bunch of field and search terms to see how it works:

>>> from solrq import Q
>>> # note: all terms in single Q object are implicitely joined with 'AND'
>>> query = Q(type="animal", species="dog")
>>> query
<Q: type:animal AND species:dog>

>>> # ohh, forgot about cats?
>>> query | Q(type="animal", species="cat")
<Q: (type:animal AND species:dog) OR (type:animal AND species:cat)>

>>># more a cat lover? Let's give them a boost boost
>>> Q(type="animal") & (Q(species="cat")^2 | Q(species="dog"))
<Q: type:animal AND ((species:cat^2) OR species:dog)>

But what to do with this Q? Simply pass it to your Solr library of choice, like pysolr or mysolr. Most of python Solr libraries expect simple string as a query parameter and do not bother with escaping of reserved characters so you must take care of that by yourself. This is why solrq integrates so easily. Here is an example how you can use it with pysolr:

from solrq import Q
import pysolr

solr = Solr("<your solr url>")

# simply using Q object
solr.search(Q(text="easy as f***"))

# or explicitely making it string
solr.search(str(Q(text="easy as f***")))

quick reference

Full reference can be found in API reference documentation page but here is a short reference.

boosting queries

Use python ^ operator:

>>> Q(text='cat') ^ 2
<Q: text:cat^2>

AND queries

Use python & operator:

>>> Q(text='cat') & Q(text='dog')
<Q: text:cat AND text:dog>

OR queries

Use python | operator:

>>> Q(text='cat') | Q(text='dog')
<Q: text:cat OR text:dog>

NOT queries

Use python ~ operator:

>>> ~ Q(text='cat')
<Q: !text:cat>

ranges

Use solrq.Range wrapper:

>>> from solrq import Range
>>> Q(age=Range(18, 25))
<Q: age:[18 TO 25]>

proximity searches

Use solrq.Proximity wrapper:

>>> from solrq import Proximity
>>> Q(age=Proximity("cat dogs", 5))
<Q: age:"cat\ dogs"~5>

safe strings

All raw string values are treated as unsafe by default and will be escaped to ensure that final query string will not be broken by some rougue search value. This of course can be disabled if you know what you're doing using Value wrapper:

>>> from solrq import Q, Value
>>> Q(type='foo bar[]')
<Q: type:foo\ bar\[\]>
>>> Q(type=Value('foo bar[]', safe=True))
<Q: type:foo bar[]>

timedeltas, datetimes

Simply as:

>>> from datetime import datetime, timedelta
>>> Q(date=datetime(1970, 1, 1))
<Q: date:"1970-01-01T00:00:00Z">
>>> # note that timedeltas has any sense mostly with ranges
>>> Q(delta=timedelta(days=1))
<Q: delta:NOW+1DAYS+0SECONDS+0MILLISECONDS>

field wildcard

If you need to use wildcards in field names just use dict and unpack it inside of Q() instead of using keyword arguments:

    >>> Q(**{"*_t": "text_to_search"})
    <Q: *_t:text_to_search>

contributing

Any contribution is welcome. Issues, suggestions, pull requests - whatever. There are no strict contribution guidelines beyond PEP-8 and sanity. Code style is checked with flakes8 and any PR that has failed build will not be merged.

One thing: if you submit a PR please do not rebase it later unless you are asked for that explicitely. Reviewing pull requests that suddenly had their history rewritten just drives me crazy.

testing

Tests are run using tox. Simply install it and run:

pip install tox
tox

And that's all.

Metadata

Release files for solrq 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for solrq 1.1.2
File Size Uploaded
solrq-1.1.2.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for solrq 1.1.2
File Interpreter ABI Platform
solrq-1.1.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 20.7 kB

Release files / solrq-1.1.2.tar.gz

Download URL solrq-1.1.2.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9ddd607ebb5f4521f925f10fcaae444f861c0a83862f47154efa8d14c173cc41
BLAKE2b-256 checksum
How to use checksums
2cd8ba3d98034f149f3707dee33f5b1b5020c264006ba5caa87270c41580e314
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / solrq-1.1.2-py2.py3-none-any.whl

Download URL solrq-1.1.2-py2.py3-none-any.whl
Size 9.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7fa57431d8a26955be91733c999266ec204b6f28cbea0eb45d21e6d282b00ff8
BLAKE2b-256 checksum
How to use checksums
869ca0eef68c7b1208fc44ced94b6a211c834bf9f40c46e0880dce5694067f19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.5

2 release files

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page