l4u / meilisearch-dotnet

.NET wrapper for the MeiliSearch API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MeiliSearch-Dotnet

MeiliSearch .NET

MeiliSearch | Documentation | Slack | Roadmap | Website | FAQ

NuGet GitHub Workflow Status License Bors enabled

⚑ The MeiliSearch API client written for .NET

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

MeiliSearch is an open-source search engine. Discover what MeiliSearch is!

Table of Contents

πŸ“– Documentation

See our Documentation or our API References.

πŸ”§ Installation

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

dotnet add package MeiliSearch

or with the Package Manager Console:

Install-Package MeiliSearch

Run MeiliSearch

There are many easy ways to download and run a MeiliSearch instance.

For example, if you use Docker:

docker pull getmeili/meilisearch:latest # Fetch the latest version of MeiliSearch image from Docker Hub
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey

NB: you can also download MeiliSearch from Homebrew or APT.

πŸš€ 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 string Genre {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 = await client.Index("movies");
            var documents = new Movie[] {
                new Movie { id = "1", Title = "Carol", Genre = ['Romance', 'Drama']  },
                new Movie { Id = "2", Title = "Wonder Woman", Genre = ['Action', 'Adventure']  },
                new Movie { Id = "3", Title = "Life of Pi", Genre = ['Adventure', 'Drama'] },
                new Movie { Id = "4", Title = "Mad Max: Fury Road", Genre = ['Adventure', 'Science Fiction'] },
                new Movie { Id = "5", Title = "Moana", Genre = ['Fantasy', 'Action']},
                new Movie { Id = "6", Title = "Philadelphia", Genre = ['Drama'] }
            };
            // If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
            var update = await index.AddDocuments<Movie>(documents); # => { "updateId": 0 }
        }
    }
}

With the updateId (via update.UpdateId), you can check the status (enqueued, processing, processed or failed) of your documents addition using the update endpoint.

Basic Search

# MeiliSearch is typo-tolerant:
SearchResult<Movie> movies = await index.Search<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.

SearchResult<Movie> movies = await index.Search<Movie>(
    "car",
    new SearchQuery
    {
        AttributesToHighlight = new string[] { "title" },
    }
);
foreach(var prop in movies.Hits) {
    Console.WriteLine (movies.Title);
}

JSON Output:

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

πŸ€– Compatibility with MeiliSearch

This package only guarantees the compatibility with the version v0.23.0 of MeiliSearch.

🎬 Examples

Indexes

Create an index

var index = client.CreateIndex("movies");

Create an index and give the primary-key

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

List all an index

var indexes = await client.GetAllIndexes();

Get an Index object

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

Documents

Add or Update Documents

 var updateStatus = await index.AddDocuments(new Movie[] { new Movie { Id = "1", Title = "Carol" } } );
 var updateStatus = await index.UpdateDocuments(new Movie[] { new Movie { Id = "1", Title = "Carol" } } );

Update Status has a reference UpdateId to get the status of the action.

Get Documents

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

Get Document by Id

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

Delete documents

 var updateStatus = await index.DeleteOneDocument("11");

Delete in Batch

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

Delete all documents

var updateStatus = await indextoDelete.DeleteAllDocuments();

Update Status

Get Update Status By Id

UpdateStatus individualStatus = await index.GetUpdateStatus(1);

Get All Update Status

var status = await index.GetAllUpdateStatus();

Search

Basic Search

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

Custom Search

var movies = await this.index.Search<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.

βš™οΈ Development Workflow and 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 wrapper for the MeiliSearch API

License:MIT License


Languages

Language:C# 99.7%Language:Shell 0.3%