• Stars
    star
    124
  • Rank 278,542 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go client for Typesense: https://github.com/typesense/typesense

typesense-go

Build Status GoReportCard Status Go Reference GitHub release Gitter

Go client for the Typesense API: https://github.com/typesense/typesense

Installation

go get github.com/typesense/typesense-go

Usage

Import the the package into your code :

import "github.com/typesense/typesense-go/typesense"

Create new client:

client := typesense.NewClient(
	    typesense.WithServer("http://localhost:8108"),
	    typesense.WithAPIKey("<API_KEY>"))

New client with advanced configuration options (see godoc):

client := typesense.NewClient(
		typesense.WithServer("http://localhost:8108"),
		typesense.WithAPIKey("<API_KEY>"),
		typesense.WithConnectionTimeout(5*time.Second),
		typesense.WithCircuitBreakerMaxRequests(50),
		typesense.WithCircuitBreakerInterval(2*time.Minute),
		typesense.WithCircuitBreakerTimeout(1*time.Minute),
	)

You can also find some examples in integration tests.

Create a collection

	schema := &api.CollectionSchema{
		Name: "companies",
		Fields: []api.Field{
			{
				Name: "company_name",
				Type: "string",
			},
			{
				Name: "num_employees",
				Type: "int32",
			},
			{
				Name:  "country",
				Type:  "string",
				Facet: true,
			},
		},
		DefaultSortingField: "num_employees",
	}

	client.Collections().Create(schema)

Index a document

	document := struct {
		ID           string `json:"id"`
		CompanyName  string `json:"company_name"`
		NumEmployees int    `json:"num_employees"`
		Country      string `json:"country"`
	}{
		ID:           "123",
		CompanyName:  "Stark Industries",
		NumEmployees: 5215,
		Country:      "USA",
	}

	client.Collection("companies").Documents().Create(document)

Upserting a document

	document := struct {
		ID           string `json:"id"`
		CompanyName  string `json:"company_name"`
		NumEmployees int    `json:"num_employees"`
		Country      string `json:"country"`
	}{
		ID:           "123",
		CompanyName:  "Stark Industries",
		NumEmployees: 5215,
		Country:      "USA",
	}

	client.Collection("companies").Documents().Upsert(newDocument)

Search a collection

	searchParameters := &api.SearchCollectionParams{
		Q:        "stark",
		QueryBy:  "company_name",
		FilterBy: pointer.String("num_employees:>100"),
		SortBy:   &([]string{"num_employees:desc"}),
	}

	client.Collection("companies").Documents().Search(searchParameters)

for the supporting multiple QueryBy params, you can add , after each field

	searchParameters := &api.SearchCollectionParams{
		Q:        "stark",
		QueryBy:  "company_name, country",
		FilterBy: pointer.String("num_employees:>100"),
		SortBy:   &([]string{"num_employees:desc"}),
	}

	client.Collection("companies").Documents().Search(searchParameters)

Retrieve a document

client.Collection("companies").Document("123").Retrieve()

Update a document

	document := struct {
		CompanyName  string `json:"company_name"`
		NumEmployees int    `json:"num_employees"`
	}{
		CompanyName:  "Stark Industries",
		NumEmployees: 5500,
	}

	client.Collection("companies").Document("123").Update(document)

Delete an individual document

client.Collection("companies").Document("123").Delete()

Delete a bunch of documents

filter := &api.DeleteDocumentsParams{FilterBy: "num_employees:>100", BatchSize: 100}
client.Collection("companies").Documents().Delete(filter)

Retrieve a collection

client.Collection("companies").Retrieve()

Export documents from a collection

client.Collection("companies").Documents().Export()

Import documents into a collection

The documents to be imported can be either an array of document objects or be formatted as a newline delimited JSON string (see JSONL).

Import an array of documents:

	documents := []interface{}{
		struct {
			ID           string `json:"id"`
			CompanyName  string `json:"companyName"`
			NumEmployees int    `json:"numEmployees"`
			Country      string `json:"country"`
		}{
			ID:           "123",
			CompanyName:  "Stark Industries",
			NumEmployees: 5215,
			Country:      "USA",
		},
	}
	params := &api.ImportDocumentsParams{
		Action:    "create",
		BatchSize: 40,
	}

	client.Collection("companies").Documents().Import(documents, params)

