Skip to main content

Async library for Typesense

Project description

Tests Codecov pypi versions

aio_typesense

Async Library for Typesense with type hints

pip install aio_typesense

Usage

# examples/example.py

import asyncio
from typing import TypedDict, List

from aio_typesense import Client, Collection


class Movie(TypedDict):
    id: str
    name: str
    year: int


MOVIES: List[Movie] = [
    {"id": "id1", "name": "Wonder Woman", "year": 2017, "year_facet": "2017"},
    {"id": "id2", "name": "Justice League", "year": 2017, "year_facet": "2017"},
    {"id": "id3", "name": "Wonder Woman 1984", "year": 2020, "year_facet": "2020"},
    {"id": "id4", "name": "Death on the Nile", "year": 2021, "year_facet": "2021"},
]


async def main():
    client = Client(
        node_urls=["http://localhost:8108"],
        api_key="Rhsdhas2asasdasj2",
    )

    r = await client.collections.create(
        {
            "name": "movies",
            "num_documents": 0,
            "fields": [
                {
                    "name": "name",
                    "type": "string",
                },
                {
                    "name": "year",
                    "type": "int32",
                },
                {
                    "name": "year_facet",
                    "type": "string",
                    "facet": True,
                },
            ],
            "default_sorting_field": "year",
        }
    )

    collection: Collection[Movie] = client.collections["movies"]

    r = await collection.documents.create_many(documents=MOVIES)
    print(r)

    search_r = await collection.documents.search(
        {
            "q": "wonder woman 2021",
            "query_by": "year_facet,name",
            "query_by_weights": "1,1",
        }
    )

    print(search_r["hits"])

    # [
    #     {
    #         "document": {
    #             "id": "id3",
    #             "name": "Wonder Woman 1984",
    #             "year": 2020,
    #             "year_facet": "2020",
    #         },
    #         "highlights": [
    #             {
    #                 "field": "year_facet",
    #                 "matched_tokens": ["2020"],
    #                 "snippet": "<mark>2020</mark>",
    #             }
    #         ],
    #         "text_match": 1125899907169635,
    #     },
    #     {
    #         "document": {
    #             "id": "id1",
    #             "name": "Wonder Woman",
    #             "year": 2017,
    #             "year_facet": "2017",
    #         },
    #         "highlights": [
    #             {
    #                 "field": "year_facet",
    #                 "matched_tokens": ["2017"],
    #                 "snippet": "<mark>2017</mark>",
    #             }
    #         ],
    #         "text_match": 1125899907169379,
    #     },
    #     {
    #         "document": {
    #             "id": "id4",
    #             "name": "Death on the Nile",
    #             "year": 2021,
    #             "year_facet": "2021",
    #         },
    #         "highlights": [
    #             {
    #                 "field": "year_facet",
    #                 "matched_tokens": ["2021"],
    #                 "snippet": "<mark>2021</mark>",
    #             }
    #         ],
    #         "text_match": 562949953552128,
    #     },
    #     {
    #         "document": {
    #             "id": "id2",
    #             "name": "Justice League",
    #             "year": 2017,
    #             "year_facet": "2017",
    #         },
    #         "highlights": [
    #             {
    #                 "field": "year_facet",
    #                 "matched_tokens": ["2017"],
    #                 "snippet": "<mark>2017</mark>",
    #             }
    #         ],
    #         "text_match": 562949953551616,
    #     },
    # ]


if __name__ == "__main__":
    asyncio.run(main())

Contributing

Prerequisites:

  • poetry
  • nox
  • nox-poetry

Install them on your system:

pip install poetry nox nox-poetry

Run tests:

nox

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

aio_typesense-0.1.0.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

aio_typesense-0.1.0-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

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