Skip to main content

A Solr client library written in Rust

Project description

Solrstice: A Solr 8+ Client for Rust and Python

Solrstice is a solr client library written in rust. With this wrapper you can use it in python.

Both asyncio and blocking clients are provided. All apis have type hints.

Features

  • Config API
  • Collection API
  • Alias API
  • Select Documents
    • Grouping Component Query
    • Stats Component
    • DefTypes (lucene, dismax, edismax)
    • Facet Counts (Query, Field, Pivot)
    • Json Facet (Query, Stat, Terms, Nested)
  • Indexing Documents
  • Deleting Documents

Installation

pip install solrstice

Basic Usage

Async

import asyncio

from solrstice import SolrBasicAuth, SolrServerContext, SolrSingleServerHost, AsyncSolrCloudClient, UpdateQuery, \
    SelectQuery, DeleteQuery

# A SolrServerContext specifies how the library should interact with Solr
context = SolrServerContext(SolrSingleServerHost('localhost:8983'), SolrBasicAuth('solr', 'SolrRocks'))
client = AsyncSolrCloudClient(context)


async def main() -> None:
    # Create config and collection
    await client.upload_config('example_config', 'path/to/config')
    await client.create_collection('example_collection', 'example_config', shards=1, replication_factor=1)

    # Index a document
    await client.index(UpdateQuery(), 'example_collection', [{'id': 'example_document', 'title': 'Example document'}])

    # Search for the document
    response = await client.select(SelectQuery(fq=['title:Example document']), 'example_collection')
    docs_response = response.get_docs_response()
    assert docs_response is not None
    assert docs_response.get_num_found() == 1
    docs = docs_response.get_docs()

    # Delete the document
    await client.delete(DeleteQuery(ids=['example_document']), 'example_collection')


asyncio.run(main())

Blocking

from solrstice import SolrBasicAuth, BlockingSolrCloudClient, SolrServerContext, SolrSingleServerHost, DeleteQuery, \
    SelectQuery, UpdateQuery

# A SolrServerContext specifies how the library should interact with Solr
context = SolrServerContext(SolrSingleServerHost('localhost:8983'), SolrBasicAuth('solr', 'SolrRocks'))
client = BlockingSolrCloudClient(context)

# Create config and collection
client.upload_config('example_config', 'path/to/config')
client.create_collection('example_collection', 'example_config', shards=1, replication_factor=1)

# Index a document
client.index(UpdateQuery(), 'example_collection', [{'id': 'example_document', 'title': 'Example document'}])

# Search for the document
response = client.select(SelectQuery(fq=['title:Example document']), 'example_collection')
docs_response = response.get_docs_response()
assert docs_response is not None
assert docs_response.get_num_found() == 1
docs = docs_response.get_docs()

# Delete the document
client.delete(DeleteQuery(ids=['example_document']), 'example_collection')

Grouping component

Field grouping

from solrstice import GroupingComponent, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  group_builder = GroupingComponent(fields=["age"], limit=10)
  select_builder = SelectQuery(fq=["age:[* TO *]"], grouping=group_builder)
  groups = (await client.select(select_builder, "example_collection")).get_groups()
  age_group = groups["age"]
  docs = age_group.get_field_result()

Query grouping

from solrstice import GroupingComponent, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  group_builder = GroupingComponent(queries=["age:[0 TO 59]", "age:[60 TO *]"], limit=10)
  select_builder = SelectQuery(fq=["age:[* TO *]"], grouping=group_builder)
  groups = (await client.select(select_builder, "example_collection")).get_groups()
  age_group = groups["age:[0 TO 59]"]
  group = age_group.get_query_result()
  assert group is not None
  docs = group.get_docs()

Query parsers

Lucene

from solrstice import LuceneQuery, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  query_parser = LuceneQuery(df="population")
  select_builder = SelectQuery(q="outdoors", def_type=query_parser)
  response = (await client.select(select_builder, "example_collection")).get_docs_response()
  assert response is not None
  docs = response.get_docs()

Dismax

