- Notifications
You must be signed in to change notification settings - Fork70
.NET wrapper for the Meilisearch API
License
meilisearch/meilisearch-dotnet
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
⚡ 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.
- 📖 Documentation
- 🔧 Installation
- 🚀 Getting started
- 🤖 Compatibility with Meilisearch
- 🎬 Examples
- 🧰 Use a Custom HTTP Client
- ⚙️ Contributing
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 ourmain documentation website.
This package targets .NET Standard 2.1.
Using the.NET Core command-line interface (CLI) tools:
dotnet add package MeiliSearch
or with thePackage Manager Console:
Install-Package 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.
usingSystem;usingSystem.Threading.Tasks;usingMeilisearch;namespaceGettingStarted{classProgram{publicclassMovie{publicstringId{get;set;}publicstringTitle{get;set;}publicIEnumerable<string>Genres{get;set;}}staticasyncTaskMain(string[]args){MeilisearchClientclient=newMeilisearchClient("http://localhost:7700","masterKey");// An index is where the documents are stored.varindex=client.Index("movies");vardocuments=newMovie[]{newMovie{Id="1",Title="Carol",Genres=newstring[]{"Romance","Drama"}},newMovie{Id="2",Title="Wonder Woman",Genres=newstring[]{"Action","Adventure"}},newMovie{Id="3",Title="Life of Pi",Genres=newstring[]{"Adventure","Drama"}},newMovie{Id="4",Title="Mad Max: Fury Road",Genres=newstring[]{"Adventure","Science Fiction"}},newMovie{Id="5",Title="Moana",Genres=newstring[]{"Fantasy","Action"}},newMovie{Id="6",Title="Philadelphia",Genres=newstring[]{"Drama"}}};// If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.vartask=awaitindex.AddDocumentsAsync<Movie>(documents);// # => { "uid": 0 }}}}
With theuid, you can check the status (enqueued,canceled,processing,succeeded orfailed) of your documents addition using thetask.
//Meilisearch is typo-tolerant:SearchResult<Movie>movies=awaitindex.SearchAsync<Movie>("philadalphia");foreach(varpropinmovies.Hits){Console.WriteLine(prop.Title);}
JSON Output:
{"hits": [ {"id":6,"title":"Philadelphia", } ],"offset":0,"limit":20,"processingTimeMs":10,"query":"philadalphia"}All the supported options are described in thesearch parameters section of the documentation.
varmovies=awaitindex.SearchAsync<Movie>("car",newSearchQuery{AttributesToHighlight=newstring[]{"title"},});foreach(varpropinmovies.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"}If you want to enable filtering, you must add your attributes to theFilterableAttributes index setting.
TaskInfotask=awaitindex.UpdateFilterableAttributesAsync(newstring[]{"id","genres"});
You only need to perform this operation once.
Note that MeiliSearch will rebuild your index whenever you updateFilterableAttributes. Depending on the size of your dataset, this might take time. You can track the process using theupdate status.
Then, you can perform the search:
varmovies=awaitindex.SearchAsync<Movie>("wonder",newSearchQuery{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"}You can paginate search results by making queries combining bothoffset andlimit.
varresults=awaitindex.SearchAsync<T>(query,newSearchQuery(){Limit=5,Offset=0});if(resultsisSearchResult<T>limitedResults){varestimatedTotalHits=limitedResults.EstimatedTotalHits;}
To get paginated results with page numbers, theHitsPerPage andPage properties must be defined.
varresults=awaitindex.SearchAsync<T>(query,newSearchQuery(){HitsPerPage=pageSize,Page=pageNumber,});if(resultsisPaginatedSearchResult<T>paginatedResults){vartotalHits=paginatedResults.TotalHits;vartotalPages=paginatedResults.TotalPages;}
To get results sorted by attributes and preferred sorting order, theSort property must be defined.
varresults=awaitindex.SearchAsync<T>(query,newSearchQuery(){Sort=newList<string>{"genre:asc","name:desc"}});
This package guarantees compatibility withversion v1.x of Meilisearch, but some features may not be present. Please check theissues for more info.
varindex=awaitclient.CreateIndexAsync("movies");
varindex=awaitclient.CreateIndexAsync("movies","id");
varindexes=awaitclient.GetAllIndexesAsync();
varindex=awaitclient.GetIndexAsync("movies");
vartask=awaitindex.AddDocumentsAsync(newMovie[]{newMovie{Id="1",Title="Carol"}});vartask=awaitindex.UpdateDocumentsAsync(newMovie[]{newMovie{Id="1",Title="Carol"}});
The returnedtask is aTaskInfo that can access toUid to get the status of the task.
vardocuments=awaitindex.GetDocumentsAsync<Movie>(newDocumentsQuery{Limit=1});
vardocument=awaitindex.GetDocumentAsync<Movie>("10");
vartask=awaitindex.DeleteOneDocumentAsync("11");
vartask=awaitindex.DeleteDocumentsAsync(new[]{"12","13","14"});
vartask=awaitindextoDelete.DeleteAllDocumentsAsync();
TaskInfotask=awaitindex.GetTaskAsync(1);// OrTaskInfotask=awaitclient.GetTaskAsync(1);
vartask=awaitindex.GetTasksAsync();// Orvartask=awaitclient.GetTasksAsync();
varmovies=awaitthis.index.SearchAsync<Movie>("prince");
varmovies=awaitthis.index.SearchAsync<Movie>("prince",newSearchQuery{Limit=100});
You can replace the default client used in this package by the one you want.
For example:
var_httpClient=ClientFactory.Instance.CreateClient<MeilisearchClient>();varclient=newMeilisearchClient(_httpClient);
WhereClientFactory is declaredlike this.
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 ourcontributing guidelines for detailed instructions!
Meilisearch provides and maintains manySDKs and Integration tools like this one. We want to provide everyone with anamazing 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 theintegration-guides repository.
About
.NET wrapper for the Meilisearch API
Topics
Resources
License
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.