Skip to main content

Server SDK for SearchFn - Full text search for DataFn

Project description

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).

  1. Tokenization: It splits text into tokens, lowercases them, and removes punctuation.
  2. Index Table: Stores rows mapping term -> recordId.
  3. 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

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

searchfn-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

searchfn-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file searchfn-0.1.0.tar.gz.

File metadata

  • Download URL: searchfn-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for searchfn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4493d9d117aad18470d0bcb62648a28026e050775b3f7df91468494a1cda6ca8
MD5 ad042107f2322dbeb279b0d29a6431aa
BLAKE2b-256 2e31660956f1c899013dd69cbc2f22fa4785939f130e814e67e29fd5c7ca7126

See more details on using hashes here.

File details

Details for the file searchfn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: searchfn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for searchfn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b0f92f79c6590710c3d4aaee4e64fc1adbc9cb2d87c9c74fa32bf03c8f2a9a8
MD5 a573ff63f5879307706b2f98635bb62d
BLAKE2b-256 aa7eff12de5dbd9c48cd97d8aeff582dc4e126461aa9a20082c811b17b4917a9

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