Skip to content

stumct-sa/typesense-dotnet

Repository files navigation

Typesense-dotnet

Unofficial .net client for Typesense HTTP API.

You can get the NuGet package here

NOTE newly created so if you experience any bugs please report them

Progress

  • Create collection
  • Create document
  • Upsert document
  • Search collection
  • Retrieve document
  • Update document
  • Delete document
  • Retrieve collection
  • Export documents
  • Import documents
  • List all collections
  • Drop a collection

Setup

Setup in service collection. The AddTypesenseClient can be found in the Typesense.Setup namespace.

var provider = new ServiceCollection()
    .AddTypesenseClient(config =>
    {
        config.ApiKey = "Hu52dwsas2AdxdE";
        config.Nodes = new List<Node>
        {
            new Node
            {
                Host = "localhost",
                Port = "8108", Protocol = "http"
            }
        };
    }).BuildServiceProvider();

Create collection

var schema = new Schema
{
    Name = "Addresses",
    Fields = new List<Field>
    {
        new Field("id", "int32", false),
        new Field("houseNumber", "int32", false),
        new Field("accessAddress", "string", false),
    },
    DefaultSortingField = "id"
};

var createCollectionResult = await typesenseClient.CreateCollection(schema);

Index document

var address = new Address
{
    Id = 1,
    HouseNumber = 2,
    AccessAddress = "Smedgade 25B"
};

var createDocumentResult = await typesenseClient.CreateDocument<Address>("Addresses", address);

Upsert document

var address = new Address
{
    Id = 1,
    HouseNumber = 2,
    AccessAddress = "Smedgade 25B"
};

var upsertResult = await typesenseClient.UpsertDocument<Address>("Addresses", address);

Search document in collection

var query = new SearchParameters
{
    Text = "Smed",
    QueryBy = "accessAddress"
};

var searchResult = await typesenseClient.Search<Address>("Addresses", query);

Retrieve a document on id

var retrievedDocument = await typesenseClient.RetrieveDocument<Address>("Addresses", "1");

Update document on id

var address = new Address
{
    Id = 1,
    HouseNumber = 2,
    AccessAddress = "Smedgade 25B"
};

var updateDocumentResult = await typesenseClient.UpdateDocument<Address>("Addresses", "1", address);

Delete document on id

var deleteResult = await typesenseClient.DeleteDocument<Address>("Addresses", "1");

Delete documents using filter

var deleteResult = await typesenseClient.DeleteDocuments("Addresses", "houseNumber:>=3", 100);

Drop a collection on name

var deleteCollectionResult = await typesenseClient.DeleteCollection("Addresses");

Import documents

The default batch size is 40. The default ImportType is Create. You can pick between three different import types Create, Upsert, Update. The returned values are a list of ImportResponse that contains a success code, error and the failed document as a string representation.

var importDocumentResults = await typesenseClient.ImportDocuments<Address>("Addresses", addresses, 40, ImportType.Create);

Export documents

var addresses = await typesenseClient.ExportDocuments<Address>("Addresses");

About

.net client for Typesense HTTP API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%