🔗Access

This guide explains how to access existing indexes on the enVector server, list all available indexes, and create client SDK objects to examine their contents and perform operations.

Overall Usage

The process of accessing existing indexes involves these main steps:

  1. Connect to Server - Initialize the SDK and connect to the enVector server

  2. List Available Indexes - Retrieve a list of all indexes currently on the server

  3. Get Index Information - Obtain detailed information about specific indexes

  4. Create Index Objects - Instantiate Index objects for existing indexes to perform operations

  5. Examine Index Contents - Access index data, metadata, and perform searches

Detailed Usage Using Arguments

1. Connect to the Server

First, establish a connection to the enVector server:

import pyenvector as ev

# Initialize connection to the server
HOST_ADDRESS = "your.hostname:50050"
ev.init(address=HOST_ADDRESS, key_path="./keys", key_id="your-key-id")

print("Connected to enVector server successfully")

2. List All Available Indexes

Retrieve a list of all indexes currently registered on the server:

# Get list of all indexes
index_list = ev.get_index_list()

if index_list:
    print(f"Found {len(index_list)} indexes on the server:")
    for i, index_name in enumerate(index_list, 1):
        print(f"  {i}. {index_name}")
else:
    print("No indexes found on the server")

3. Get Detailed Index Information

For each index, you can retrieve detailed information:

def get_index_details(index_name):
    """Get detailed information about a specific index"""
    try:
        index_info = ev.get_index_info(index_name)
        print(f"\nIndex: {index_name}")
        print(f"  Dimension: {index_info['dim']}")
        print(f"  Row Count: {index_info['row_count']}")
        print(f"  Search Type: {index_info['search_type']}")
        print(f"  Key ID: {index_info['key_id']}")
        print(f"  Index Encryption: {index_info['index_encryption']}")
        print(f"  Query Encryption: {index_info['query_encryption']}")
        print(f"  Created Time: {index_info['created_time']}")
        return index_info
    except Exception as e:
        print(f"Failed to get info for index '{index_name}': {e}")
        return None

# Get details for all indexes
for index_name in index_list:
    get_index_details(index_name)

Alternatively, you can simply print the returned value of the get_index_info API:

print(ev.get_index_info(index_name))

4. Create Index Objects for Existing Indexes

Create Index objects to interact with existing indexes:

from pyenvector.index import Index

try:
    # Create Index object for existing index
    index = Index(index_name)    
    print(f"\nSuccessfully created Index object for '{index_name}'")
except Exception as e:
    print(f"Failed to create Index object for '{index_name}': {e}")

5. Examine Index Contents

Once you have Index objects, you can examine their contents by simply printing the object:

print(index)

You can also access the index configuration object to examine specific settings:

# Get the index configuration object
config = index.index_config

# Access configuration properties
print(f"Index name: {config.index_name}")
print(f"Dimension: {config.dim}")
print(f"Key ID: {config.key_id}")
print(f"Index encryption: {config.index_encryption}")
print(f"Query encryption: {config.query_encryption}")
print(f"Index type: {config.index_type}")
print(f"Preset: {config.preset}")

6. Complete Working Example

Here's a complete script that demonstrates all the functionality described above:

import pyenvector as ev
from pyenvector.index import Index

def main():
    # 1. Connect to server
    HOST_ADDRESS = "localhost:50050"
    ev.init(address=HOST_ADDRESS, key_path="./keys", key_id="test-key")
    print("Connected to enVector server")
    
    # 2. List all indexes
    index_list = ev.get_index_list()
    if not index_list:
        print("No indexes found on the server")
        return
    
    print(f"Found {len(index_list)} indexes: {index_list}")
    
    # 3. Access each index
    for index_name in index_list:
        print(f"\n{'='*50}")
        print(f"Accessing Index: {index_name}")
        print(f"{'='*50}")
        
        try:
            # Create Index object
            index = Index(index_name)
            
            # Get index information
            index_info = ev.get_index_info(index_name)
            print(f"Index Details:")
            for key, value in index_info.items():
                print(f"  {key}: {value}")
            
            # Examine contents
            print(f"\nIndex Object Properties:")
            print(f"  Dimension: {index.dim}")
            print(f"  Entities: {index.num_entities}")
            print(f"  Connected: {index.is_connected}")            
            
        except Exception as e:
            print(f"Error accessing index '{index_name}': {e}")
    
    print(f"\n{'='*50}")
    print("Index access completed")

if __name__ == "__main__":
    main()

7. Error Handling

Here's an example of comprehensive error handling:

def safe_index_access(index_name):
    """Safely access an index with comprehensive error handling"""
    try:
        # Check if index exists
        index_list = ev.get_index_list()
        if index_name not in index_list:
            print(f"Index '{index_name}' not found on server")
            return None
        
        # Get index info
        index_info = ev.get_index_info(index_name)
        
        # Create Index object
        index = Index(index_name)
        
        return index, index_info
        
    except ValueError as e:
        if "Indexer not connected" in str(e):
            print("Server connection error. Please check your connection.")
        elif "Key ID not found" in str(e):
            print(f"Key not found for index '{index_name}'. Please register the key first.")
        else:
            print(f"Configuration error: {e}")
        return None, None
        
    except Exception as e:
        print(f"Unexpected error accessing index '{index_name}': {e}")
        return None, None

8. Best Practices

  1. Connection Management: Always check connection status before performing operations

  2. Error Handling: Implement comprehensive error handling for network and configuration issues

  3. Resource Cleanup: Properly manage Index objects to avoid memory leaks

9. Troubleshooting

Common Issues and Solutions:

  • "Indexer not connected": Reinitialize the SDK with ev.init()

  • "Key ID not found": Register the key first using ev.register_key()

  • "Index not found": Verify the index name exists using ev.get_index_list()

  • Connection timeouts: Check network connectivity and server status

This guide provides comprehensive coverage of accessing existing indexes on the enVector server, allowing you to examine their contents and perform operations using the client SDK.

Last updated