
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
github.com/GianOrtiz/typesense-go
An unofficial Go client for Typesense HTTP API.
To install typesense-go
using go modules just run the command below:
go get github.com/GianOrtiz/typesense-go
We will show you how to use this package to create a client, create a collection, index a document and search for documents. This usage section is inspired by the guide for other programming languages in the typesense website.
Before you can communicate with Typesense you need a client, to create a client you can use the following code:
client := typesense.NewClient(
&typesense.Node{
Host: "localhost",
Port: "8108",
Protocol: "http",
APIKey: "api-key",
},
2,
)
if err := client.Ping(); err != nil {
log.Printf("couldn't connect to typesense: %v", err)
}
Now you can define your collection and create it:
booksSchema := typesense.CollectionSchema{
Name: "books",
Fields: []typesense.CollectionField{
{
Name: "title",
Type: "string",
},
{
Name: "authors",
Type: "string[]",
},
{
Name: "image_url",
Type: "string",
},
{
Name: "publication_year",
Type: "int32",
},
{
Name: "ratings_count",
Type: "int32",
},
{
Name: "average_rating",
Type: "int32",
},
{
Name: "authors_facet",
Type: "string[]",
Facet: true,
},
{
Name: "publication_year_facet",
Type: "string",
Facet: true,
},
},
DefaultSortingField: "ratings_count",
}
collection, err := client.CreateCollection(booksSchema)
if err != nil {
log.Printf("couldn't create collection books: %v", err)
}
Let's suppose we have a struct type Book
that represents the document for the books
collection:
type Book struct {
Title string `json:"title"`
Authors []string `json:"authors"`
ImageURL string `json:"image_url"`
PublicationYear int32 `json:"publication_year"`
RatingsCount int32 `json:"ratings_count"`
AverageRating float64 `json:"average_rating"`
AuthorsFacet []string `json:"authors_facet"`
PublicationYearFacet string `json:"publication_year_facet"`
}
We can create a new book document:
goProgrammingLanguage := Book{
Title: "The Go Programming Language",
Authors: []string{"Brian W. Kernighan", "Alan Donovan"},
ImageURL: "https://images-na.ssl-images-amazon.com/images/I/41aSIiETPPL.jpg",
PublicationYear: 2015,
RatingsCount: 287,
AverageRating: 4.7,
AuthorsFacet: []string{"Brian W. Kernighan", "Alan Donovan"},
PublicationYearFacet: "2015",
}
documentResponse := client.IndexDocument("books", goProgrammingLanguage)
if documentResponse.Error != nil {
log.Printf("couldn't index book document: %v", err)
}
Now that we have a collection and a book document in the collection we can search for the book:
search, err := client.Search("books", "The Go Programming Language", []string{"title"}, nil)
if err != nil {
log.Printf("couldn't search for books: %v", err)
}
for _, hit := range search.Hits {
// hit.Document is a map[string]interface{}
log.Println(hit.Document["title"])
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.