searchfn
A lightweight, database-backed full-text search engine SDK for Python, designed as a companion plugin for DataFn.
It enables full-text search capabilities over your DataFn models by leveraging your existing database connection to store and query inverted indices.
Features
- Database Agnostic: Works with any DataFn-compatible DB adapter (Postgres, SQLite, etc.).
- Automatic Indexing: Scans your DataFn schema to identify and index string fields.
- Easy Integration: Provides a pluggable server dictionary compliant with the SuperFunctions HTTP abstraction.
- Pythonic API: Direct access to indexing and search functions for background tasks.
Installation
pip install searchfn
Quick Start
1. Setup
Initialize the SearchFn server configuration alongside your DataFn setup.
from searchfn import create_searchfn_server
from datafn.db import PostgresAdapter
from my_app.schema import schema # Your DataFn schema dict
# 1. Initialize Adapter
db = PostgresAdapter("postgresql://user:pass@localhost/db")
# 2. Create SearchFn Server Plugin
search_plugin = create_searchfn_server({
"schema": schema,
"db": db,
"table_prefix": "_searchfn_" # Optional
})
# 3. Integrate with your HTTP Framework
# The `search_plugin` returns a dict with a "routes" key mapping paths to handlers.
# You can mount these in your application (FastAPI, Flask, etc.)
routes = search_plugin["routes"]
2. Indexing Data
You can trigger indexing via the HTTP endpoint or programmatically.
Via HTTP:
curl -X POST http://localhost:8000/searchfn/index \
-H "Content-Type: application/json" \
-d '{ "model": "products" }'
Programmatically:
import asyncio
from searchfn import index_data
async def run_indexing():
await index_data(schema, db, model="products")
asyncio.run(run_indexing())
3. Searching
Via HTTP:
curl -X POST http://localhost:8000/searchfn/search \
-H "Content-Type: application/json" \
-d '{ "query": "fast laptop", "limit": 10 }'
Programmatically:
from searchfn import search_index
async def do_search():
results = await search_index(schema, db, query="fast laptop")
for res in results:
print(f"Found {res['id']} in {res['model']} (Score: {res['score']})")
API Reference
create_searchfn_server(config)
Creates the server plugin definitions.
Arguments:
config(dict):schema(required): The DataFn schema object/dict.db(required): An initialized DB adapter instance.table_prefix(optional): String prefix for internal tables (default:_searchfn_).
Returns:
- A dictionary containing
routes.
HTTP Endpoints
POST /searchfn/index
Triggers indexing.
- Payload:
{ "model": "optional_name" } - Response:
{ "ok": true, "result": { "totalIndexed": 123 } }
POST /searchfn/search
Performs a search.
- Payload:
{ "query": "text", "model": "optional_filter", "limit": 20 } - Response:
{ "ok": true, "results": [...] }
Core Functions
index_data(schema, db, model=None, table_prefix="_searchfn_")
Scans database records for the given model (or all models) and populates the index table.
search_index(schema, db, query, model=None, limit=20, table_prefix="_searchfn_")
Tokenizes the query, retrieves matching records from the index, and returns them sorted by relevance score.
How it Works
SearchFn maintains a simple Inverted Index in your database (default table: _searchfn_index).
- Tokenization: It splits text into tokens, lowercases them, and removes punctuation.
- Index Table: Stores rows mapping
term->recordId. - Scoring: Search results are ranked by the number of matching tokens found for a record.
This solution is ideal for:
- Prototyping and MVPs.
- Applications with moderate dataset sizes (tens to hundreds of thousands of records).
- Environments where adding a dedicated search service (Elasticsearch/Solr) is overkill.
License
MIT
Metadata
Release files for searchfn 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| searchfn-0.1.0.tar.gz | 5.3 kB | Without provenance | Details for searchfn-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| searchfn-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for searchfn-0.1.0-py3-none-any.whl |
Total release size: 11.1 kB
Release files / searchfn-0.1.0.tar.gz
| Download URL | searchfn-0.1.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4493d9d117aad18470d0bcb62648a28026e050775b3f7df91468494a1cda6ca8
|
|
BLAKE2b-256 checksum How to use checksums |
2e31660956f1c899013dd69cbc2f22fa4785939f130e814e67e29fd5c7ca7126
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / searchfn-0.1.0-py3-none-any.whl
| Download URL | searchfn-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2b0f92f79c6590710c3d4aaee4e64fc1adbc9cb2d87c9c74fa32bf03c8f2a9a8
|
|
BLAKE2b-256 checksum How to use checksums |
aa7eff12de5dbd9c48cd97d8aeff582dc4e126461aa9a20082c811b17b4917a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for searchfn 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.