Go client for the Typesense API: https://github.com/leobispo/typesense
go get github.com/leobispo/typesense-go
Import the the package into your code :
import "github.com/leobispo/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.
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)
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)
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)
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)
client.Collection("companies").Document("123").Retrieve()
document := struct {
CompanyName string `json:"company_name"`
NumEmployees int `json:"num_employees"`
}{
CompanyName: "Stark Industries",
NumEmployees: 5500,
}
client.Collection("companies").Document("123").Update(document)
client.Collection("companies").Document("123").Delete()
filter := &api.DeleteDocumentsParams{FilterBy: "num_employees:>100", BatchSize: 100}
client.Collection("companies").Documents().Delete(filter)
client.Collection("companies").Retrieve()
client.Collection("companies").Documents().Export()
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)
client.Collections().Retrieve()
client.Collection("companies").Delete()
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)
client.Key(1).Retrieve()
client.Keys().Retrieve()
client.Key(1).Delete()
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)
client.Collection("companies").Overrides().Retrieve()
client.Collection("companies").Override("customize-apple").Delete()
body := &api.CollectionAliasSchema{CollectionName: "companies_june11"}
client.Aliases().Upsert("companies", body)
client.Alias("companies").Retrieve()
client.Aliases().Retrieve()
client.Alias("companies").Delete()
synonym := &api.SearchSynonymSchema{
Synonyms: []string{"blazer", "coat", "jacket"},
}
client.Collection("products").Synonyms().Upsert("coat-synonyms", synonym)
synonym := &api.SearchSynonymSchema{
Root: "blazer",
Synonyms: []string{"blazer", "coat", "jacket"},
}
client.Collection("products").Synonyms().Upsert("coat-synonyms", synonym)
client.Collection("products").Synonym("coat-synonyms").Retrieve()
client.Collection("products").Synonyms().Retrieve()
client.Collection("products").Synonym("coat-synonyms").Delete()
client.Operations().Snapshot("/tmp/typesense-data-snapshot")
client.Operations().Vote()
Bug reports and pull requests are welcome on GitHub at https://github.com/leobispo/typesense-go.
Install dependencies,
go mod download
Update the generated files,
go generate
NOTE: Make sure you have
GOBIN
in environment variables
typesense-go
is distributed under the Apache 2 license.