#!/usr/bin/env python3 """ index_org_patents.py — Build the org_patents Elasticsearch index for one institution. Fetches all OpenAlex authors affiliated with the institution, searches EPO/USPTO for patents matched via institution assignee and/or author name (match_type: "institution", "name", or "both"), deletes existing documents for the institution, and reindexes. Usage: python scripts/index_org_patents.py --institution-id Example (University of Duisburg-Essen): python scripts/index_org_patents.py --institution-id I62318514 Requires src/main/inse/.env with PATENT_ELASTICSEARCH_URL/API_KEY (and OpenAlex ES credentials for author lookup). Runtime scales with author count (~30-40 min for UDE's 4,740 authors). Re-running is safe: existing documents are deleted before reindexing. Future: integrate into an Admin Panel so admins can trigger this via the UI. """ from __future__ import annotations import argparse import asyncio import logging import sys from pathlib import Path current_dir = Path(__file__).resolve().parent sys.path.insert(0, str(current_dir.parent / "src" / "main")) from dotenv import load_dotenv load_dotenv(dotenv_path=current_dir.parent / "src" / "main" / "inse" / ".env") from inse.patents.org.logic import index_org_patents_for_institution logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s") logger = logging.getLogger(__name__) async def main() -> None: parser = argparse.ArgumentParser( description="Build the org_patents index for one institution.", formatter_class=argparse.RawDescriptionHelpFormatter, epilog="Example: python scripts/index_org_patents.py --institution-id I62318514", ) parser.add_argument( "--institution-id", required=True, help="OpenAlex institution ID, e.g. I62318514 (University of Duisburg-Essen)", ) args = parser.parse_args() logger.info("Starting org_patents indexing for institution %s ...", args.institution_id) count = await index_org_patents_for_institution(args.institution_id) logger.info("Done. Indexed %d patents for institution %s.", count, args.institution_id) if __name__ == "__main__": asyncio.run(main())