Skip to content

About

.NET client for Meilisearch

Topics

Resources

Contributing

Security policy

Stars

344 stars

Watchers

14 watching

Forks

Repository files navigation

Meilisearch-Dotnet

Meilisearch .NET

NuGet GitHub Workflow Status License

⚡ The Meilisearch API client written for .NET

Meilisearch .NET is the Meilisearch API client for C# developers.

Meilisearch is an open-source search engine. Learn more about Meilisearch.

Table of Contents

📖 Documentation

This readme contains all the documentation you need to start using this Meilisearch SDK.

For general information on how to use Meilisearch—such as our API reference, tutorials, guides, and in-depth articles—refer to our main documentation website.

🔧 Installation

This package targets .NET Standard 2.1.

Using the .NET Core command-line interface (CLI) tools:

dotnet add package MeiliSearch

or with the Package Manager Console:

Install-Package MeiliSearch

Run Meilisearch

⚡️ Launch, scale, and streamline in minutes with Meilisearch Cloud—no maintenance, no commitment, cancel anytime. Try it free now.

🪨 Prefer to self-host? Download and deploy our fast, open-source search engine on your own infrastructure.

🚀 Getting started

Add Documents

using System;
using System.Threading.Tasks;
using Meilisearch;

namespace GettingStarted
{
    class Program
    {
        public class Movie
        {
            public string Id { get; set; }
            public string Title { get; set; }
            public IEnumerable<string> Genres { get; set; }
        }

        static async Task Main(string[] args)
        {
            MeilisearchClient client = new MeilisearchClient("http://localhost:7700", "masterKey");

            // An index is where the documents are stored.
            var index = client.Index("movies");
            var documents = new Movie[] {
                new Movie { Id = "1", Title = "Carol", Genres = new string[] { "Romance", "Drama" }  },
                new Movie { Id = "2", Title = "Wonder Woman", Genres = new string[] { "Action", "Adventure" } },
                new Movie { Id = "3", Title = "Life of Pi", Genres = new string[] { "Adventure", "Drama" } },
                new Movie { Id = "4", Title = "Mad Max: Fury Road", Genres = new string[] { "Adventure", "Science Fiction"} },
                new Movie { Id = "5", Title = "Moana", Genres = new string[] { "Fantasy", "Action" } },
                new Movie { Id = "6", Title = "Philadelphia", Genres = new string[] { "Drama" } }
            };

            // If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.
            var task = await index.AddDocumentsAsync<Movie>(documents); // # => { "uid": 0 }
        }
    }
}

With the uid, you can check the status (enqueued, canceled, processing, succeeded or failed) of your documents addition using the task.

Basic Search

//Meilisearch is typo-tolerant:
SearchResult<Movie> movies = await index.SearchAsync<Movie>("philadalphia");
foreach(var prop in movies.Hits) {
    Console.WriteLine (prop.Title);
}

JSON Output:

{
    "hits": [
        {
            "id": 6,
            "title": "Philadelphia",
        }
    ],
    "offset": 0,
    "limit": 20,
    "processingTimeMs": 10,
    "query": "philadalphia"
}

Custom Search

All the supported options are described in the search parameters section of the documentation.

var movies = await index.SearchAsync<Movie>(
    "car",
    new SearchQuery
    {
        AttributesToHighlight = new string[] { "title" },
    }
);

foreach(var prop in movies.Hits) {
    Console.WriteLine (prop.Title);
}

JSON Output:

{
    "hits": [
        {
            "id": 1,
            "title": "Carol",
            "_formatted": {
                "id": 1,
                "title": "<em>Car</em>ol"
            }
        }
    ],
    "offset": 0,
    "limit": 20,
    "processingTimeMs": 10,
    "query": "car"
}

Custom Search With Filters

If you want to enable filtering, you must add your attributes to the FilterableAttributes index setting.

TaskInfo task = await index.UpdateFilterableAttributesAsync(
    new string[] { "id", "genres" }
);

You only need to perform this operation once.

Note that MeiliSearch will rebuild your index whenever you update FilterableAttributes. Depending on the size of your dataset, this might take time. You can track the process using the update status.

Then, you can perform the search:

var movies = await index.SearchAsync<Movie>(
    "wonder",
    new SearchQuery
    {
        Filter = "id > 1 AND genres = Action",
    }
);

JSON Output:

{
  "hits": [
    {
      "id": 2,
      "title": "Wonder Woman",
      "genres": ["Action","Adventure"]
    }
  ],
  "offset": 0,
  "limit": 20,
  "estimatedTotalHits": 1,
  "processingTimeMs": 0,
  "query": "wonder"
}

Search with Limit and Offset

You can paginate search results by making queries combining both offset and limit.

var results = await index.SearchAsync<T>(query, new SearchQuery()
{
    Limit = 5,
    Offset = 0
});

if (results is SearchResult<T> limitedResults)
{
    var estimatedTotalHits = limitedResults.EstimatedTotalHits;
}

Search with defined number of results per page

To get paginated results with page numbers, the HitsPerPage and Page properties must be defined.

var results = await index.SearchAsync<T>(query, new SearchQuery()
{
    HitsPerPage = pageSize,
    Page = pageNumber,
});

