Search Functionality

WharfDocs includes a powerful built-in search feature that helps users quickly find the information they need.

#How Search Works

The search system automatically:

  1. Indexes all documentation on page load
  2. Searches through titles, headings, and content
  3. Ranks results by relevance
  4. Highlights matching terms in search results

#Search Indexing

When the documentation engine initializes, it:

  • Scans all Markdown files in the docs/ directory
  • Extracts titles, headings, and content
  • Creates a searchable index in memory
  • Removes Markdown syntax for clean text search

#Search Algorithm

The search algorithm prioritizes results based on:

  1. Title matches (highest priority - 100 points)
  2. Heading matches (medium priority - 50 points per match)
  3. Content matches (10 points per occurrence)

Results are sorted by score and limited to the top 10 matches.

#From the UI

  1. Click the search box in the header
  2. Type your query (minimum 2 characters)
  3. Results appear in real-time as you type
  4. Click any result to navigate to that page

#Search Tips

  • Be specific: More specific queries return better results
  • Use keywords: Search for important terms from your topic
  • Try variations: If you don't find what you need, try different words
  • Check spelling: Typos won't match content

#API Endpoint

Search is also available via API:

GET /api/search?q=your+query

Response format:

[
  {
    "path": "getting-started/introduction",
    "title": "Introduction",
    "excerpt": "Welcome to <mark>WharfDocs</mark>...",
    "score": 150
  }
]

You can customize search behavior by modifying src/SearchIndexer.php:

// Change result limit
return array_slice($results, 0, 20); // Show 20 results

// Adjust scoring weights
if (stripos($titleLower, $query) !== false) {
    $score += 200; // Increase title match weight
}

#Performance Considerations

  • Index is built on initialization: First page load may be slightly slower
  • Search is performed in-memory: Very fast for small to medium documentation sites
  • No database required: Keeps the system simple and portable

#Future Enhancements

Potential improvements for search:

  • Fuzzy matching for typo tolerance
  • Search suggestions and autocomplete
  • Filter by section or category
  • Search history
  • Advanced search operators