Multi-Index Operations
- Python SDK
- JavaScript SDK
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
memory = client.memory
# Add to multiple indexes
memory.add("User likes pizza", user_id="alice", index_name="food_preferences")
memory.add("User prefers React", user_id="alice", index_name="tech_preferences")
memory.add("Meeting at 3pm", user_id="alice", index_name="calendar_events")
# Search across specific indexes
food_results = memory.search("food", user_id="alice", index_name="food_preferences")
tech_results = 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")
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const memory = client.memory;
// Add to multiple indexes
await memory.add("User likes pizza", "alice", { indexName: "food_preferences" });
await memory.add("User prefers React", "alice", { indexName: "tech_preferences" });
await memory.add("Meeting at 3pm", "alice", { indexName: "calendar_events" });
// Search across specific indexes
const foodResults = await memory.search("food", "alice", { indexName: "food_preferences" });
const techResults = await memory.search("framework", "alice", { indexName: "tech_preferences" });
console.log(`Food preferences: ${foodResults.results.length} results`);
console.log(`Tech preferences: ${techResults.results.length} results`);
Coordinated Search Strategy
Search across multiple indexes to build comprehensive user profiles:- Python SDK
- JavaScript SDK
Copy
def get_user_profile(user_id):
"""Get comprehensive user profile from multiple indexes"""
# Search different aspects of user
work_info = memory.search("job skills experience", user_id=user_id, index_name="work_info")
preferences = memory.search("likes dislikes settings", user_id=user_id, index_name="user_preferences")
personal = memory.search("location age family", user_id=user_id, index_name="personal_info")
profile = {
"work": work_info['results'][:3], # Top 3 work-related memories
"preferences": preferences['results'][:3],
"personal": personal['results'][:3]
}
return profile
# Usage
user_profile = get_user_profile("alice")
print(f"Work info: {len(user_profile['work'])} memories")
print(f"Preferences: {len(user_profile['preferences'])} memories")
print(f"Personal: {len(user_profile['personal'])} memories")
Copy
async function getUserProfile(userId) {
// Search different aspects of user
const workInfo = await memory.search("job skills experience", userId, { indexName: "work_info" });
const preferences = await memory.search("likes dislikes settings", userId, { indexName: "user_preferences" });
const personal = await memory.search("location age family", userId, { indexName: "personal_info" });
return {
work: workInfo.results.slice(0, 3), // Top 3 work-related memories
preferences: preferences.results.slice(0, 3),
personal: personal.results.slice(0, 3)
};
}
// Usage
const userProfile = await getUserProfile("alice");
console.log(`Work info: ${userProfile.work.length} memories`);
console.log(`Preferences: ${userProfile.preferences.length} memories`);
console.log(`Personal: ${userProfile.personal.length} memories`);
Bulk Operations Across Indexes
- Python SDK
- JavaScript SDK
Copy
def migrate_user_data(user_id, from_index, to_index):
"""Migrate all memories from one index to another"""
# Get all memories from source index
memories = memory.get_all(user_id=user_id, index_name=from_index)
# Add to destination index
for mem in memories['results']:
memory.add(mem['memory'], user_id=user_id, index_name=to_index,
metadata=mem.get('metadata', {}))
print(f"Migrated {len(memories['results'])} memories from {from_index} to {to_index}")
def cleanup_old_memories(user_id, index_name, days_old=30):
"""Remove old memories from specific index"""
memories = memory.get_all(user_id=user_id, index_name=index_name)
# Filter and delete old memories (implementation depends on metadata structure)
for mem in memories['results']:
# Delete based on your criteria
memory.delete(mem['id'], user_id=user_id, index_name=index_name)
# Usage examples
migrate_user_data("alice", "temp_storage", "permanent_storage")
cleanup_old_memories("alice", "conversation_history", days_old=7)
Copy
async function migrateUserData(userId, fromIndex, toIndex) {
// Get all memories from source index
const memories = await memory.getAll(userId, { indexName: fromIndex });
// Add to destination index
for (const mem of memories.results) {
await memory.add(mem.memory, userId, {
indexName: toIndex,
metadata: mem.metadata || {}
});
}
console.log(`Migrated ${memories.results.length} memories from ${fromIndex} to ${toIndex}`);
}
async function cleanupOldMemories(userId, indexName, daysOld = 30) {
const memories = await memory.getAll(userId, { indexName });
// Filter and delete old memories (implementation depends on metadata structure)
for (const mem of memories.results) {
// Delete based on your criteria
await memory.delete(mem.id, userId, { indexName });
}
}
// Usage examples
await migrateUserData("alice", "temp_storage", "permanent_storage");
await cleanupOldMemories("alice", "conversation_history", 7);
Index Synchronization
Keep related data synchronized across different indexes:- Python SDK
- JavaScript SDK
Copy
def sync_user_preferences(user_id):
"""Sync user preferences across different contexts"""
# Get core preferences
core_prefs = memory.search("theme language timezone", user_id=user_id, index_name="user_preferences")
# Apply to different application contexts
for pref in core_prefs['results']:
if "theme" in pref['memory'].lower():
memory.add(f"UI: {pref['memory']}", user_id=user_id, index_name="ui_settings")
elif "language" in pref['memory'].lower():
memory.add(f"Locale: {pref['memory']}", user_id=user_id, index_name="localization")
def update_across_indexes(user_id, old_info, new_info):
"""Update information across all relevant indexes"""
indexes_to_update = ["personal_info", "work_info", "user_preferences"]
for index in indexes_to_update:
# Search for old information
results = memory.search(old_info, user_id=user_id, index_name=index)
# Update if found
for result in results['results']:
memory.update(result['id'], new_info, user_id=user_id, index_name=index)
# Usage
sync_user_preferences("alice")
update_across_indexes("alice", "lives in NYC", "lives in San Francisco")
Copy
async function syncUserPreferences(userId) {
// Get core preferences
const corePrefs = await memory.search("theme language timezone", userId, { indexName: "user_preferences" });
// Apply to different application contexts
for (const pref of corePrefs.results) {
if (pref.memory.toLowerCase().includes("theme")) {
await memory.add(`UI: ${pref.memory}`, userId, { indexName: "ui_settings" });
} else if (pref.memory.toLowerCase().includes("language")) {
await memory.add(`Locale: ${pref.memory}`, userId, { indexName: "localization" });
}
}
}
async function updateAcrossIndexes(userId, oldInfo, newInfo) {
const indexesToUpdate = ["personal_info", "work_info", "user_preferences"];
for (const index of indexesToUpdate) {
// Search for old information
const results = await memory.search(oldInfo, userId, { indexName: index });
// Update if found
for (const result of results.results) {
await memory.update(result.id, newInfo, userId, { indexName: index });
}
}
}
// Usage
await syncUserPreferences("alice");
await updateAcrossIndexes("alice", "lives in NYC", "lives in San Francisco");

