|
| 1 | +import streamlit as st |
| 2 | +import typesense |
| 3 | +import traceback |
| 4 | +from typesense.exceptions import TypesenseClientError |
| 5 | +from urllib.parse import urlparse |
| 6 | +from .config import TYPESENSE_URL, TYPESENSE_API_KEY |
| 7 | + |
| 8 | +@st.cache_resource |
| 9 | +def get_typesense_client(): |
| 10 | + """Establishes and caches a connection to the Typesense client.""" |
| 11 | + if not TYPESENSE_URL or not TYPESENSE_API_KEY: |
| 12 | + st.error("TYPESENSE_URL and TYPESENSE_API_KEY must be set in your .env file.") |
| 13 | + return None |
| 14 | + |
| 15 | + try: |
| 16 | + # Parse URL using urllib for safety |
| 17 | + parsed = urlparse(TYPESENSE_URL) |
| 18 | + protocol = parsed.scheme or 'http' |
| 19 | + host = parsed.hostname or 'localhost' |
| 20 | + port = parsed.port or (443 if protocol == 'https' else 8108) |
| 21 | + |
| 22 | + config = { |
| 23 | + 'nodes': [{ |
| 24 | + 'host': host, |
| 25 | + 'port': str(port), |
| 26 | + 'protocol': protocol |
| 27 | + }], |
| 28 | + 'api_key': str(TYPESENSE_API_KEY), |
| 29 | + 'connection_timeout_seconds': 10 |
| 30 | + } |
| 31 | + |
| 32 | + client = typesense.Client(config) |
| 33 | + |
| 34 | + # Test connection by retrieving collections |
| 35 | + # This avoids using operations.perform which seems to have signature issues in the installed version |
| 36 | + client.collections.retrieve() |
| 37 | + return client |
| 38 | + |
| 39 | + except TypesenseClientError as e: |
| 40 | + st.error(f"Error connecting to Typesense: {e}. Please check if the service is running.") |
| 41 | + return None |
| 42 | + except Exception as e: |
| 43 | + st.error(f"An unexpected error occurred: {e}") |
| 44 | + st.write(f"Debug - Config used: {config if 'config' in locals() else 'Not created'}") |
| 45 | + st.write(f"Debug - Exception type: {type(e)}") |
| 46 | + st.code(traceback.format_exc()) |
| 47 | + return None |
| 48 | + |
| 49 | + |
| 50 | +def check_collection_exists(client, collection_name): |
| 51 | + """Check if a Typesense collection exists.""" |
| 52 | + try: |
| 53 | + client.collections[collection_name].retrieve() |
| 54 | + return True |
| 55 | + except TypesenseClientError: |
| 56 | + return False |
| 57 | + except Exception: |
| 58 | + return False |
| 59 | + |
| 60 | + |
| 61 | +def get_collection_stats(client, collection_name): |
| 62 | + """Get stats for a Typesense collection.""" |
| 63 | + try: |
| 64 | + collection = client.collections[collection_name].retrieve() |
| 65 | + return { |
| 66 | + 'number_of_documents': collection.get('num_documents', 0), |
| 67 | + 'name': collection.get('name', collection_name) |
| 68 | + } |
| 69 | + except Exception as e: |
| 70 | + st.error(f"Error getting collection stats: {e}") |
| 71 | + return None |
| 72 | + |
| 73 | + |
| 74 | +def get_collection(client, collection_name): |
| 75 | + """Get a Typesense collection object for direct operations.""" |
| 76 | + try: |
| 77 | + return client.collections[collection_name] |
| 78 | + except Exception as e: |
| 79 | + st.error(f"Error getting collection: {e}") |
| 80 | + return None |
| 81 | + |
| 82 | + |
| 83 | +def search_collection(client, collection_name, query="", **params): |
| 84 | + """Search a Typesense collection with given parameters.""" |
| 85 | + try: |
| 86 | + collection = client.collections[collection_name] |
| 87 | + return collection.documents.search(params) |
| 88 | + except Exception as e: |
| 89 | + st.error(f"Error searching collection: {e}") |
| 90 | + return None |
| 91 | + |
| 92 | + |
| 93 | +def get_documents(client, collection_name, **params): |
| 94 | + """Get documents from a Typesense collection.""" |
| 95 | + try: |
| 96 | + collection = client.collections[collection_name] |
| 97 | + # Typesense uses export for bulk document retrieval |
| 98 | + # But for compatibility with the page, we'll use search with high limit |
| 99 | + if 'limit' not in params: |
| 100 | + params['limit'] = 250 |
| 101 | + params['q'] = params.get('q', '*') # Wildcard search to get all documents |
| 102 | + |
| 103 | + result = collection.documents.search(params) |
| 104 | + return result.get('hits', []) |
| 105 | + except Exception as e: |
| 106 | + st.error(f"Error getting documents: {e}") |
| 107 | + return [] |
| 108 | + |
| 109 | + |
| 110 | +def get_collection_schema(client, collection_name): |
| 111 | + """Get the schema/settings for a Typesense collection.""" |
| 112 | + try: |
| 113 | + collection = client.collections[collection_name].retrieve() |
| 114 | + return collection |
| 115 | + except Exception as e: |
| 116 | + st.error(f"Error getting collection schema: {e}") |
| 117 | + return None |
0 commit comments