from utils import load_config
from opensearch_sdk.opengauss_client import OpenGauss
from opensearch_sdk.helpers import Index
config = load_config()
client = OpenGauss(
hosts=[{'host': config['host'], 'port': config['port']}],
database=config['database'],
user=config['user'],
password=config['password']
)
try:
client.indices.create(
index='test1',
body={
'settings': {
'number_of_shards': 1,
'number_of_replicas': 0
},
'mappings': {
'properties': {
'title': {'type': 'text'},
'description': {'type': 'text'},
'vector_field': {
'type': 'float_vector',
'dimension': 128
}
}
}
}
)
print("Index 'test1' created successfully.")
if client.indices.exists(index='test1'):
print("Index 'test1' exists!")
info = client.indices.get(index='test1')
print(f"Index information: {info}")
client.indices.delete(index='test1')
print("Index 'test1' deleted.")
index = Index('test1', using=client)
index.settings(
number_of_shards=1,
number_of_replicas=0
)
index.mapping({
'title': {'type': 'text'},
'description': {'type': 'text'},
'vector_field': {
'type': 'float_vector',
'dimension': 128
}
})
index.create()
print("Index 'test1' created using helper class.")
if index.exists():
print("Index 'test1' exists!")
index.delete()
print("Index 'test1' deleted using helper class.")
finally:
client.close()
print("Database connection closed.")