
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.