Skip to main content

Python Solr query utility

Project description

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 python2.6, python2.7, python3.3, python3.4, python3.5, 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).

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.

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

solrq-1.1.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

solrq-1.1.1-py2.py3-none-any.whl (11.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file solrq-1.1.1.tar.gz.

File metadata

  • Download URL: solrq-1.1.1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for solrq-1.1.1.tar.gz
Algorithm Hash digest
SHA256 1fd1fa77fbfb09d7a0a4752bc23c53acf734fc4a52b0718eb19171319e64bc30
MD5 cf34f67003265ae746e1948bd5abb5d2
BLAKE2b-256 c5511055c735f59446f028457aeb27432e4765a8f0bcc4ff059f1e01bb011c03

See more details on using hashes here.

File details

Details for the file solrq-1.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for solrq-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 eec72c6025b829a4004876a52a0e42cd8d6e2046860ff8cd65130d9361d37dda
MD5 d7fae3cf28baf3db694427a12f3eb927
BLAKE2b-256 91052ab6e14fc24fab4aa67fcaa2ecc878323028918a925f55a8f4b61dafa113

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page