MemoryStackMemoryStack/Documentation
    Back to Documentation

    Semantic Search

    Search by meaning, not just keywords. Semantic search understands context and finds relevant memories even when exact words don't match.

    The Problem with Traditional Search

    Traditional Keyword Search

    Traditional databases use keyword matching. If the exact word isn't in the text, it won't be found.

    Stored: "User prefers Python"
    Query: "Python" → Found
    Query: "programming language" → Not found
    Query: "coding preferences" → Not found
    Query: "favorite language" → Not found

    Semantic Search

    Semantic search understands meaning. It finds relevant content even when the exact words don't match.

    Stored: "User prefers Python"
    Query: "Python" → Found (exact match)
    Query: "programming language" → Found (semantic match)
    Query: "coding preferences" → Found (semantic match)
    Query: "favorite language" → Found (semantic match)

    How Semantic Search Works

    Semantic search uses embeddings - mathematical representations of meaning. Similar concepts have similar embeddings, even if they use different words.

    1

    Text → Embedding

    When you store a memory, the text is converted into a vector (array of numbers) that represents its meaning.

    Text: "User prefers Python for data science"
    Embedding: [0.12, -0.34, 0.89, 0.23, -0.67, ...] (1536 dimensions)
    2

    Query → Embedding

    Your search query is also converted into an embedding.

    Query: "programming language preferences"
    Embedding: [0.15, -0.31, 0.87, 0.21, -0.65, ...] (1536 dimensions)
    3

    Similarity Matching

    The system finds memories with embeddings similar to your query embedding.

    Similarity Score: 0.92 (very similar!)
    Result: "User prefers Python for data science"

    The Magic of Embeddings

    Embeddings capture semantic relationships. Words like "Python", "programming language", and "coding" have similar embeddings because they're conceptually related. This is why semantic search works - it finds meaning, not just matching text.

    Real-World Examples

    Example 1: Customer Support

    Stored Memories:
    "Customer had trouble logging in due to forgotten password"
    "User couldn't access account after password reset"
    "Authentication failed with incorrect credentials"
    Customer asks:
    "I can't sign in to my account"
    Semantic Search Finds:
    0.94"Customer had trouble logging in due to forgotten password"
    0.91"User couldn't access account after password reset"
    0.88"Authentication failed with incorrect credentials"

    Example 2: Personal Assistant

    Stored Memories:
    "User enjoys Italian cuisine, especially pasta dishes"
    "Prefers restaurants with outdoor seating"
    "Allergic to shellfish"
    User asks:
    "Recommend a place for dinner tonight"
    Semantic Search Finds:
    0.89"User enjoys Italian cuisine, especially pasta dishes"
    0.85"Prefers restaurants with outdoor seating"
    0.82"Allergic to shellfish"

    Using Semantic Search

    Basic Search

    from memorystack import MemoryStackClient
    
    memory = MemoryStackClient(
        api_key="your_api_key",
        user_id="user_123"
    )
    
    # Simple semantic search
    results = memory.search_memories(
        query="programming preferences",
        limit=5
    )
    
    # Process results
    for result in results['results']:
        print(f"Score: {result['relevance_score']}")
        print(f"Content: {result['content']}")
        print("---")

    Advanced Filtering

    # Combine semantic search with filters
    results = memory.search_memories(
        query="authentication issues",
        memory_type="conversation",  # Only conversations
        limit=10,
        metadata_filter={
            "severity": "high",
            "resolved": False
        }
    )
    
    # Get only highly relevant results
    high_confidence = [
        r for r in results['results'] 
        if r['relevance_score'] > 0.85
    ]

    Building Context for AI

    def build_ai_context(user_message: str) -> str:
        """Build rich context from semantic search"""
        
        # Search for relevant memories
        results = memory.search_memories(
            query=user_message,
            limit=10
        )
        
        # Filter by relevance
        relevant = [
            r for r in results['results']
            if r['relevance_score'] > 0.75
        ]
        
        # Build context string
        context_parts = []
        for mem in relevant:
            context_parts.append(
                f"[{mem['memory_type']}] {mem['content']}"
            )
        
        return "\n".join(context_parts)
    
    # Use in AI prompt
    user_msg = "Help me with authentication"
    context = build_ai_context(user_msg)
    
    prompt = f"""
    Context about the user:
    {context}
    
    User's question: {user_msg}
    
    Provide a helpful response based on the context.
    """

    Search Strategies

    Broad Search

    Use general queries to find a wide range of related memories.

    query="user preferences"
    limit=20

    Focused Search

    Use specific queries to find exact information.

    query="Python coding style"
    limit=5

    Multi-Query Search

    Search multiple times with different queries to get comprehensive results.

    queries=["preferences",
      "settings", "config"]

    Contextual Search

    Include context in your query for better results.

    query="authentication issues
      in production environment"

    Best Practices

    ✅ Do

    • • Use natural language queries
    • • Include context in your search terms
    • • Filter by relevance_score (> 0.75 is good)
    • • Combine with memory_type filters
    • • Test different query phrasings

    ❌ Don't

    • • Use single-word queries (too broad)
    • • Expect exact keyword matching
    • • Ignore relevance scores
    • • Search without a clear intent
    • • Over-filter (let semantics work)

    Next Steps