from solrstice import DismaxQuery, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  query_parser = DismaxQuery(qf="interests^20", bq=["interests:cars^20"])
  select_builder = SelectQuery(q="outdoors", def_type=query_parser)
  response = (await client.select(select_builder, "example_collection")).get_docs_response()
  assert response is not None
  docs = response.get_docs()

Edismax

from solrstice import EdismaxQuery, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  query_parser = EdismaxQuery(qf="interests^20", bq=["interests:cars^20"])
  select_builder = SelectQuery(q="outdoors", def_type=query_parser)
  response = (await client.select(select_builder, "example_collection")).get_docs_response()
  assert response is not None
  docs = response.get_docs()

FacetSet Component

Pivot facet

from solrstice import FacetSetComponent, PivotFacetComponent, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  select_builder = SelectQuery(facet_set=FacetSetComponent(pivots=PivotFacetComponent(["interests,age"])))
  response = await client.select(select_builder, "example_collection")
  facets = response.get_facet_set()
  pivots = facets.get_pivots()
  interests_age = pivots.get("interests,age")

Field facet

from solrstice import FacetSetComponent, FieldFacetComponent, FieldFacetEntry, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  facet_set = FacetSetComponent(fields=FieldFacetComponent(fields=[FieldFacetEntry("age")]))
  select_builder = SelectQuery(facet_set=facet_set)
  response = await client.select(select_builder, "example_collection")
  facets = response.get_facet_set()
  fields = facets.get_fields()
  age = fields.get("age")

Query facet

from solrstice import AsyncSolrCloudClient, SolrServerContext, SelectQuery, FacetSetComponent, FacetSetComponent
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  select_builder = SelectQuery(facet_set=FacetSetComponent(queries=["age:[0 TO 59]"]))
  response = await client.select(select_builder, "example_collection")
  facets = response.get_facet_set()
  queries = facets.get_queries()
  query = queries.get("age:[0 TO 59]")

Stats Component

from solrstice import StatsComponent, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  select_builder = SelectQuery(stats=StatsComponent(fields=["age"]))
  response = await client.select(select_builder, "example_collection")
  stats = response.get_stats()
  assert stats is not None
  age_stats = stats.get_fields()["age"]
  age_count = age_stats.get_count()

Json Facet Component

Query

from solrstice import JsonFacetComponent, JsonQueryFacet, SelectQuery, SolrServerContext, AsyncSolrCloudClient
async def main() -> None:
  client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))
  select_builder = SelectQuery(
      json_facet=JsonFacetComponent(
          facets={"below_60": JsonQueryFacet(q="age:[0 TO 59]")}
      )
  )
  response = await client.select(select_builder, "example_collection")
  facets = response.get_json_facets()
  assert facets is not None
  below_60 = facets.get_nested_facets().get("below_60")
  assert below_60 is not None
  assert below_60.get_count() == 4

Stat

from solrstice import JsonFacetComponent, JsonStatFacet, SelectQuery, SolrServerContext, AsyncSolrCloudClient
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  select_builder = SelectQuery(
      json_facet=JsonFacetComponent(
          facets={"total_people": JsonStatFacet("sum(count)")}
      )
  )
  response = await client.select(select_builder, "example_collection")
  facets = response.get_json_facets()
  assert facets is not None
  total_people = facets.get_flat_facets()["total_people"]
  assert total_people == 1000

Terms

from solrstice import AsyncSolrCloudClient, SolrServerContext, SelectQuery, JsonFacetComponent, JsonTermsFacet
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  select_builder = SelectQuery(
      json_facet=JsonFacetComponent(facets={"age": JsonTermsFacet("age")})
  )
  response = await client.select(select_builder, "example_collection")
  facets = response.get_json_facets()
  assert facets is not None
  age_buckets = facets.get_nested_facets()["age"].get_buckets()
  assert len(age_buckets) == 3

Nested

from solrstice import AsyncSolrCloudClient, SolrServerContext, SelectQuery, JsonFacetComponent, JsonQueryFacet, JsonStatFacet
client = AsyncSolrCloudClient(SolrServerContext('localhost:8983'))

