Skip to content

Fix bug on Vector index creation #49

@fleray

Description

@fleray

In https://github.com/couchbase-examples/vector-search-cookbook/blob/71780f4152b79e4bfb95aacd84f6bc856f5ea96e/capella-ai/RAG_with_Couchbase_Capella.ipynb at Creating or Updating Search Indexes section, replace buggy code:

# Create the Vector Index via SDK
try:
    scope_index_manager = (
        cluster.bucket(CB_BUCKET_NAME).scope(SCOPE_NAME).search_indexes()
    )

    # Check if index already exists
    existing_indexes = scope_index_manager.get_all_indexes()
    index_name = index_definition["name"]

    if index_name in [index.name for index in existing_indexes]:
        logging.info(f"Index '{index_name}' found")
    else:
        logging.info(f"Creating new index '{index_name}'...")

    # Create SearchIndex object from JSON definition
    search_index = SearchIndex.from_json(index_definition)

    # Upsert the index (create if not exists, update if exists)
    scope_index_manager.upsert_index(search_index)
    logging.info(f"Index '{index_name}' successfully created/updated.")

except Exception as e:
    logging.error(f"Index exists: {e}")

by something like:

# Create the Vector Index via SDK
try:
    scope_index_manager = (
        cluster.bucket(CB_BUCKET_NAME).scope(SCOPE_NAME).search_indexes()
    )

    # Check if index already exists
    existing_indexes = scope_index_manager.get_all_indexes()
    index_name = index_definition["name"]

    index_name = index_name[index_name.rfind('.')+1:]
    if index_name in [index.name for index in existing_indexes]:
        print(f"Index '{index_name}' found. Let's update it!")
        scope_index_manager.drop_index(index_name)
    else:
        print(f"Creating new index '{index_name}'...")

    # Create SearchIndex object from JSON definition
    search_index = SearchIndex.from_json(index_definition)

    # Upsert the index (create if not exists, update if exists)
    scope_index_manager.upsert_index(search_index)
    print(f"Index '{index_name}' successfully created/updated.")

except Exception as e:
    print(f"Index exists: {e}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions