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

Uploaded CPython 3.9+Windows x86-64

solrstice-0.13.0-cp39-abi3-musllinux_1_2_x86_64.whl (4.3 MB view details)

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

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

Uploaded CPython 3.9+musllinux: musl 1.2+ ARMv7l

solrstice-0.13.0-cp39-abi3-musllinux_1_2_aarch64.whl (4.2 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

solrstice-0.13.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.13.0-cp39-abi3-manylinux_2_28_s390x.whl (4.0 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ s390x

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

Uploaded CPython 3.9+manylinux: glibc 2.28+ ppc64le

solrstice-0.13.0-cp39-abi3-manylinux_2_28_armv7l.whl (3.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARMv7l

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

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

solrstice-0.13.0-cp39-abi3-macosx_10_12_x86_64.whl (3.8 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for solrstice-0.13.0.tar.gz
Algorithm Hash digest
SHA256 ef159cffc3ec0c54f40a42a719cfd29e4e0ed01be35a846dc7c12725cd88e729
MD5 f546c0718d49e3aa729d156936076513
BLAKE2b-256 fa616f7480e81474846612576e8b813f4a7b4a51b3058764a585df06bd856b64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 47cc56f986c52e2473d6d1bc78ff132bb9d37df9001ea5e60c1cb0223f615bac
MD5 e5523f9d6dd4033e3d3df9796b69ca60
BLAKE2b-256 aa8848a8f142ec85f006e3b57ce3a5f66ed36fada9d9c301f7b50697f5ba80a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 11666f9f6df04de4e95dede0f84f4475c1f0e33ed23017b762f38b02e1a1b8ba
MD5 e114fe8a37481d62d9bc5291fbfde45f
BLAKE2b-256 f23645b298baee1ae6d96a2349d21f9896dc6a45a7388bf7b57fc4d771d6581e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 e4362e22e35a4218fbfcddb9056941046f40c89e86e9924ffacb1c29f77fcf23
MD5 89a88f30eec8a01ac90851ad6bf1b49f
BLAKE2b-256 21650137aa621728ef9f1376cbf5083e65ef84ba82b6d0347b5f84cc5150acc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 873965f7daee512c4a9613a67bed81b79146b65ab3ebbbe4655e0d0c84eb211b
MD5 7372e7aec17006276517dd07807a8a04
BLAKE2b-256 0561e6ece8723393ffa4341e2a78a2b3ae01ad22c4fb1f7da93c9381bda414c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bfc82d4df744228a67e09b2fc603774dcdad910cfae65911feb01abfa9fd5c41
MD5 d71ea6c3510a60559535f1979b267dcf
BLAKE2b-256 be0a27faa3e490cff29bd403e11bfab0d4e29d5c4321dfc9aa81ce9f6d1ce9ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-manylinux_2_28_s390x.whl
Algorithm Hash digest
SHA256 490a36c1d714f67698b095a9b092d82afaa5726aca4e648a5177c01f7762797b
MD5 c761e9591ede0dc00176921a90f6cdd5
BLAKE2b-256 01d82bc70ddf0d5c54f1139f0e2959068c01c85560a2256ac1c18ea5df039119

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-manylinux_2_28_ppc64le.whl
Algorithm Hash digest
SHA256 fce75b8eab1bdbc05b1c376cd947b0ae7f29271c1d64dcd50a4aa5d33d929d4a
MD5 81cd1dd0c58d9ba0ca6e4a8f4900c9e5
BLAKE2b-256 2143af548299065e8415319a05aa5274f4fd1a40f4bf5643a43b3b2db3658698

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-manylinux_2_28_armv7l.whl
Algorithm Hash digest
SHA256 0cfffc26a49f4f7d5e0b5b2390d926118417692452b9eabfdf3f4b3a63646439
MD5 77e8ac602599dc0c2ca9f72a753e79ce
BLAKE2b-256 07e0b023db57a8373a4c48f4acb7684c243d3eb43f94e15b6490563318a64b4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6ce70f8e243ab1f668ac5f65c3be8ff594d7786b0617491fc8ec015593a4619c
MD5 8a6d7b079eeb15ea3c0394d6fdd228fd
BLAKE2b-256 45724cb02fe174980f5a658e5d5decb827aafed53b31b8ec69b20a9e7be3c239

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a1f1b4ae0267d3faa8b2e3a7d7f10bc52c94e1654f095356487b5669159b4bca
MD5 e4bf03bbefe8e09719c65f3f429c15e6
BLAKE2b-256 2780f3db6f9d83f4eefa07c99ffae21992555488f9eb2e8474109b80e962fb86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solrstice-0.13.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 75a14f317e7abd23908d313df820c2ca10249b461976bf6ab91ba43bd298096d
MD5 27b08c0fc1db03760deaba7f3fb4dcdc
BLAKE2b-256 4c3f1c43b9133d6b639bdcbdcf5d5b73b6e525a05b1f73190d0b8f6bdeccbd0d

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