async def main() -> None:
  select_builder = SelectQuery(
      json_facet=JsonFacetComponent(
          facets={
              "below_60": JsonQueryFacet(
                  q="age:[0 TO 59]",
                  facets={"total_people": JsonStatFacet("sum(count)")},
              )
          }
      )
  )
  response = await client.select(select_builder, "example_collection")
  facets = response.get_json_facets()
  assert facets is not None
  total_people = (
      facets.get_nested_facets()
      ["below_60"]
      .get_flat_facets()
      .get("total_people")
  )
  assert total_people == 750.0

Hosts

Single Server

from solrstice import SolrServerContext, SolrSingleServerHost, SolrBasicAuth, AsyncSolrCloudClient

context = SolrServerContext(SolrSingleServerHost('localhost:8983'), SolrBasicAuth('solr', 'SolrRocks'))
client = AsyncSolrCloudClient(context)

Multiple servers

from solrstice import SolrServerContext, SolrMultipleServerHost, SolrBasicAuth, AsyncSolrCloudClient

# The client will randomly select a server to send requests to. It will wait 5 seconds for a response, before trying another server.
context = SolrServerContext(
    SolrMultipleServerHost(["localhost:8983", "localhost:8984"], 5),
    SolrBasicAuth('solr', 'SolrRocks'),
)
client = AsyncSolrCloudClient(context)

Zookeeper

from solrstice import SolrServerContext, ZookeeperEnsembleHostConnector, SolrBasicAuth, AsyncSolrCloudClient

async def main() -> None:
  context = SolrServerContext(
      await ZookeeperEnsembleHostConnector(["localhost:2181"], 30).connect(),
      SolrBasicAuth('solr', 'SolrRocks'),
  )
  client = AsyncSolrCloudClient(context)

Notes

  • Multiprocessing does not work, and will block forever. Normal multithreading works fine.

  • Pyo3, the Rust library for creating bindings does not allow overriding the __init__ method on objects from Rust. __new__ has to be overridden instead.

    For example, if you want to create a simpler way to create a client

    from typing import Optional
    from solrstice import SolrServerContext, SolrSingleServerHost, SolrBasicAuth, AsyncSolrCloudClient, SolrAuth
    class SolrClient(AsyncSolrCloudClient):
        def __new__(cls, host: str, auth: Optional[SolrAuth] = None):
            context = SolrServerContext(SolrSingleServerHost(host), auth)
            return super().__new__(cls, context=context)
    client = SolrClient("localhost:8983", SolrBasicAuth("username", "password"))
    

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

solrstice-0.11.0.tar.gz (108.2 kB view details)

Uploaded Source

Built Distributions

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

solrstice-0.11.0-cp39-abi3-win_amd64.whl (3.3 MB view details)

Uploaded CPython 3.9+Windows x86-64

solrstice-0.11.0-cp39-abi3-musllinux_1_2_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

