neosqlite.collection.index_manager module

class neosqlite.collection.index_manager.IndexManager(collection)[source]

Bases: object

Manages indexes for a NeoSQLite collection.

This class provides functionality to create, list, drop, and manage various types of indexes including single-field, compound, unique, sparse, FTS (Full Text Search), and datetime indexes. It handles both regular B-tree indexes using JSON extraction and specialized FTS5 virtual tables for text search capabilities.

__init__(collection)[source]

Initialize the IndexManager with a collection instance.

Parameters:

collection – The NeoSQLite collection instance to manage indexes for

create_index(key: str | list[str] | list[tuple[str, int]], reindex: bool = True, sparse: bool = False, unique: bool = False, fts: bool = False, tokenizer: str | None = None, datetime_field: bool = False)[source]

Create an index on the specified key(s) for this collection.

Handles both single-key and compound indexes by using SQLite’s json_extract function to create indexes on the JSON-stored data. For compound indexes, multiple json_extract calls are used for each key in the list.

Parameters:
  • key – A string or list of strings representing the field(s) to index.

  • reindex – Boolean indicating whether to reindex (not used in this implementation).

  • sparse – Boolean indicating whether the index should be sparse (only include documents with the field).

  • unique – Boolean indicating whether the index should be unique.

  • fts – Boolean indicating whether to create an FTS index for text search.

  • tokenizer – Optional tokenizer to use for FTS index (e.g., ‘icu’, ‘icu_th’).

  • datetime_field – Boolean indicating whether this is a datetime field that requires special indexing.

_create_fts_index(field: str, tokenizer: str | None = None)[source]

Creates an FTS5 index on the specified field for text search.

For FTS indexes, we create both JSON and JSONB versions to ensure compatibility with both types of queries.

For nested array fields (e.g., “comments.text”), we use json_tree() to properly index all array elements, not just the first one.

Parameters:
  • field (str) – The field to create the FTS index on.

  • tokenizer (str, optional) – Optional tokenizer to use for the FTS index.

create_indexes(indexes: list[IndexModel]) list[str][source]

Create multiple indexes at once.

This method accepts a list of IndexModel objects, matching the PyMongo API.

Parameters:

indexes – A list of IndexModel objects.

Returns:

A list of the names of the indexes that were created.

Return type:

list[str]

reindex(table: str, sparse: bool = False, documents: list[dict[str, Any]] | None = None)[source]

Reindex the collection.

With native JSON indexing, reindexing is handled automatically by SQLite. This method is kept for API compatibility but does nothing.

Parameters:
  • table (str) – The table name (not used in this implementation).

  • sparse (bool) – Whether the index should be sparse (not used in this implementation).

  • documents (list[dict[str, Any]]) – List of documents to reindex (not used in this implementation).

list_indexes(as_keys: Literal[True]) list[list[str]][source]
list_indexes(as_keys: Literal[False] = False) list[str]

Retrieve indexes for the collection. Indexes are identified by names following a specific pattern.

Parameters:

as_keys (bool) – If True, return the key names (converted from underscores to dots) instead of the full index names.

Returns:

List of index names or keys, depending on the as_keys parameter.

If as_keys is True, each entry is a list containing a single string (the key name).

Return type:

list[str] or list[list[str]]

drop_index(index: str)[source]

Drop an index from the collection.

Handles both single-key and compound indexes. For compound indexes, the input should be a list of field names. The index name is generated by joining the field names with underscores and replacing dots with underscores.

Parameters:

index (str or list) – The name of the index to drop. If a list is provided, it represents a compound index.

drop_indexes()[source]

Drop all indexes associated with this collection.

This method retrieves the list of indexes using the list_indexes method and drops each one.

index_information() dict[str, Any][source]

Retrieves information on this collection’s indexes.

The function fetches all indexes associated with the collection and extracts relevant details such as whether the index is unique and the keys used in the index. It constructs a dictionary where the keys are the index names and the values are dictionaries containing the index information.

Returns:

A dictionary containing index information.

Return type:

dict

create_search_index(key: str, tokenizer: str | None = None)[source]

Create a search index on the specified key for text search functionality.

This is a convenience method that creates an FTS5 index for efficient text search. It is equivalent to calling create_index(key, fts=True, tokenizer=tokenizer).

Parameters:
  • key – A string representing the field to index for text search.

  • tokenizer – Optional tokenizer to use for the FTS index (e.g., ‘icu’).

Returns:

The result of the index creation operation.

create_search_indexes(indexes: list[str]) list[str][source]

Create multiple search indexes at once for text search functionality.

This is a convenience method that creates multiple FTS5 indexes for efficient text search. It is equivalent to calling create_index(key, fts=True) for each key.

Parameters:

indexes – A list of strings representing the fields to index for text search.

Returns:

A list of the names of the search indexes that were created.

Return type:

list[str]

list_search_indexes() list[str][source]

List all search indexes for the collection.

This method returns a list of all FTS5 search indexes associated with the collection. Note: This implementation scans for FTS virtual tables in the database schema.

Returns:

A list of search index names.

Return type:

list[str]

update_search_index(key: str, tokenizer: str | None = None)[source]

Update a search index by recreating it with potentially new options.

This method drops and recreates a search index, allowing for updates to tokenizer settings or other index options.

Parameters:
  • key – The field name for which to update the search index.

  • tokenizer – Optional new tokenizer to use for the FTS index.

drop_search_index(index: str)[source]

Drop a search index from the collection.

This is a convenience method for dropping FTS5 search indexes.

Parameters:

index (str) – The name of the search index to drop (field name).

_create_datetime_index(key: str, unique: bool = False)[source]

Create a timezone normalized datetime index.

This method creates a specialized datetime index with datetime() for timezone normalization.

Parameters:
  • key – The field name to create the datetime index on (e.g., ‘timestamp’ or ‘user.created_at’)

  • unique – Whether the index should be unique