typesense/typesense_schema.py

37 lines
1.2 KiB
Python

# Using Python client for schema creation
import typesense
client = typesense.Client({
'nodes': [{
'host': '192.168.2.203', # Or your Typesense host
'port': '8108',
'protocol': 'http'
}],
'api_key': 'imdiowenxodjioqwenlj', # Use admin key for schema operations
'connection_timeout_seconds': 2
})
# Define the schema
users_schema = {
'name': 'users',
'fields': [
{'name': 'user_id', 'type': 'string', 'index': True, 'facet': False},
{'name': 'user_name', 'type': 'string', 'index': True, 'facet': False, 'infix': True},
# Optional: for cleaner prefix sorting if needed, otherwise _text_match often suffices
{'name': 'user_name_lowercase_sort', 'type': 'string', 'sort': True, 'optional': True}
]
}
# Delete collection if it already exists (for development)
try:
client.collections['users'].delete()
print("Collection 'users' deleted.")
except Exception as e:
print(f"Collection 'users' does not exist or could not be deleted: {e}")
# Create the collection
try:
client.collections.create(users_schema)
print("Collection 'users' created successfully.")
except Exception as e:
print(f"Error creating collection 'users': {e}")