|
1 | 1 | # sqlalchemy-paradedb |
2 | 2 |
|
3 | | -SQLAlchemy query helpers for ParadeDB. |
| 3 | +Typed SQLAlchemy helpers for ParadeDB BM25 indexing and query composition. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Python 3.10+ |
| 8 | +- PostgreSQL with ParadeDB (`pg_search`) available |
| 9 | +- SQLAlchemy 2.x |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install sqlalchemy-paradedb |
| 15 | +``` |
| 16 | + |
| 17 | +For local development: |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install -e .[test,dev] |
| 21 | +``` |
| 22 | + |
| 23 | +## Core Modules |
| 24 | + |
| 25 | +- `paradedb.sqlalchemy.indexing`: BM25 field definitions and tokenizer specs. |
| 26 | +- `paradedb.sqlalchemy.search`: ParadeDB predicates (`match_all`, `fuzzy`, `parse`, `more_like_this`, etc.). |
| 27 | +- `paradedb.sqlalchemy.pdb`: function wrappers (`score`, `snippet`, `snippets`, `agg`). |
| 28 | +- `paradedb.sqlalchemy.facets`: aggregate/facet JSON builders and rows+facets helper. |
| 29 | +- `paradedb.sqlalchemy.select_with`: select decorators for score/snippet columns. |
| 30 | +- `paradedb.sqlalchemy.alembic`: Alembic operations for BM25 index lifecycle. |
| 31 | + |
| 32 | +## Quickstart |
| 33 | + |
| 34 | +```python |
| 35 | +from sqlalchemy import Index, select |
| 36 | +from paradedb.sqlalchemy import indexing, search |
| 37 | + |
| 38 | +products_bm25_idx = Index( |
| 39 | + "products_bm25_idx", |
| 40 | + indexing.BM25Field(Product.id), |
| 41 | + indexing.BM25Field(Product.description, tokenizer=indexing.tokenize.unicode(lowercase=True)), |
| 42 | + indexing.BM25Field(Product.category, tokenizer=indexing.tokenize.literal()), |
| 43 | + postgresql_using="bm25", |
| 44 | + postgresql_with={"key_field": "id"}, |
| 45 | +) |
| 46 | + |
| 47 | +products_bm25_idx.create(engine) |
| 48 | + |
| 49 | +stmt = select(Product.id, Product.description).where(search.match_any(Product.description, "running", "shoes")) |
| 50 | +``` |
| 51 | + |
| 52 | +## Query APIs |
| 53 | + |
| 54 | +- Basic predicates: `match_all`, `match_any`, `term`, `phrase`, `fuzzy`, `regex`, `all` |
| 55 | +- Advanced predicates: `parse`, `phrase_prefix`, `regex_phrase`, `near`, `proximity`, `more_like_this` |
| 56 | +- Scoring/snippets: `pdb.score`, `pdb.snippet`, `pdb.snippets`, `select_with.score`, `select_with.snippet` |
| 57 | +- Aggregations/facets: `facets.*` builders + `pdb.agg(...)` |
| 58 | +- Rows + facets: `facets.with_rows(...)` |
| 59 | + |
| 60 | +## Facets |
| 61 | + |
| 62 | +```python |
| 63 | +from sqlalchemy import select |
| 64 | +from paradedb.sqlalchemy import facets, pdb, search |
| 65 | + |
| 66 | +stmt = ( |
| 67 | + select( |
| 68 | + pdb.agg(facets.value_count(field="id")).label("count"), |
| 69 | + pdb.agg(facets.avg(field="rating")).label("avg_rating"), |
| 70 | + ) |
| 71 | + .select_from(Product) |
| 72 | + .where(search.match_all(Product.description, "running")) |
| 73 | +) |
| 74 | +``` |
| 75 | + |
| 76 | +## Alembic Operations |
| 77 | + |
| 78 | +Import once in migration env startup so operations are registered: |
| 79 | + |
| 80 | +```python |
| 81 | +import paradedb.sqlalchemy.alembic # noqa: F401 |
| 82 | +``` |
| 83 | + |
| 84 | +Usage: |
| 85 | + |
| 86 | +```python |
| 87 | +op.create_bm25_index("products_bm25_idx", "products", ["id", "description"], key_field="id") |
| 88 | +op.reindex_bm25("products_bm25_idx", concurrently=True) |
| 89 | +op.drop_bm25_index("products_bm25_idx", if_exists=True) |
| 90 | +``` |
| 91 | + |
| 92 | +## Validation and Guardrails |
| 93 | + |
| 94 | +- Search and facet builders validate option bounds and shapes at build time. |
| 95 | +- `select_with.snippet*` raises `SnippetWithFuzzyPredicateError` with fuzzy predicates. |
| 96 | +- `facets.with_rows` enforces `ORDER BY` + `LIMIT`, and can auto-inject a ParadeDB sentinel (`pdb.all()`). |
| 97 | + |
| 98 | +## Examples |
| 99 | + |
| 100 | +See `examples/`: |
| 101 | + |
| 102 | +- `quickstart.py` |
| 103 | +- `faceted_search.py` |
| 104 | +- `autocomplete.py` |
| 105 | +- `more_like_this.py` |
| 106 | +- `hybrid_rrf.py` |
| 107 | +- `rag.py` |
| 108 | + |
| 109 | +## Testing |
| 110 | + |
| 111 | +Unit tests: |
| 112 | + |
| 113 | +```bash |
| 114 | +python -m pytest tests/unit |
| 115 | +``` |
| 116 | + |
| 117 | +Integration tests (requires running ParadeDB): |
| 118 | + |
| 119 | +```bash |
| 120 | +PARADEDB_TEST_DSN=postgres://postgres:postgres@localhost:5432/postgres python -m pytest -m integration |
| 121 | +``` |
| 122 | + |
| 123 | +## CI |
| 124 | + |
| 125 | +GitHub Actions workflow at `.github/workflows/ci.yml` runs: |
| 126 | + |
| 127 | +- Ruff lint |
| 128 | +- Mypy type check |
| 129 | +- Unit tests |
| 130 | +- Integration tests against a ParadeDB service container |
0 commit comments