Categorycode.sajari.com/sdk-go
modulepackage
0.0.0-20220302044211-e744b498bc85
Repository: https://github.com/sajari/sdk-go.git
Documentation: pkg.go.dev

# README

Search.io SDK for Go

Go reference Build status Report card Sourcegraph

The official Search.io Go client library.

Search.io offers a search and discovery service with Neuralsearch®, the world's first instant AI search technology. Businesses of all sizes use Search.io to build site search and discovery solutions that maximize e-commerce revenue, optimize on-site customer experience, and scale their online presence.

Table of contents

Requirements

Requires Go version 1.13 or higher.

Installation

Install sdk-go with:

go get -u code.sajari.com/sdk-go

Then, import it using:

import "code.sajari.com/sdk-go"

Documentation

Below are a few simple examples that will help get you up and running.

Creating a client

To start you need to create a client to make calls to the API.

You can get your account ID, collection ID, key ID and key secret from the Search.io console.

creds := sajari.KeyCredentials("key-id", "key-secret")
client, err := sajari.New("account_id", "collection_id", sajari.WithCredentials(creds))
if err != nil {
	// handle
}
defer client.Close()

Note: do not forget to close the client when you are finished with it.

Overriding the default endpoint

If you need to override the default endpoint, you can use the WithEndpoint client option.

opts := []sajari.Opt{
	sajari.WithEndpoint("api-au-valkyrie.sajari.com:50051"),
	sajari.WithCredentials(sajari.KeyCredentials("key-id", "key-secret")),
}

client, err := sajari.New(shop.AccountID, shop.CollectionID, opts...)
if err != nil {
	// handle
}
defer client.Close()

Available endpoints

The endpoints that you can pass to WithEndpoint include:

  • api-au-valkyrie.sajari.com:50051
  • api-us-valkyrie.sajari.com:50051

Adding a record

A record can be added to a collection using the CreateRecord method on a record Pipeline.

First, you should initialise the record pipeline by passing in its name and the version you want to use.

pipeline := client.Pipeline("record", "v5")

Next, set up any values that you need to pass to the record pipeline, define your record and call CreateRecord.

Values allow you to control the pipeline execution. For example, they can be used to dynamically turn pipeline steps on or off and control how the record is processed.

values := map[string]string{
	// ...
}

record := sajari.Record{
	"id":    12345,
	"name":  "Smart TV",
	"brand": "Sunny",
	"price": 999,
}

key, _, err := pipeline.CreateRecord(context.Background(), values, record)
if err != nil {
	// handle
}

You can use the returned key to uniquely identify the newly inserted record. This can be used in various calls such as GetRecord, MutateRecord, DeleteRecord and ReplaceRecord.

Getting a record

An existing record in your collection can be retrieved using the GetRecord method on your Client.

key := sajari.NewKey("id", "12345") // or using your Key returned from another call
record, err := client.GetRecord(context.Background(), key)
if err != nil {
	// handle
}

Replacing a record

An existing record in your collection can be entirely replaced using the ReplaceRecord method on a record Pipeline.

When calling ReplaceRecord Search.io actually performs an upsert. If the record is an existing record, Search.io performs a diff between the old and new records and applies your changes—this is extremely efficient. Because ReplaceRecord can both insert and update it is typically preferred over CreateRecord when adding a record.

Note: if you want to make granular changes to the record it is best to use MutateRecord.

Note: if you want to change an indexed field you will need to use ReplaceRecord.

First, you should initialise the record pipeline by passing in its name and the version you want to use.

pipeline := client.Pipeline("record", "v5")

Next, set up any values that you need to pass to the record pipeline, define your record and call ReplaceRecord.

Values allow you to control the pipeline execution. For example, they can be used to dynamically turn pipeline steps on or off and control how the record is processed.

values := map[string]string{
	// ...
}

key := sajari.NewKey("id", "12345") // or using your Key returned from another call

record := sajari.Record{
	"id":    12345,
	"name":  "Large Smart TV",
	"brand": "Sunny",
	"price": 999,
}

key, _, err = pipeline.ReplaceRecord(context.Background(), values, key, record)
if err != nil {
	// handle
}

Mutating a record

An existing record in your collection can be mutated using the MutateRecord method on your Client. You might need this method if you need to update a single field or unset a single field.

As an example, if you were storing products in your collection and you needed to update a product's price or stock levels this method would be useful.

Note: if you want to replace the entire record it is best to use ReplaceRecord.

Note: if you want to change an indexed field you will need to use ReplaceRecord.

You will need to pass one or more mutation operations to MutateRecord that will be appled to your record. For example, you can pass an operation to set a field, unset a field or set multiple fields at once.

key := sajari.NewKey("id", "12345") // or using your Key returned from another call

// update a single field
err := client.MutateRecord(context.Background(), key, sajari.SetFieldValue("updated_at", time.Now().String()))
if err != nil {
	// handle
}

// unset a single field
err := client.MutateRecord(context.Background(), key, sajari.SetFieldValue("available", nil))
if err != nil {
	// handle
}

// set multiple fields at once
err := client.MutateRecord(context.Background(), key, sajari.SetFields(map[string]interface{}{
	"updated_at": time.Now().String(),
	"available":  nil,
})...)
if err != nil {
	// handle
}

Deleting a record

An existing record in your collection can be deleted using the DeleteRecord method on your Client.

key := sajari.NewKey("id", "12345") // or using your Key returned from another call
err := client.DeleteRecord(context.Background(), key)
if err != nil {
	// handle
}

Searching for records

You can search for records in your collection using the Search method with a query Pipeline.

