Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/GianOrtiz/typesense-go

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/GianOrtiz/typesense-go

  • v0.3.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

Typesense Go

An unofficial Go client for Typesense HTTP API.

GoDoc Go Report Card codecov Build Status

Installation

To install typesense-go using go modules just run the command below:

go get github.com/GianOrtiz/typesense-go

Usage

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

Package last updated on 07 Jan 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc