Skip to main content

Lightweight Python client for Apache Solr.

Project description

aiosolr

AsyncIO Python client for Apache Solr

Requirements

This project requires Python 3.6+

Installation

To install with pip

pip install aiosolr

Usage

The connection to the Solr backend is defined during object initialization. The accepted kwargs to init are scheme, host, port, collection, and timeout.

Note: collection may also be passed at query time

import aiosolr

client = aiosolr.Client(host=localhost, collection="example", port=8983)

Alternatively you may instantiate via passing a connection URL like:

import aiosolr

client = aiosolr.Client(connection_url="http://host:1234/path/to/solr/collection")

Once you have your Solr instance, you need to setup the session like:

client.setup()

Then there are methods available for querying. You can use Solr's built-in get handler with the get method like:

client.get(document_id)

to retrieve a single document.

You can use a pre-defined suggestions handler by using the suggestions method like:

client.suggestions("suggest_handler", query="asdf")

You can also use the suggestions method to build your suggestions like:

client.suggestions("suggest_handler", build=True)

Note: handler is a required argument for suggestions unlike for get or query

You can use the query method to query your search handler. The default handler used is "select". If you would like spellcheck suggestion turned on, pass spellcheck=True (default is False).

client.query(handler="my_handler", query="asdf", spellcheck=True)

If spellcheck is True the query method returns a tuple with the first element being an array of documents and the 2nd element being an array of spellcheck suggestions. Otherwise, the query method returns a simple array of documents.

You can use the update method to access Solr's built-in update handler like:

client.update(my_data)

At any point that you need to commit data to your collection you can use the commit method. Arguments should be the "handler" ("update" by default) and "soft" as a boolean indicating whether it should be a hard or soft commit (defaults to False).

There is one more method you might want to use before querying Solr especially if the query is coming from an untrusted end user. There is a clean_query method which can be used to strip out unwanted characters. The function signature allows the following arguments:

@staticmethod
def clean(
    query,  # end user query
    allow_html_tags=False,
    allow_http=False,
    allow_wildcard=False,
    escape_chars=(":", r"\:"),  # tuple of (replace_me, replace_with)
    max_len=200,
    # regex of chars to remove
    remove_chars=r'[\&\|\!\(\)\{\}\[\]\^"~\?\\;]',
    urlencode=True,
):

Use it like:

trusted_query = aiosolr.clean_query(users_query)

Once you are finished with the Solr instance, you should call the method close to cleanup sessions like:

await client.close()

Debugging

To get more information from the Client you can initialize with debug=True like:

import aiosolr

client = aiosolr.Client(host=localhost, collection="example", port=8983, debug=True)

This sets the aiosolr logger to debug level, and also sets the internally used HTTP session (provied by AIOHTTP) to a debug level.

This makes it easier to see the actual network request going to Solr.

Project details


Release history Release notifications | RSS feed

This version

4.3.0

Download files

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

Source Distribution

aiosolr-4.3.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiosolr-4.3.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file aiosolr-4.3.0.tar.gz.

File metadata

  • Download URL: aiosolr-4.3.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.7

File hashes

Hashes for aiosolr-4.3.0.tar.gz
Algorithm Hash digest
SHA256 6df2ed9403f8827bab2b47b70fc1a7794504e0545b501f93e61d645c93b8995d
MD5 d85a5713c88499d1851edacbedbedf56
BLAKE2b-256 08e04288c8b0af9d269e97774c670b29435a6b75efaab2e9572cb3e9d0b53b76

See more details on using hashes here.

File details

Details for the file aiosolr-4.3.0-py3-none-any.whl.

File metadata

  • Download URL: aiosolr-4.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.7

File hashes

Hashes for aiosolr-4.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0788064f9625b1c24f01ff42c2fb990715b63b00a7b5372b36c6922fcdd44e9
MD5 358683d65b11c01d595983f1948e1f54
BLAKE2b-256 ac1d8740b51b6146e1d7b17584e61d741af690c1fbb2785f3a75aca2e9b0b4bf

See more details on using hashes here.

Supported by

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