Index

module index

Index Module

This module provides classes and methods for managing index configurations and operations.

  • IndexConfig — Configuration class for index settings.

  • Index — Class for managing index operations.

class IndexConfig

class IndexConfig()

Configuration class for index settings.

ARGUMENTS

  • index_name (str): Name of the index.

  • dim (int): Dimensionality of the index.

  • key_path (str): Path to the key.

  • key_id (str): ID of the key.

  • seal_mode (str): Seal mode for the key.

  • seal_kek_path (str): KeK for AES Seal Mode

  • preset (str): Preset for the index.

  • eval_mode (str): Evaluation mode for the index.

  • query_encryption (str): The encryption type for query, e.g. "plain", "cipher", "hybrid".

  • index_encryption (str): The encryption type for database, e.g. "plain", "cipher", "hybrid".

  • index_params (dict): Parameters for the index.

  • metadata_encryption (bool): The encryption type for metadata, e.g. True, False.


__init__()

def __init__(index_name: Optional[str] = None,
             dim: Optional[int] = None,
             key_path: Optional[str] = None,
             key_id: Optional[str] = None,
             seal_mode: Optional[str] = None,
             seal_kek_path: Optional[str] = None,
             preset: Optional[str] = None,
             eval_mode: Optional[str] = None,
             query_encryption: Optional[str] = None,
             index_encryption: Optional[str] = None,
             index_params: Optional[dict] = None,
             index_type: Optional[str] = None,
             metadata_encryption: Optional[bool] = None)

Initializes the IndexConfig class.


index_name()

@property
def index_name() -> str

Returns the index name.

RETURNS

str: Name of the index.


index_name()

@index_name.setter
def index_name(index_name: str)

Sets the index name.

ARGUMENTS

  • index_name (str): Name of the index.


context_param()

@property
def context_param() -> ContextParameter

Returns the context parameter object.

RETURNS

ContextParameter: The parameter object for this context.


context_param()

@context_param.setter
def context_param(context_param: ContextParameter)

Sets the context parameter object.

ARGUMENTS

  • context_param (ContextParameter): The parameter object for this context.


key_param()

@property
def key_param() -> KeyParameter

Returns the key parameter object.

RETURNS

KeyParameter: The parameter object for the key.


key_param()

@key_param.setter
def key_param(key_param: KeyParameter)

Sets the key parameter object.

ARGUMENTS

  • key_param (KeyParameter): The parameter object for the key.


index_param()

@property
def index_param() -> IndexParameter

Returns the index parameter object.

RETURNS

IndexParameter: The parameter object for the index.


index_param()

@index_param.setter
def index_param(index_param: IndexParameter)

Sets the index parameter object.

ARGUMENTS

  • index_param (IndexParameter): The parameter object for the index.


preset()

@property
def preset() -> str

Returns the preset.

RETURNS

str: Preset for the index.


preset()

@preset.setter
def preset(preset: str)

Sets the preset.

ARGUMENTS

  • preset (str): Preset for the index.


dim()

@property
def dim() -> int

Returns the dimensionality of the index.

RETURNS

int: Dimensionality of the index.


dim()

@dim.setter
def dim(dim: int)

Sets the dimensionality of the index.

ARGUMENTS

  • dim (int): Dimensionality of the index.


eval_mode()

@property
def eval_mode() -> str

Returns the evaluation mode.

RETURNS

str: Evaluation mode for the context.


eval_mode()

@eval_mode.setter
def eval_mode(eval_mode: str)

Sets the evaluation mode.

ARGUMENTS

  • eval_mode (str): Evaluation mode for the context.


search_type()

@property
def search_type() -> str

Returns the search type.

RETURNS

str: Search type for the index.


index_encryption()

@property
def index_encryption() -> str

Returns whether database encryption is enabled.

RETURNS

str: The encryption type for database, e.g. "plain", "cipher", "hybrid".


index_encryption()

@index_encryption.setter
def index_encryption(index_encryption: str)

Sets whether database encryption is enabled.

