I am moving something from a selfhosted to cloud U...
# community-help
s
I am moving something from a selfhosted to cloud Until now I used:
Copy code
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
})

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
Copy code
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:
Copy code
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?
f
The only thing you'd change in your code is transferring your configuration's host over to Typesense cloud, and adding the cloud nodes. Everything from then on is left as is. Cloud's dashboard isn't meant to replace your code, it's meant to compliment your Typesense experience by visualizing results
🆗 1
Also, whether you prefer to relay your calls to an
/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 make
j
We 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
🙏 1