if (results is PaginatedSearchResult<T> paginatedResults)
{
    var totalHits = paginatedResults.TotalHits;
    var totalPages = paginatedResults.TotalPages;
}

Search with sort

To get results sorted by attributes and preferred sorting order, the Sort property must be defined.

var results = await index.SearchAsync<T>(query, new SearchQuery()
{
    Sort = new List<string> { "genre:asc", "name:desc" }
});

🤖 Compatibility with Meilisearch

This package guarantees compatibility with version v1.x of Meilisearch, but some features may not be present. Please check the issues for more info.

🎬 Examples

Indexes

Create an index

var index = await client.CreateIndexAsync("movies");

Create an index and give the primary-key

var index = await client.CreateIndexAsync("movies", "id");

List all an index

var indexes = await client.GetAllIndexesAsync();

Get an Index object

var index = await client.GetIndexAsync("movies");

Documents

Add or Update Documents

var task = await index.AddDocumentsAsync(new Movie[] { new Movie { Id = "1", Title = "Carol" } } );
var task = await index.UpdateDocumentsAsync(new Movie[] { new Movie { Id = "1", Title = "Carol" } } );

The returned task is a TaskInfo that can access to Uid to get the status of the task.

Get Documents

var documents = await index.GetDocumentsAsync<Movie>(new DocumentsQuery { Limit = 1 });

Get Document by Id

var document = await index.GetDocumentAsync<Movie>("10");

Delete documents

var task = await index.DeleteOneDocumentAsync("11");

Delete in Batch

var task = await index.DeleteDocumentsAsync(new [] {"12","13","14"});

Delete all documents

var task = await indextoDelete.DeleteAllDocumentsAsync();

Get Task information

Get one Task By Uid

TaskInfo task = await index.GetTaskAsync(1);
// Or
TaskInfo task = await client.GetTaskAsync(1);

Get All Tasks

var task = await index.GetTasksAsync();
// Or
var task = await client.GetTasksAsync();

Search

Basic Search

var movies = await this.index.SearchAsync<Movie>("prince");

Custom Search

var movies = await this.index.SearchAsync<Movie>("prince", new SearchQuery { Limit = 100 });

🧰 Use a Custom HTTP Client

You can replace the default client used in this package by the one you want.

For example:

var _httpClient = ClientFactory.Instance.CreateClient<MeilisearchClient>();
var client = new MeilisearchClient(_httpClient);

Where ClientFactory is declared like this.

🗜️ Request Compression

The SDK supports automatic HTTP request compression to reduce bandwidth usage and improve performance when sending large document payloads. This is especially beneficial for document upload operations.

Supported Algorithms

  • Gzip (recommended) - Widely supported, good compression ratio, works with .NET Standard 2.0+
  • Deflate - Alternative to Gzip (requires .NET 6.0+ for ZLibStream support)
  • Brotli - Best compression ratio (requires .NET Standard 2.1+ / .NET Core 2.1+)

Note: The package ships a .NET Standard 2.0 build and a .NET 6 build. Gzip is available in both. Deflate and Brotli are included in the .NET 6 build, so applications targeting .NET 6 or later get them automatically. On .NET Standard 2.0, only Gzip is available.

Basic Usage

Enable compression by passing CompressionOptions when creating the client:

using Meilisearch;

// Enable Gzip compression with default settings (1400 byte threshold)
var client = new MeilisearchClient(
    "http://localhost:7700",
    "masterKey",
    CompressionOptions.Gzip());

// All document operations automatically use compression
var index = client.Index("movies");
await index.AddDocumentsAsync(largeDocumentCollection);

Custom Configuration

You can customize compression behavior:

var compressionOptions = new CompressionOptions
{
    Algorithm = CompressionAlgorithm.Gzip,
    MinimumSizeBytes = 1024,  // Only compress payloads >= 1KB
    EnableResponseDecompression = true  // Request compressed responses from server
};

var client = new MeilisearchClient(
    "http://localhost:7700",
    "masterKey",
    compressionOptions);

Note: Request compression and EnableResponseDecompression apply only when using the URL constructor, which creates the HttpClient itself. Passing CompressionOptions to the custom HttpClient constructor stores the options and does not compress requests or decompress responses. With your own HttpClient, add a MeilisearchMessageHandler configured with those options to the handler pipeline, and set HttpClientHandler.AutomaticDecompression for response decompression.

When to Use Compression

Compression is beneficial when:

  • Sending large document collections (hundreds to thousands of documents)
  • Working with documents containing large text fields
  • Operating on limited bandwidth connections
  • Performing batch operations with significant payload sizes

The default threshold of 1400 bytes ensures compression only applies when beneficial, avoiding unnecessary overhead for small payloads.

Performance Notes

  • Compression adds minimal CPU overhead (typically <5ms for MB-sized payloads)
  • Network transfer time reduction often exceeds compression overhead
  • Most effective with text-heavy documents (JSON, CSV, NDJSON)
  • The 1400-byte default threshold aligns with TCP packet sizes for optimal performance

⚙️ Contributing

Any new contribution is more than welcome in this project!

If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!


Meilisearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.

About

.NET client for Meilisearch

Topics

Resources

Contributing

Security policy

Stars

344 stars

Watchers

14 watching

Forks

Releases

Used by

Contributors

Languages