# MeiliSearch .NET Embedded

![Gitea Release](https://img.shields.io/gitea/v/release/damien/meilisearch.NET?gitea_url=https://git.d4m13n.dev)
![Dotnet 8](https://img.shields.io/badge/-.NET%208.0-blueviolet?logo=dotnet)
![Meilisearch](https://img.shields.io/badge/Meilisearch-FA8072)
![Ollama](https://img.shields.io/badge/Ollama-Powered-orange)
[![Gitea](https://img.shields.io/badge/Gitea-6eaa5b?logo=gitea&logoColor=fff)](#)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Discord](https://img.shields.io/discord/1195961063183765575?label=chat&logo=discord)](https://discord.gg/8dHnaarghJ)

## Overview

MeiliSearch .NET Embedded is a powerful NuGet package that seamlessly integrates MeiliSearch into your .NET applications. It provides a robust wrapper around MeiliSearch, handling process management, health monitoring, and advanced features like index compression - all while maintaining compatibility with the native MeiliSearch SDK.

## Key Features

- **Embedded MeiliSearch Engine**: Run MeiliSearch directly within your application
- **Automatic Process Management**: Handles startup, shutdown, and health monitoring
- **Smart Index Management**: 
  - Create and manage indexes with type safety
  - Enable/disable indexes on demand
  - Automatic compression for optimized storage
- **Efficient Document Management**:
  - Batch processing system with configurable thresholds
  - Automatic validation of index availability
- **Resource Monitoring**:
  - Track memory and CPU usage
  - Monitor storage utilization
  - Index-specific metrics
- **Native SDK Compatibility**: Full support for the official MeiliSearch SDK

## Installation

### Via Package Manager Console
```bash
Install-Package meilisearch.NET
```

### Via .NET CLI
```bash
dotnet add package meilisearch.NET
```

## Quick Start

### 1. Basic Setup
Add MeiliSearch service to your dependency injection container:

```csharp
var builder = Host.CreateApplicationBuilder();
builder.Services.AddMeiliSearchService();
```

### 2. Configuration
Configure MeiliSearch in your `appsettings.json`:

```json
{
  "MeiliSearch": {
    "Port": 7700,
    "UiEnabled": true
  }
}
```

### 3. Basic Usage

#### Define Your Document Model
```csharp
public class Product : IDocument
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
}
```

#### Create and Manage Indexes
```csharp
public class SearchService
{
    private readonly MeiliSearchService _searchService;

    public SearchService(MeiliSearchService searchService)
    {
        _searchService = searchService;
    }

    public async Task InitializeProductIndex()
    {
        // Create index
        await _searchService.CreateIndex<Product>("products");

        // Add documents
        var product = new Product 
        {
            Id = "1",
            Name = "Gaming Laptop",
            Description = "High-performance gaming laptop",
            Price = 1299.99m
        };
        
        _searchService.AddDocument("products", product);
        
        //_searchService.AddDocument("products", product, true); if you set the third parameter, which is autocommit, to true, it will ignore batching.
    }
}
```
### 4. Use MeiliSearch SDK 
### Using Native MeiliSearch SDK
```csharp
await _searchService.SDK("products", async client => 
{
    var index = await client.GetIndex("products");
    var searchResults = await index.SearchAsync<Product>("laptop");
    return searchResults;
});
```

## Advanced Usage

### Resource Monitoring
```csharp
var usage = _searchService.GetResourceUsage();
Console.WriteLine($"Memory Usage: {usage.MemoryUsageBytes} bytes");
Console.WriteLine($"CPU Usage: {usage.CpuPercentage}%");
Console.WriteLine($"Storage Usage: {_searchService.GetTotalStorageUsage()} bytes");
```

### Index Management
```csharp
// Disable an index (automatically compresses)
await _searchService.SetIndexEnabled("products", false);

// Enable an index (automatically decompresses)
await _searchService.SetIndexEnabled("products", true);

// Get all indexes
var indexes = await _searchService.GetAllIndexes();
```

## Best Practices

1. **Resource Management**
   - Always dispose of the `MeiliSearchService` when your application shuts down
   - Monitor resource usage in production environments

2. **Index Management**
   - Disable unused indexes to save resources
   - Use type-safe index creation with generic parameters

3. **Document Management**
   - Utilize batch processing for bulk operations
   - Handle exceptions when adding documents

## Performance Considerations

- The batch system automatically manages document additions with a default threshold of 100 documents
- Compressed indexes use less storage but require decompression before use
- Monitor resource usage in production environments

## Contributing

We welcome contributions! Please follow these steps:

1. Fork the repository
2. Create a feature branch
3. Commit your changes
4. Push to the branch
5. Create a Pull Request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Support

- Create an issue on GitHub
- Contact us on discord [![Discord](https://img.shields.io/discord/1195961063183765575?label=chat&logo=discord)](https://discord.gg/8dHnaarghJ)
- Visit our documentation at meilisearchdotnet.d4m13n.dev

## Acknowledgments

- Built on top of the excellent [MeiliSearch](https://www.meilisearch.com/) search engine
- Powered by [Ollama](https://ollama.ai/) for AI capabilities