typesense-go
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.