Go to file
2025-03-13 08:37:33 +01:00
.github docs: fix links 2025-03-11 16:52:21 +01:00
.husky chore: add husky 2025-03-11 16:03:04 +01:00
pkg refactor: log messages 2025-03-13 08:37:33 +01:00
.editorconfig feat: initial boostrapping of api structure 2025-02-05 16:46:14 +01:00
.gitignore chore: update gitignore 2025-03-11 16:03:14 +01:00
.golangci.yml chore: update golangci lint config 2025-03-11 16:03:20 +01:00
.goreleaser.yml chore: update goreleaser config 2025-03-11 16:03:27 +01:00
.husky.yaml chore: add husky 2025-03-11 16:03:04 +01:00
go.mod feat: go 1.24.1 2025-03-11 16:03:34 +01:00
go.sum feat: bootstrap application lifecycle 2025-02-07 13:26:09 +01:00
LICENSE Initial commit 2025-02-05 08:35:50 +01:00
Makefile feat: initial boostrapping of api structure 2025-02-05 16:46:14 +01:00
README.md docs: update readme 2025-03-12 11:58:26 +01:00

Typesense API

Build Status Go Report Card GoDoc

Overview

This package provides an API for managing and searching Typesense collections. It offers functionalities for initializing, indexing, searching, and maintaining Typesense collections and aliases.

Features

  • Initialization: Ensures that all aliases point to the latest revision-based collections.
  • Health Check: Verifies if the Typesense client is operational.
  • Index Management: Lists, creates, and updates index collections.
  • Document Upsertion: Bulk upsert support for indexing documents.
  • Search Operations: Provides simple and advanced search capabilities.
  • Revision Management: Supports committing and reverting indexing revisions.

Installation

To use this package, add it as a dependency in your Go project:

 go get github.com/foomo/typesense

Usage example

import (
	"context"
	"time"

	"github.com/foomo/contentserver/client"
	"github.com/foomo/keel/config"
	"github.com/foomo/keel/log"
	typesenseapi "github.com/foomo/typesense/pkg/api"
	typesenseindexing "github.com/foomo/typesense/pkg/indexing"

	"github.com/typesense/typesense-go/v3/typesense"
)

func main() {
	ctx := context.Background()
	l := log.Logger()

	// contentserver client
	csClient, errClient := client.NewHTTPClient("contentserver_url")
	log.Must(l, errClient, "could not get contentserver client")

	// contentful client
	// provide list of content types
	cfClients := contentful.NewDefaultContentfulClients(ctx, l, contentful_types, true)
	cfClients.UpdateCache()
	cfClients.Client.ClientStats()

	// create typesense client
	typesenseClient := typesense.NewClient(
		typesense.WithConnectionTimeout(2*time.Minute),
		typesense.WithServer("typesense_server"),
		typesense.WithAPIKey("typesense_api_key"),
	)

	// configure document provider
	documentProvider := typesenseindexing.NewContentServer(
		l, csClient,
		GetDocumentProviderFunctions(...),  // retrieve document provider functions
		supportedMimeTypes,                 // provide supported mime types
	)

	// create typesense api
	// create indexDocument and returnType
	api := typesenseapi.NewBaseAPI[indexDocument, returnType](
		l,
		typesenseClient,
		collectionSchemas,  //map[IndexID]*api.CollectionSchema
		presetUpsertSchema, //*api.PresetUpsertSchema
	)

	// create typesense indexer
	indexer := typesenseindexing.NewBaseIndexer(
		l,
		api,
		documentProvider,
	)

	// run indexer
	err := indexer.Run(ctx)
	log.Must(l, err, "could not run indexer")
}

Health Check

err := apiInstance.Healthz(context.Background())
if err != nil {
	log.Fatalf("Health check failed: %v", err)
}

Searching Documents

results, scores, total, err := apiInstance.SimpleSearch(context.Background(), "products", "laptop", nil, 1, 10, "price:desc")
if err != nil {
	log.Fatalf("Search failed: %v", err)
}
log.Printf("Found %d results", total)
searchParams := &api.SearchCollectionParams{
	Q:       pointer.String("laptop"),
	SortBy:  pointer.String("price:desc"),
}

results, scores, total, err = apiInstance.ExpertSearch(context.Background(), "products", searchParams)
if err != nil {
	log.Fatalf("Advanced search failed: %v", err)
}
log.Printf("Found %d results", total)

How to Contribute

Please refer to the CONTRIBUTING details and follow the CODE_OF_CONDUCT and SECURITY guidelines.

License

Distributed under MIT License, please see license file within the code for more details.

Made with ♥ foomo by bestbytes