Import a JSONL file:

	params := &api.ImportDocumentsParams{
		Action:    "create",
		BatchSize: 40,
	}
	importBody, err := os.Open("documents.jsonl")
	// defer close, error handling ...

	client.Collection("companies").Documents().ImportJsonl(importBody, params)

List all collections

client.Collections().Retrieve()

Drop a collection

client.Collection("companies").Delete()

Create an API Key

	keySchema := &api.ApiKeySchema{
		Description: "Search-only key.",
		Actions:     []string{"documents:search"},
		Collections: []string{"companies"},
		ExpiresAt:   time.Now().AddDate(0, 6, 0).Unix(),
	}

	client.Keys().Create(keySchema)

Retrieve an API Key

client.Key(1).Retrieve()

List all keys

client.Keys().Retrieve()

Delete API Key

client.Key(1).Delete()

Create or update an override

	override := &api.SearchOverrideSchema{
		Rule: api.SearchOverrideRule{
			Query: "apple",
			Match: "exact",
		},
		Includes: []api.SearchOverrideInclude{
			{
				Id:       "422",
				Position: 1,
			},
			{
				Id:       "54",
				Position: 2,
			},
		},
		Excludes: []api.SearchOverrideExclude{
			{
				Id: "287",
			},
		},
	}

	client.Collection("companies").Overrides().Upsert("customize-apple", override)

List all overrides

client.Collection("companies").Overrides().Retrieve()

Delete an override

client.Collection("companies").Override("customize-apple").Delete()

Create or Update an alias

	body := &api.CollectionAliasSchema{CollectionName: "companies_june11"}
	client.Aliases().Upsert("companies", body)

Retrieve an alias

client.Alias("companies").Retrieve()

List all aliases

client.Aliases().Retrieve()

Delete an alias

client.Alias("companies").Delete()

Create or update a multi-way synonym

	synonym := &api.SearchSynonymSchema{
		Synonyms: []string{"blazer", "coat", "jacket"},
	}
	client.Collection("products").Synonyms().Upsert("coat-synonyms", synonym)

Create or update a one-way synonym

	synonym := &api.SearchSynonymSchema{
		Root:     "blazer",
		Synonyms: []string{"blazer", "coat", "jacket"},
	}
	client.Collection("products").Synonyms().Upsert("coat-synonyms", synonym)

Retrieve a synonym

client.Collection("products").Synonym("coat-synonyms").Retrieve()

List all synonyms

client.Collection("products").Synonyms().Retrieve()

Delete a synonym

client.Collection("products").Synonym("coat-synonyms").Delete()

Create snapshot (for backups)

client.Operations().Snapshot("/tmp/typesense-data-snapshot")

Re-elect Leader

client.Operations().Vote()

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/typesense/typesense-go.

Development Workflow Setup

Install dependencies,

go mod download

Update the generated files,

go generate

NOTE: Make sure you have GOBIN in environment variables

License

typesense-go is distributed under the Apache 2 license.

More Repositories

1

typesense

Open Source alternative to Algolia and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences
C++
12,770
star
2

showcase-recipe-search

Instantly search 2M cooking recipes using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚡ 🥘 🔍
JavaScript
437
star
3

typesense-js

JavaScript / TypeScript client for Typesense
JavaScript
266
star
4

typesense-instantsearch-adapter

A JS adapter library to build rich search interfaces with Typesense and InstantSearch.js
JavaScript
262
star
5

typesense-php

PHP client for Typesense: https://github.com/typesense/typesense
PHP
141
star
6

showcase-songs-search

A site to instantly search 32M songs from the MusicBrainz songs database, using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚡ 🎵 🔍
JavaScript
135
star
7

showcase-books-search

A site to instantly search 28M books from OpenLibrary using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚡ 📚 🔍
JavaScript
130
star
8

firestore-typesense-search

Firebase Extension to automatically push Firestore documents to Typesense for full-text search with typo tolerance, faceting, and more
JavaScript
122
star
9

laravel-scout-typesense-driver

Laravel Scout Driver for Typesense
PHP
114
star
10

typesense-python

Python client for Typesense: https://github.com/typesense/typesense
Python
104
star
11

typesense-docsearch-scraper

