# MeiliSearch Setup

## Overview

MeiliSearch is configured for fast, typo-tolerant search across tutorials, parts, and pitfalls.

## Installation (macOS)

MeiliSearch is installed at: `~/meilisearch/meilisearch`

Data directory: `~/meilisearch/data`

## Auto-Start Configuration

LaunchAgent plist: `~/Library/LaunchAgents/com.meilisearch.plist`

The service starts automatically on boot and keeps running.

### Managing the Service

```bash
# Stop MeiliSearch
launchctl unload ~/Library/LaunchAgents/com.meilisearch.plist

# Start MeiliSearch
launchctl load ~/Library/LaunchAgents/com.meilisearch.plist

# Check status
launchctl list | grep meilisearch

# View logs
tail -f ~/meilisearch/meilisearch.log
tail -f ~/meilisearch/meilisearch.error.log
```

## Configuration

Environment variables in `Backend/.env`:

```env
MEILI_HOST=http://127.0.0.1:7700
MEILI_MASTER_KEY=<your-master-key>
```

## Indexes

Three indexes are pre-configured:

- `tutorials` - iFixit repair guides and tutorials
- `parts` - Compatible parts and components  
- `pitfalls` - Common repair pitfalls and gotchas

## Usage in Backend

```javascript
import client, { INDEXES, testConnection, getIndex } from './config/meilisearch.js';

const isHealthy = await testConnection();

const tutorialsIndex = getIndex(INDEXES.TUTORIALS);
await tutorialsIndex.addDocuments([
  { id: 1, title: 'MacBook battery replacement', content: '...' }
]);

const results = await tutorialsIndex.search('macbok batery');
```

## Verification

```bash
# Check health
curl -s http://localhost:7700/health | jq '.status'

# List indexes
curl -s http://localhost:7700/indexes \
  -H "Authorization: Bearer $MEILI_MASTER_KEY" | jq '.[].uid'

# Test from Backend
cd Backend
node -e "import('dotenv/config'); import('./config/meilisearch.js').then(async (m) => { \
  const healthy = await m.testConnection(); \
  const indexes = await m.getIndexes(); \
  console.log('Health:', healthy); \
  console.log('Indexes:', indexes.map(i => i.uid).join(', ')); \
  process.exit(0); \
})"
```

## Security

- MeiliSearch is bound to `127.0.0.1:7700` (localhost only)
- Master key is required for all operations
- Never expose MeiliSearch to public internet

## Resources

- [MeiliSearch Documentation](https://www.meilisearch.com/docs)
- [Node.js Client](https://github.com/meilisearch/meilisearch-js)