First, you should initialise the query pipeline by passing in its name and the version you want to use.

pipeline := client.Pipeline("search", "v5")

Next, set up any values that you need to pass to the query pipeline, create a session and run your search.

Values allow you to control the pipeline execution. For example, they can be used to dynamically turn pipeline steps on or off and control how the records are processed.

In the example below, passing the resultsPerPage and page values allows you to paginate through records for the search query provided in q. Note: this assumes that you have the pagination step in your query pipeline.

values := map[string]string{
	"q":              "your search terms",
	"resultsPerPage": "10",
	"page":           "1",
}

res, _, err := pipeline.Search(context.Background(), values, sajari.NonTrackedSession())
if err != nil {
	// handle
}

for _, r := range res.Results {
	log.Printf("Values: %v", r.Values)
	log.Printf("Tokens: %v", r.Tokens)
}

Tracking

If you don't want tracking enabled, then use NonTrackedSession. For example your Search call might look like:

res, _, err := pipeline.Search(context.Background(), values, sajari.NonTrackedSession())
if err != nil {
	// handle
}

If you're tracking website-style searches, then use WebSearchSession. For example your Search call might look like:

res, _, err := pipeline.Search(context.Background(), values, sajari.WebSearchSession("q", sajari.NewSession()))
if err != nil {
	// handle
}

If you want to manage the details of tracking externally, use Tracking. For example your Search call might look like:

res, _, err := pipeline.Search(context.Background(), values, sajari.Tracking{
	Type:     sajari.TrackingPosNeg,
	QueryID:  "4216691599",
	Sequence: 1,
	Field:    "id",
	Data:     map[string]string{},
})
if err != nil {
	// handle
}

Development

Pull requests from the community are welcome. If you submit one, please keep the following guidelines in mind:

  1. Code must be go fmt compliant.
  2. All types, structs and funcs should be documented.
  3. Ensure that go test ./... succeeds.

Test

Run all tests:

go test ./...

License

We use the MIT License.

# Functions

FieldAddIndexMutation adds a schema field mutation which adds an index to a field.
FieldModeMutation creates a schema field mutation which changes the unique constraint on a field.
FieldNameMutation creates a schema field mutation which changes the name of a field.
FieldRepeatedMutation creates a schema field mutation which changes the repeated property on a field.
FieldTypeMutation creates a schema field mutation which changes the type of a field.
KeyCredentials defines a Credential which uses a Key ID-Secret pair.
New creates a new Client which can be used to make requests to Search.io services.
NewKey creates a new key with a field and value.
NewSession creates a Session which generates tracking information for performing queries within a search.
NonTrackedSession creates a session with no tracking enabled.
SetAuthorizedQueryDomains is a collection mutation that set a collection's authorized query domains.
SetCollectionDisplayName is a collection mutation that set a collection's display name.
SetFields is a convenience method for creating field mutations to set a map of values.
SetFieldValue is a RecordMutation which sets field to value.
WebSearchSession handles search sessions for website-style searching.
WithCredentials sets the client credentials used in each request.
WithEndpoint configures the client to use a custom endpoint.
WithGRPCDialOption returns an Opt which appends a new grpc.DialOption to an underlying gRPC dial.
WithV4Endpoint configures the client to use a v4 endpoint.

# Constants

Don't require a value.
Field value must be set.
Field value must be unique (and hence also set).
QueryPipelineType is the query pipeline type.
RecordPipelineType is the record pipeline type.
Click tracking is enabled, Click tokens will be returned with results.
No tracking is enabled.
Positive/negative interaction tokens should be returned with results.
Enumeration of field types.
Enumeration of field types.
Enumeration of field types.
Enumeration of field types.
Enumeration of field types.
Enumeration of field types.

# Variables

ErrDone is returned when the iteration is complete.
ErrNoSuchCollection is returned when a collection was requested but there is no such collection.
ErrNoSuchCollectionDefaultPipeline is returned when a collection default pipeline was requested but there is no such default.
ErrNoSuchRecord is returned when a record was requested but there is no such record.
No description provided by the author

# Structs

BucketResult is bucket information as reported by an aggregate.
Client is a type which makes requests to Search.io.
Field represents a meta field which can be assigned in a collection record.
FieldIndex is a field index.
FieldIterator iterates through a list of fields.
Interaction is used to register interactions.
InteractionOptions are passed with the token.
Key is a unique identifier record.
KeyIterator iterates through a list of keys.
NoDefaultPipelineError is the error type returned when the collection does not have a default version set for a given pipeline.
Pipeline is a handler for a named pipeline.
Result is an individual query result.
Results is a collection of results from a Search.
Schema provides methods for managing collection schemas.
Tracking provides a Session implementation where the details of the tracking object are managed by an external source.

# Interfaces

AggregateResult is an interface implemented by aggregate results.
Credentials is an interface which is implemented by types providing credential information used in requests.
FieldMutation is an interface which is satisfied by schema field mutations.
RecordMutation is an interface satisfied by all record mutations defined in this package.
Session is an interface which defines session handling for search.

# Type aliases

BucketsResult is a type returned from a query performing bucket aggregate.
A Collection stores the records that can be searched.
CountResult is a type returned from a query which has performed a count aggregate.
DateResult is a type returned from a query which has performed a date aggregate.
FieldMode defines field modes.
FieldType defines field data types.
Opt is a type which defines Client options.
PipelineType represents the type of a pipeline.
Record is a set of field-value pairs representing a record in a collection.
No description provided by the author
TrackingType defines different modes of tracking which can be applied to query requests.
UpdateCollectionOpt is a type which defines options to update a collection.