A fork of Algolia's awesome DocSearch Scraper, customized to index data in Typesense (an open source alternative to Algolia)
Python
70
star
12

typesense-dart

Dart client for Typesense
Dart
59
star
13

docusaurus-theme-search-typesense

A fork of the awesome @docusaurus/theme-search-algolia library customized to work with Typesense
TypeScript
48
star
14

typesense-ruby

Ruby client for Typesense: https://github.com/typesense/typesense
Ruby
43
star
15

typesense-instantsearch-demo

A demo app that shows how to use the Typesense InstantSearch adapter, to build rich search interfaces.
JavaScript
41
star
16

showcase-nextjs-typesense-ecommerce-store

An app showing how you can use Typesense & Next.js / React to build a full-fledged ecommerce browsing and searching experience
JavaScript
36
star
17

typesense-swift

Swift Client for Typesense ⚡️🔎
Swift
34
star
18

typesense-website

Typesense website and documentation | An open source search engine alternative to Algolia, Elasticsearch and Pinecone
Vue
33
star
19

typesense-java

Java client for Typesense
Java
31
star
20

showcase-ecommerce-store

An app showing how you can use Typesense to build a full-fledged ecommerce browsing and searching experience
JavaScript
31
star
21

gatsby-plugin-typesense

A Gatsby plugin to automatically index content to Typesense post-build
JavaScript
30
star
22

showcase-linux-commits-search

Instantly search 1M Linux Kernel Commit Messages using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚡ 💻 🔍
JavaScript
29
star
23

typesense-kubernetes

Typesense Kubernetes
24
star
24

postman

Postman collection for Typesense
21
star
25

typesense-rust

Rust client for Typesense | Work In Progress & Help Wanted
Rust
21
star
26

typesense-mongodb

A Node.js CLI to sync documents from a MongoDB collection to Typesense.
TypeScript
21
star
27

showcase-hn-comments-semantic-search

Semantic Search + Keyword Search + Hybrid Search + Filtering + Faceting on 300K HN Comments
JavaScript
20
star
28

typesense-docsearch.js

A fork of Algolia's awesome DocSearch.js, customized to use data from Typesense.
TypeScript
20
star
29

typesense-api-spec

Contains the API specs for the Typesense HTTP API
14
star
30

typesense-autocomplete-demo

A demo app that shows you how to use Algolia's autocomplete.js library with Typesense
HTML
13
star
31

typesense-instantsearch-semantic-search-demo

A demo that shows how to build a semantic search experience with Typesense's vector search feature and Instantsearch.js
JavaScript
13
star
32

showcase-xkcd-search

Search & Browse xkcd by topic & keywords | Using Typesense, an open source Algolia alternative and an easier-to-use alternative to Elasticsearch
JavaScript
12
star
33

typesense-rails

Rails Integration for Typesense | Work in Progress | Help Needed
Ruby
9
star
34

showcase-spellcheck

Use Typesense to build a type-ahead spellchecker
JavaScript
8
star
35

typesense-instantsearch-demo-no-npm-yarn

A demo that shows how to use typesense-instantsearch-adapter without NPM or YARN
HTML
8
star
36

showcase-airbnb-geosearch

Browse & GeoSearch 1 Million AirBnB listings with Typesense ⚡🔍 ️🌎
JavaScript
8
star
37

typesense-wordpress-plugin

Deprecated in favor of https://wordpress.org/plugins/search-with-typesense/
JavaScript
7
star
38

typesense-vue-instantsearch-demo

A demo app that shows you how to use Vue & the Typesense InstantSearch adapter, to build rich search interfaces.
Vue
6
star
39

homebrew-tap

Homebrew Formulae for Typesense
Ruby
4
star
40

showcase-federated-search

An app that showcases federated search in Typesense (Open source alternative to Algolia)
JavaScript
3
star
41

typesense-lando-plugin

A Lando plugin for Typesense
JavaScript
2
star
42

typesense-flutter-demo

1
star
43

.github

Org-wide shared github configs
1
star
44

solr-xml-to-jsonl

A CLI utility to convert a solr data file to a simple JSONL file
JavaScript
1
star
45

user-admin-search-laravel-demo

A demo Laravel app that integrates with Typesense
PHP
1
star