{"library":"sqlalchemy-searchable","title":"SQLAlchemy Searchable","type":"library","description":"SQLAlchemy Searchable provides full-text search capabilities for declarative SQLAlchemy models, primarily leveraging PostgreSQL's `tsvector` type for robust search. The current version is 3.0.0, and the library maintains a regular release cadence, with major versions often aligning with SQLAlchemy and PostgreSQL version support changes.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install sqlalchemy-searchable","pip install sqlalchemy-searchable[sqlalchemy_utils]","pip install sqlalchemy-searchable[psycopg2-binary]"],"cli":null},"imports":["from sqlalchemy_searchable import make_searchable","from sqlalchemy_searchable import SearchQueryMixin"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/falcony-io/sqlalchemy-searchable","docs":"https://sqlalchemy-searchable.readthedocs.io/","changelog":null,"pypi":"https://pypi.org/project/sqlalchemy-searchable/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nfrom sqlalchemy import create_engine, Column, Integer, String, Text\nfrom sqlalchemy.orm import sessionmaker, declarative_base\nfrom sqlalchemy_searchable import make_searchable\n# from sqlalchemy_utils import TSVectorType # Often used for explicit search vector column type\n\n# Database setup (using in-memory SQLite for simplicity)\n# For PostgreSQL, use 'postgresql://user:password@host:port/database'\nengine = create_engine(os.environ.get('SQLALCHEMY_DATABASE_URL', 'sqlite:///:memory:'))\nSession = sessionmaker(bind=engine)\nsession = Session()\n\nBase = declarative_base()\n\n# IMPORTANT: Call make_searchable BEFORE defining your models\nmake_searchable(Base.metadata)\n\nclass Document(Base):\n    __tablename__ = 'document'\n    # __searchable__ defines which columns contribute to the search vector\n    __searchable__ = ['title', 'content']\n\n    id = Column(Integer, primary_key=True)\n    title = Column(String(255))\n    content = Column(Text)\n    # For PostgreSQL, you might define an explicit TSVectorType:\n    # search_vector = Column(TSVectorType('title', 'content'))\n\n    def __repr__(self):\n        return f\"<Document(id={self.id}, title='{self.title}')>\"\n\nBase.metadata.create_all(engine)\n\n# Add some data\ndoc1 = Document(title=\"Python Programming Basics\", content=\"Learn the fundamentals of Python, including variables, data types, and control flow.\")\ndoc2 = Document(title=\"Advanced SQLAlchemy Techniques\", content=\"Explore advanced features of SQLAlchemy like custom types, events, and performance tuning.\")\ndoc3 = Document(title=\"Web Development with Flask\", content=\"Build web applications efficiently using the Flask microframework and Jinja2 templates.\")\n\nsession.add_all([doc1, doc2, doc3])\nsession.commit()\n\n# Perform searches\nprint(\"Searching for 'Python':\")\nresults_python = session.query(Document).search('Python').all()\nfor doc in results_python:\n    print(f\"- {doc.title}\")\n\nprint(\"\\nSearching for 'web applications':\")\nresults_web = session.query(Document).search('web applications').all()\nfor doc in results_web:\n    print(f\"- {doc.title}\")\n\nsession.close()","lang":"python","description":"This quickstart demonstrates how to integrate `sqlalchemy-searchable` into a basic SQLAlchemy application. It sets up a simple `Document` model, makes it searchable using `make_searchable`, and then performs a basic full-text search query. Note that while this example uses SQLite for simplicity, `sqlalchemy-searchable`'s full potential, especially for advanced full-text search, is realized with PostgreSQL.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}