Skip to content

Instantly share code, notes, and snippets.

@lorentzca
Created February 7, 2017 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorentzca/cbae6d9ac5ae425f47fd971e3ba77659 to your computer and use it in GitHub Desktop.
Save lorentzca/cbae6d9ac5ae425f47fd971e3ba77659 to your computer and use it in GitHub Desktop.
Search algolia index data with golang
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/algolia/algoliasearch-client-go/algoliasearch"
"github.com/jessevdk/go-flags"
"os"
)
var opts struct {
AppId *string `short:"d" long:"appid" required:"true" description:"App ID"`
ApiKey *string `short:"k" long:"apikey" required:"true" description:"Readonly Api Key"`
IndexName *string `short:"i" long:"index" required:"true" description:"Index Name"`
Query *string `short:"q" long:"query" required:"true" description:"Query"`
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
appid := *opts.AppId
apikey := *opts.ApiKey
indexname := *opts.IndexName
query := *opts.Query
client := algoliasearch.NewClient(appid, apikey)
index := client.InitIndex(indexname)
res, err := index.Search(query, nil)
if err != nil {
fmt.Println(err)
}
jsonBytes, err := json.Marshal(res)
if err != nil {
fmt.Println("JSON Marshal error:", err)
}
out := new(bytes.Buffer)
json.Indent(out, jsonBytes, "", " ")
fmt.Println(out.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment