from utils import load_config
from opensearch_sdk.opengauss_client import OpenGauss
mapping = {
"mappings": {
"properties": {
"categories": {"type": "text", "index": True},
"question": {"type": "keyword", "index": True},
"answerList": {"type": "text"}
}
}
}
config = load_config()
os_client = OpenGauss(
hosts=[{'host': config['host'], 'port': config['port']}],
database=config['database'],
user=config['user'],
password=config['password']
)
try:
print("Getting all index names (initial state)...")
index_names = os_client.cat.indices()
print(f"All index names: {index_names}")
print("\nChecking if index 'test_index' exists...")
exists = os_client.indices.exists(index="test_index")
print(f"Index 'test_index' exists: {exists}")
print("\nCreating index 'test_index'...")
result = os_client.create_index("test_index", mapping)
print(f"Index creation result: {result}")
print("\nChecking if index 'test_index' exists after creation...")
exists = os_client.indices.exists(index="test_index")
print(f"Index 'test_index' exists: {exists}")
print("\nGetting index 'test_index' information...")
info = os_client.indices.get(index="test_index")
print(f"Index information: {info}")
print("\nGetting all index names (after creating test_index)...")
index_names = os_client.cat.indices()
print(f"All index names: {index_names}")
print("\nGetting all index names using direct method...")
index_names = os_client.get_all_index_names()
print(f"All index names: {index_names}")
print("\nDeleting index 'test_index'...")
result = os_client.delete_index("test_index")
print(f"Index deletion result: {result}")
print("\nChecking if index 'test_index' exists after deletion...")
exists = os_client.indices.exists(index="test_index")
print(f"Index 'test_index' exists: {exists}")
print("\nGetting all index names (after deleting test_index)...")
index_names = os_client.cat.indices()
print(f"All index names: {index_names}")
finally:
os_client.close()
print("\nDatabase connection closed.")