ARGUMENTS

  • index_encryption (str): The encryption type for database, e.g. "plain", "cipher", "hybrid".


query_encryption()

@property
def query_encryption() -> str

Returns whether query encryption is enabled.

RETURNS

str: The encryption type for query, e.g. "plain", "cipher", "hybrid".


query_encryption()

@query_encryption.setter
def query_encryption(query_encryption: str)

Sets whether query encryption is enabled.

ARGUMENTS

  • query_encryption (str): The encryption type for query, e.g. "plain", "cipher", "hybrid".


index_type()

@property
def index_type() -> str

Returns the index type.

RETURNS

str: Type of the index.


index_type()

@index_type.setter
def index_type(index_type: str)

Sets the index type.

ARGUMENTS

  • index_type (str): Type of the index.


index_params()

@property
def index_params() -> dict

Returns the index parameters.

RETURNS

dict: Parameters for the index.


nlist()

@property
def nlist()

Returns the nlist parameter for IVF indices.

RETURNS

int: Number of clusters (nlist) for IVF indices.


default_nprobe()

@property
def default_nprobe()

Returns the default nprobe parameter for IVF indices.

RETURNS

int: Default number of probes (nprobe) for IVF indices.


centroids()

@property
def centroids()

Returns the centroids for IVF indices.

RETURNS

list[list[float]]: Centroids for IVF indices.


key_path()

@property
def key_path() -> str

Returns the key path.

RETURNS

str: Path to the key.


key_path()

@key_path.setter
def key_path(key_path: str)

Sets the key path.

ARGUMENTS

  • key_path (str): Path to the key.


key_id()

@property
def key_id() -> str

Returns the key ID.

RETURNS

str: ID of the key.


key_id()

@key_id.setter
def key_id(key_id: str)

Sets the key ID.

ARGUMENTS

  • key_id (str): ID of the key.


seal_info()

@property
def seal_info() -> SealInfo

Returns the seal mode.

RETURNS

str: Seal mode for the keys.


seal_mode()

@property
def seal_mode() -> str

Returns the seal mode.

RETURNS

str: Seal mode for the keys.


seal_kek_path()

@property
def seal_kek_path() -> str

Returns the seal KEK path.

RETURNS

str: Path to the seal KEK.


eval_key_path()

@property
def eval_key_path() -> str

Returns the evaluation key path.

RETURNS

str: Path to the evaluation key.


enc_key_path()

@property
def enc_key_path() -> str

Returns the encryption key path.

RETURNS

str: Path to the encryption key.


sec_key_path()

@property
def sec_key_path() -> str

Returns the secret key path.

RETURNS

str: Path to the secret key.


metadata_enc_key_path()

@property
def metadata_enc_key_path() -> str

Returns the metadata encryption key path.

RETURNS

str: Path to the metadata encryption key.


key_dir()

@property
def key_dir() -> str

Returns the directory where the keys are stored.

RETURNS

str: Directory for the keys.


need_cipher()

@property
def need_cipher() -> bool

Returns whether cipher operations are needed.

RETURNS

bool: True if cipher operations are needed, False otherwise.


deepcopy()

def deepcopy(index_name: Optional[str] = None,
             dim: Optional[int] = None,
             key_path: Optional[str] = None,
             key_id: Optional[str] = None,
             seal_mode: Optional[str] = None,
             seal_kek_path: Optional[str] = None,
             preset: Optional[str] = None,
             eval_mode: Optional[str] = None,
             query_encryption: Optional[str] = None,
             index_encryption: Optional[str] = None,
             index_params: Optional[dict] = None,
             metadata_encryption: Optional[bool] = None) -> "IndexConfig"

Creates a deep copy of the index configuration.

RETURNS

IndexConfig: A deep copy of the index configuration.

class Index

class Index()

Class for managing index operations.

ARGUMENTS

  • index_config (IndexConfig): Configuration for the index.

  • indexer (Indexer): Indexer object for managing connections.

  • num_entities (int): Number of entities in the index.

  • cipher (Cipher): Cipher object for encryption and decryption.


