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("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(
                  "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.10.0.tar.gz (108.1 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.10.0-cp39-abi3-win_amd64.whl (3.3 MB view details)

Uploaded CPython 3.9+Windows x86-64

solrstice-0.10.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.10.0-cp39-abi3-musllinux_1_2_armv7l.whl (4.0 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARMv7l

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

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

solrstice-0.10.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.10.0-cp39-abi3-manylinux_2_28_s390x.whl (3.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ s390x

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

Uploaded CPython 3.9+manylinux: glibc 2.28+ ppc64le

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

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARMv7l

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

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

solrstice-0.10.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.10.0.tar.gz.

File metadata

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

File hashes

Hashes for solrstice-0.10.0.tar.gz
Algorithm Hash digest
SHA256 66924fff97cd0df72eaf5130638295492bb90422ee5d30d2c42a8366baf8322d
MD5 f4272b5b502b03eed5badcca97eebba5
BLAKE2b-256 7642927e309b4cd37bffb0967120785e4e6075ee90742b8bb57aa2096ed5f2a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 394c5efdc92041729536bc959e9525ee7018f6d0bfef3e4e18574f21786f757e
MD5 51392a765c5e87eaec7f9159d00ddea0
BLAKE2b-256 d0f73c3ea127acd8ae5ff8fc7828b22281aa8b96e8eab57c62522413094dc017

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5e39a6ec8648039df34ad303b7c8703926e6346b01cd6c145a1c40fa75137a9d
MD5 a3e0b2e85df3bf111ebe6fc66c99c2ac
BLAKE2b-256 7127cb63fa7c5309652a29ff4520b937e626e7c41a239af2eaf3764a79048270

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 5bddbe219395ffc37e393e94a60412b0215b3183dd391edbfdab5e6a21f18c00
MD5 593a7d1ca37b2aa1b375178d9a39ca8c
BLAKE2b-256 a85a955c58b560d61b9cc03edc07eec4483a14a64d1dddf8166189f9c89674f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2d4e1ce7ffd7ba1dfbea4a354ffec51a1d44286408f08754e07e9572252d7fb8
MD5 0e022d3e42d7750b2fc9a19f886821ea
BLAKE2b-256 98f73588c6ee3040dc5a9ec5151c951b64e70b0f351f0de3eaaf782029447b42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 08ae5fddd78198356cb6e1125394a03afe8c902eea1f2dcad9af8231f3a8f8d7
MD5 6cb262b87eb3e3d6f1d517e51482374f
BLAKE2b-256 cb6519860cb65f232abc81a33694cfefabe0908addee1957fc66f556888c7320

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-manylinux_2_28_s390x.whl
Algorithm Hash digest
SHA256 9ee7c6b10db67e3156112db5570274ddf0260c99a31192eba005806c49f895c4
MD5 4a59337896de32827460ce1b76ba20e8
BLAKE2b-256 c22b9048637f90fa90fac0bdcd0ed68b162de4c5b7b197e4b5f696d8ccceb64a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-manylinux_2_28_ppc64le.whl
Algorithm Hash digest
SHA256 8f668324839b6c4cce65ae6d7f38e735a34891c3046278fd1a1d3def9f91a3a5
MD5 d10c2061bed617fd54c62933fe3728ca
BLAKE2b-256 8b458c7ee46df6a22ed4cfb6744bcadc2ad1694f405ba0cd4e0f50f88ef6bec4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 871d2dbed62605fcbda906c684a25a7ae52d9ca9738c8846c1ad6c8a617c31e7
MD5 2304b21ebbafa775d19bd5e224fa383c
BLAKE2b-256 1f98dc617221a7381685b92bec9a72bf3a305ca2a6c89175cd528224c9b08b7e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fe53f41c22b3195de40edd0911a593321e227d04fc7fc7dddcee01e990d7a266
MD5 08c24de207b02658687b1a4fd791ef2a
BLAKE2b-256 51b4bf79e97cb5e2464997e4167dc67c6829bd59ccae3235837528d412d025cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d756c91cd8bf58d588153ba62331b8133f1d0f29e51ee8a698d8bca4065fdc6
MD5 964df58a231ba69ea6abda05abf3de25
BLAKE2b-256 4650d1b960883661964a2f7f2d40a3ae985052aa676c77071da70bca0a05a9a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.10.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0517d2d69794ae0c87528756d33e0c2c96958695109b6157710d3b4dfa33a9ad
MD5 94b9aab75450b3e93f86476b27926f77
BLAKE2b-256 db8baac15d2830c05b680eda67a3e2611b8545facf483164aa4b08d9277fbea4

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