Skip to main content
This section explains how to manage multiple memory indexes to partition data by context, user group, or application feature and safely perform cross-index operations.

Understanding Indexes

What are indexes? Think of indexes like folders on your computer. Instead of putting all memories in one big pile, you can organize them into categories:
  • Default Index: gravixlayer_memories (general storage)
  • Custom Indexes: user_preferences, work_info, food_preferences, etc.
Why use multiple indexes?
  1. Faster searches - Search only relevant memories
  2. Better organization - Keep different types of data separate
  3. Easier management - Update/delete specific categories
  4. Performance - Smaller indexes = faster operations

Basic Index Operations

  • Python SDK
  • JavaScript SDK
from gravixlayer import GravixLayer

client = GravixLayer()
memory = client.memory

# Default category (gravixlayer_memories)
memory.add("User works as a software engineer", user_id="alice")

# Switch to preferences category
memory.switch_index("user_preferences")
memory.add("User prefers dark mode", user_id="alice")
memory.add("User likes notifications off", user_id="alice")

# Switch to work category  
memory.switch_index("work_info")
memory.add("User uses Python daily", user_id="alice")
memory.add("User works remotely", user_id="alice")

# List all available indexes
indexes = memory.list_available_indexes()
print(f"Available indexes: {indexes}")

# Get current configuration
config = memory.get_current_configuration()
print(f"Current index: {config['index_name']}")
print(f"Current embedding model: {config['embedding_model']}")
Expected Output:
✅ Switched to index: user_preferences
✅ Switched to index: work_info
Available indexes: ['gravixlayer_memories', 'user_preferences', 'work_info']
Current index: work_info
Current embedding model: baai/bge-large-en-v1.5

Index Switching

Switch between different memory indexes dynamically
  • Python SDK
  • JavaScript SDK
import asyncio
import os
from gravixlayer import AsyncGravixLayer
from gravixlayer.resources.memory import Memory

async def main():
    client = AsyncGravixLayer()
    memory = Memory(client)
    
    # Work with default index
    await memory.add("User works as a software engineer", user_id="alice")
    
    # Switch to user preferences index
    memory.switch_configuration(index_name="user_preferences")
    await memory.add("User prefers dark mode", user_id="alice")
    
    # Switch to conversation history index
    memory.switch_configuration(index_name="conversation_history")
    
    # List available indexes
    indexes = await memory.list_available_indexes()
    print(f"Available indexes: {indexes}")
    
    # Get current configuration
    config = memory.get_current_configuration()
    print(f"Current index: {config['index_name']}")
    print(f"Current model: {config['embedding_model']}")
    
    # Reset to defaults
    memory.reset_to_defaults()

asyncio.run(main())
Expected Output:
Switched to database: user_preferences
Configuration updated successfully
Switched to database: conversation_history
Configuration updated successfully
Available indexes: ['gravixlayer_memories', 'sfds', 'user_preferences']
Current index: conversation_history
Current model: baai/bge-large-en-v1.5
Reset to default configuration

Index Structure

Index NamePurposeExample Memories
user_preferencesSettings, UI preferences”Dark mode”, “Language: Spanish”, “Notifications off”
work_infoJob, skills, projects”Software engineer”, “Uses Python”, “Works remotely”
food_preferencesDietary info, likes/dislikes”Vegetarian”, “Allergic to nuts”, “Loves Italian food”
conversation_historyChat logs, interactions”Asked about weather”, “Discussed movies”
personal_infoBasic user data”Lives in NYC”, “Age 25”, “Married”

Targeted Searching

  • Python SDK
  • JavaScript SDK
# Search only in food preferences (faster, more relevant)
memory.switch_index("food_preferences")
food_results = memory.search("dietary restrictions", user_id="alice")
print(f"Food-related memories: {len(food_results['results'])}")

# Search only in work info
memory.switch_index("work_info")
work_results = memory.search("programming skills", user_id="alice")
print(f"Work-related memories: {len(work_results['results'])}")

# Search in all indexes (slower but comprehensive)
memory.switch_index("gravixlayer_memories")  # Default index
all_results = memory.search("user information", user_id="alice")
print(f"All memories: {len(all_results['results'])}")

Cross-Index Operations

  • Python SDK
  • JavaScript SDK
# Add to multiple indexes in sequence
memory.switch_index("food_preferences")
memory.add("User likes spicy food", user_id="alice")

memory.switch_index("health_info")
memory.add("User has high blood pressure", user_id="alice")

memory.switch_index("work_info")
memory.add("User works night shifts", user_id="alice")

# Search across different indexes for comprehensive results
food_memories = memory.search("preferences", user_id="alice")
memory.switch_index("health_info")
health_memories = memory.search("medical", user_id="alice")

print(f"Food preferences: {len(food_memories['results'])}")
print(f"Health info: {len(health_memories['results'])}")
Expected Output:
✅ Switched to index: food_preferences
✅ Switched to index: health_info  
✅ Switched to index: work_info
✅ Switched to index: health_info
Food preferences: 1
Health info: 1

Cross-Index Operations

Work with multiple indexes in the same application:
  • Python SDK
  • JavaScript SDK
import asyncio
import os
from gravixlayer import AsyncGravixLayer
from gravixlayer.resources.memory import Memory

async def main():
    client = AsyncGravixLayer()
    memory = Memory(client)
    
    # Add to different indexes
    await memory.add("User likes pizza", user_id="alice", index_name="food_preferences")
    await memory.add("User prefers React", user_id="alice", index_name="tech_preferences")
    await memory.add("Meeting at 3pm", user_id="alice", index_name="calendar_events")
    
    # Search across specific indexes
    food_results = await memory.search("food", user_id="alice", index_name="food_preferences")
    tech_results = await memory.search("framework", user_id="alice", index_name="tech_preferences")
    
    print(f"Food preferences: {len(food_results['results'])} results")
    print(f"Tech preferences: {len(tech_results['results'])} results")

asyncio.run(main())

Best Practices

  1. Use descriptive index names - user_preferences not prefs
  2. Keep indexes focused - Don’t mix unrelated data types
  3. Start simple - Begin with 2-3 indexes, add more as needed
  4. Search specific indexes - Faster than searching everything
  5. Document your structure - Keep track of what goes where
I