__init__()

def __init__(index_name: str, index_config: Optional[IndexConfig] = None)

Initializes the Index class.

Check server connection and check if the index exists.

ARGUMENTS

  • index_name (str): Name of the index.


init_connect()

@classmethod
def init_connect(cls,
                 address: str,
                 access_token: Optional[str] = None) -> "Indexer"

Connects to the indexer.

ARGUMENTS

  • address (str): Address of the indexer.

RETURNS

Indexer: Connected indexer object.


init_key_path()

@classmethod
def init_key_path(cls, key_path: str)

Initializes the key path for the index.

ARGUMENTS

  • key_path (str): Path to the key directory.


create_index()

@classmethod
def create_index(cls,
                 index_config: IndexConfig,
                 indexer: Optional[Indexer] = None) -> "Index"

Creates a new index.

ARGUMENTS

  • index_config (IndexConfig): Configuration for the index.

  • indexer (Indexer): Indexer object for managing connections.

RETURNS

Index: The created index.


insert()

def insert(data: Union[List[List[float]], List[np.ndarray], np.ndarray,
                       List[CipherBlock]],
           metadata: List[Any] = None)

Inserts data into the index.

ARGUMENTS

  • data (list of floats, list of np.ndarray, 2D np.ndarray, or list of CipherBlock): Data to be inserted. It can be plaintext (list of lists, list of numpy arrays, or 2D numpy array) or ciphertext (CipherBlock). Currently, only a list of CipherBlock is supported for encrypted data.

  • metadata (str): Metadata for the data.

RETURNS

Index: The index object after insertion.


def search(query: Union[List[float], np.ndarray, List[List[float]],
                        List[np.ndarray], List[CipherBlock]],
           top_k: int,
           output_fields: List[str] = None,
           search_params: dict = None)

Searches the index.

ARGUMENTS

  • query (list of float or np.ndarray): Query vector.

  • top_k (int): Number of top results to return (default 3).

  • output_fields (list of str): Fields to include in the output.

RETURNS

list of dict: Search results.


scoring()

def scoring(query: Union[List[float], np.ndarray, CipherBlock,
                         List[List[float]], List[np.ndarray],
                         List[CipherBlock]],
            search_params: dict = None)

Computes the scores for a query against the index.

ARGUMENTS

  • query (list): Query vector.

Raises:

  • ValueError: If the index is not connected.

RETURNS

list of dict: Scores for the query.


get_topk_metadata_results()

def get_topk_metadata_results(result,
                              top_k: int,
                              output_fields: List[str] = None)

Get top-k metadata results from the search ciphertext result.

ARGUMENTS

  • result (CipherBlock): The result context containing encrypted scores.

  • top_k (int): Number of top results to return.

  • output_fields (list of str): Fields to include in the output.

Raises:

  • ValueError: If the indexer is not connected or if the result is empty.

RETURNS

list of dict: List of dictionaries containing the top-k results with metadata.


decrypt_score()

def decrypt_score(result_ctxt: CipherBlock,
                  sec_key_path: Optional[str] = None,
                  seal_mode: Optional[str] = None,
                  seal_kek_path: Optional[str] = None)

Decrypts the scores from the result context.

ARGUMENTS

  • result_ctxt (CipherBlock): The result context containing encrypted scores.

RETURNS

list of float: Decrypted scores.


load()

def load()

Loads the index into memory.

RETURNS

Index: The index object after loading it.


unload()

def unload()

Unloads the index from memory.

RETURNS

Index: The index object after unloading it.


drop()

def drop()

Drops the index.

RETURNS

Index: The index object after dropping it.


is_connected()

@property
def is_connected() -> bool

Checks if the indexer is connected.

RETURNS

bool: True if the indexer is connected, False otherwise.


is_loaded()

@property
def is_loaded() -> bool

Checks if the index is loaded in memory.

RETURNS

bool: True if the index is loaded, False otherwise.

Last updated