solrstice-0.11.0-cp39-abi3-musllinux_1_2_armv7l.whl (4.0 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARMv7l

solrstice-0.11.0-cp39-abi3-musllinux_1_2_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

solrstice-0.11.0-cp39-abi3-manylinux_2_28_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

solrstice-0.11.0-cp39-abi3-manylinux_2_28_s390x.whl (3.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ s390x

solrstice-0.11.0-cp39-abi3-manylinux_2_28_ppc64le.whl (4.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ppc64le

solrstice-0.11.0-cp39-abi3-manylinux_2_28_armv7l.whl (3.7 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARMv7l

solrstice-0.11.0-cp39-abi3-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

solrstice-0.11.0-cp39-abi3-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

solrstice-0.11.0-cp39-abi3-macosx_10_12_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file solrstice-0.11.0.tar.gz.

File metadata

  • Download URL: solrstice-0.11.0.tar.gz
  • Upload date:
  • Size: 108.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.1

File hashes

Hashes for solrstice-0.11.0.tar.gz
Algorithm Hash digest
SHA256 aa6b30e272d3e6edf73c68362ce3d3f19cbb3098f3f1174fb33fa7d9486b30ae
MD5 4be5ac5549efdaf3828f3afc70f16924
BLAKE2b-256 6db0389b15935b8bf3dff5951fab9b2b16453b9bd84160d3e9857c41c34248b9

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e45f9b940a197788cd7801beab6364f717a14eb7ed3b00db672a28ff988147f2
MD5 4454afe0198dc0f15113927af1ab380b
BLAKE2b-256 53acbc8e9f6085156f743c35d86752a4ec5320061ba9fc30a2bb96a328bfbfff

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a2ac0f6b05e28f6d697a62d121d2c82f0fffeaae4d123e425686cd6324c6a2e7
MD5 f787cd85c9cff6adc9ea372054f0b528
BLAKE2b-256 609c14af3ad8f0fd50e976f2575f871914b6cab02fe3b8d12d0f4eb261240379

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 a5e89e02540cea833758562dc6a2e5bf168c9ac9a79bd96395ec21c6717f4cef
MD5 7b5cb86556a0c30d14267175a185f3e2
BLAKE2b-256 e66fc91c64f3d90f7db6b99277dacce62af67b7d84f33c26494050eb81ff3ea6

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 74974eecf14bb99b20d336cbeed55df5eb32095d069eab310e3b4239459c5b8f
MD5 6a1779adfbbcbc1df289ee166371c6cd
BLAKE2b-256 51d833ca34508422109ac1d1dacd4708f109cdcb979e8b733f9ce4a16ddb293e

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e460d06088fc57e1e93aa84e158a0f836c77aa4758269326cbc592e34a0b42b9
MD5 32fd30d1b4436c661046d542df54c1dd
BLAKE2b-256 11dfdfe16239d17dce39cbdfdee82d7d1449256f4ab4874f8abb99eff1dc70c5

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-manylinux_2_28_s390x.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-manylinux_2_28_s390x.whl
Algorithm Hash digest
SHA256 82022b98d0f8be9b87921a9a09dfe452d61ab8789eb1d886e87a687c36fca3d0
MD5 1f8e51097d1a23bb399eba64f11e117b
BLAKE2b-256 d65a58606f3cf382671fff37f420b83c372d859597c768f34d1d5a8142abfc13

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-manylinux_2_28_ppc64le.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-manylinux_2_28_ppc64le.whl
Algorithm Hash digest
SHA256 efb787d95583b52d8b806c30b1a0b7cc581416e78a5d09de8c8bae084748021c
MD5 2e5b0bc5c364f4306719417479276efb
BLAKE2b-256 8971f8c5be6f952fbb83d18efafe99cf24294e0741681ae52f866c03a7385c78

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-manylinux_2_28_armv7l.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 3411144dd6a1e024143513ed79d30160e779f176e5090b4e8ed9e2a6a00c0bc4
MD5 e5adc0a0a4bcb96d234ca8853bec1a72
BLAKE2b-256 d55c75d71cf7da286af68dd2201fc0e2a9963ec76ad4e900edca8cf61fbcd4d3

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9f06bbd1c2cd38b694a50945a6d9daf736fd7be53d2685a75af2133d002ab048
MD5 b417dd8f33a485bb6691e9410fb4ce1d
BLAKE2b-256 1b791f220d24e4c8ecbfc2590e58aef9bd1a446e18bcc5d6943b8aef65e41942

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 806f2527b97bde49b016492ab5c1666f9b6af1d0ae4e8061d6ea953042a08b02
MD5 1086bd9e60d28e90ae79b487f20c7bec
BLAKE2b-256 a05c1eb9b567379863f41a79118809e86db25a975971bef162f02c881233a0ae

See more details on using hashes here.

File details

Details for the file solrstice-0.11.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for solrstice-0.11.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fc182b645c87cc6bd2edd1cda534cd6705f269db53c9c2e22b92cde774705be1
MD5 64da509338228edc5120e0939e133936
BLAKE2b-256 76d9f6ed9f4919005fcc1404dea518d491e475b6fc83d51fbe876be2fa3a76f1

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