# AI Search Implementation Guide

This guide explains how to implement the AI-powered search feature in your Homnex project.

## Overview

The AI search algorithm is designed to:

1. Learn from user interactions
2. Personalize results based on user preferences
3. Consider global trends and popularity
4. Provide similarity-based recommendations
5. Work with your existing UI without modifications

## Files Created

1. `includes/AISearchEngine.php` - The core AI search engine class
2. `includes/ai_search.php` - A simple wrapper with helper functions
3. `examples/search_with_ai.php` - An example implementation

## How to Implement

### Step 1: Include the AI Search File

At the top of your search.php or home.php file, add:

```php
<?php
// Include AI Search functionality
require_once 'includes/ai_search.php';
?>
```

### Step 2: Replace Your Search Results Query

Find where you're currently querying for search results and replace it with:

```php
// Get AI-enhanced search results
$searchResults = getAISearchResults();
$rooms = $searchResults['rooms'];
$totalRooms = $searchResults['total'];
```

That's it! The AI search will now handle all the filtering, sorting, and personalization.

### Step 3: Track Room Views (Optional but Recommended)

In your room.php file (or wherever you display room details), add:

```php
<?php
// Include AI Search functionality if not already included
require_once 'includes/ai_search.php';

// Track this room view for learning
$roomId = $_GET['id'] ?? 0;
if ($roomId) {
    trackAIRoomView($roomId);
}
?>
```

### Step 4: Show Similar Rooms (Optional)

To display similar rooms on the room details page:

```php
<?php
// Get similar rooms
$similarRooms = getAISimilarRooms($roomId);

// Display similar rooms
if (!empty($similarRooms)) {
    foreach ($similarRooms as $room) {
        // Display room card
    }
}
?>
```

### Step 5: Show Recommended Rooms (Optional)

To display recommended rooms on the home page:

```php
<?php
// Get recommended rooms
$recommendedRooms = getAIRecommendedRooms(6); // Get 6 recommendations

// Display recommended rooms
if (!empty($recommendedRooms)) {
    foreach ($recommendedRooms as $room) {
        // Display room card
    }
}
?>
```

## How It Works

1. The AI search engine creates necessary database tables automatically
2. It tracks user searches and interactions
3. It learns from user behavior to improve results
4. It considers room popularity, user preferences, and global trends
5. It personalizes results for logged-in users

## Advanced Customization

If you need to customize the AI search behavior, you can modify:

- `includes/AISearchEngine.php` - The core search algorithm
- `includes/ai_search.php` - The helper functions

## Example Implementation

See `examples/search_with_ai.php` for a complete example of how to implement the AI search in your search page.

## Troubleshooting

If you encounter any issues:

1. Check that the database connection is working
2. Ensure the necessary tables were created (they should be created automatically)
3. Check for PHP errors in your server logs
4. Make sure you're including the ai_search.php file correctly