smileBeda
11/26/2024, 9:00 PMimport typesense
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Retrieve environment variables
host = os.getenv('TYPESENSE_HOST')
api_key = os.getenv('TYPESENSE_API_KEY')
protocol = os.getenv('TYPESENSE_PROTOCOL', 'https')
port = os.getenv('TYPESENSE_PORT', '443')
timeout_seconds = int(os.getenv('TYPESENSE_TIMEOUT_SECONDS', 3600))
openai_api_key = os.getenv('OPENAI_API_KEY')
openai_model_name = os.getenv('OPENAI_MODEL_NAME')
client = typesense.Client({
'nodes': [{
'host': host, # For Typesense Cloud use xxx.a1.typesense.net
'port': port, # For Typesense Cloud use 443
'protocol': protocol, # For Typesense Cloud use https
'path': '/api'
}],
'api_key': api_key,
'connection_timeout_seconds': timeout_seconds
})
filepath = input("JSONL: ")
with open(filepath) as jsonl_file:
print(client.collections['pp_posts'].documents.import_(jsonl_file.read().encode('utf-8'), {'batch_size': 100}))
to upsert, and
import typesense
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Retrieve environment variables
host = os.getenv('TYPESENSE_HOST')
api_key = os.getenv('TYPESENSE_API_KEY')
protocol = os.getenv('TYPESENSE_PROTOCOL', 'https')
port = os.getenv('TYPESENSE_PORT', '443')
timeout_seconds = int(os.getenv('TYPESENSE_TIMEOUT_SECONDS', 3600))
openai_api_key = os.getenv('OPENAI_API_KEY')
openai_model_name = os.getenv('OPENAI_MODEL_NAME')
client = typesense.Client({
'nodes': [{
'host': host, # For Typesense Cloud use xxx.a1.typesense.net
'port': port, # For Typesense Cloud use 443
'protocol': protocol, # For Typesense Cloud use https
'path': '/api'
}],
'api_key': api_key,
'connection_timeout_seconds': timeout_seconds
})
schema = {
"name": "pp_posts",
"fields": [
.......
]
}
print(client.collections.create(schema))
to create schema
In the cloud... I am a bit confused
1. Can I use the exact same code to create schema? or do i need to use the webui?
2. We have 3 zones on high availablality. Do I need to upsert to each and every node?!
3. In my own setup I had /api
endpoint. In the cloud I do not need such endpoint?
So it becomes:
client = typesense.Client({
'nearest_node': {'host': 'xxx.a1.typesense.net', 'port': posrt, 'protocol': protocol}, # This is the special Nearest Node hostname that you'll see in the Typesense Cloud dashboard if you turn on Search Delivery Network
'nodes': [
{'host': 'xxx-1.a1.typesense.net', 'port': port, 'protocol': protocol},
{'host': 'xxx-2.a1.typesense.net', 'port': port, 'protocol': protocol},
{'host': 'xxx-3.a1.typesense.net', 'port': port, 'protocol': protocol}
],
'api_key': api_key,
'connection_timeout_seconds': timeout_seconds
})
is this correct?Fanis Tharropoulos
11/26/2024, 10:27 PMFanis Tharropoulos
11/26/2024, 10:28 PM/api
endpoint or not is up to you, the client itself will call out to the cluster, so dedicating an /api
endpoint isn't necessary, but a choice you makeJason Bosco
11/27/2024, 1:33 AMWe have 3 zones on high availablality. Do I need to upsert to each and every node?!No, sending the data to the load balanced hostname will automatically cause the data to be replicated to all the nodes in the cluster