Documentation ¶
Overview ¶
Package sajari provides functionality for interacting with Search.io APIs.
Index ¶
- Variables
- type AggregateResult
- type BucketResult
- type BucketsResult
- type Client
- func (c *Client) Close() error
- func (c *Client) DeleteCollection(ctx context.Context, id string) error
- func (c *Client) DeleteRecord(ctx context.Context, k *Key) error
- func (c *Client) GetCollection(ctx context.Context, id string) (*Collection, error)
- func (c *Client) GetDefaultPipeline(ctx context.Context, id string, typ PipelineType) (string, error)
- func (c *Client) GetRecord(ctx context.Context, k *Key) (Record, error)
- func (c *Client) Interaction() *Interaction
- func (c *Client) Keys(ctx context.Context, field string) *KeyIterator
- func (c *Client) MutateRecord(ctx context.Context, k *Key, fms ...RecordMutation) error
- func (c *Client) Pipeline(name, version string) *Pipeline
- func (c *Client) Schema() *Schema
- func (c *Client) SendEvent(ctx context.Context, r *SendEventRequest) error
- func (c *Client) UpdateCollection(ctx context.Context, id string, opts ...UpdateCollectionOpt) error
- type Collection
- type CountResult
- type Credentials
- type DateResult
- type Field
- type FieldIndex
- type FieldIterator
- type FieldMode
- type FieldMutation
- type FieldType
- type Interaction
- type InteractionOptions
- type Key
- type KeyIterator
- type NoDefaultPipelineError
- type Opt
- type Pipeline
- func (p *Pipeline) CreateRecord(ctx context.Context, values map[string]string, r Record) (*Key, map[string]string, error)
- func (p *Pipeline) ReplaceRecord(ctx context.Context, values map[string]string, key *Key, r Record) (*Key, map[string]string, error)
- func (p *Pipeline) Search(ctx context.Context, params map[string]string, s Session) (*Results, map[string]string, error)
- type PipelineType
- type Record
- type RecordMutation
- type Result
- type Results
- type Schema
- type SendEventRequest
- type Session
- type Tracking
- type TrackingType
- type UpdateCollectionOpt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoSuchCollection is returned when a collection was requested but there // is no such collection. ErrNoSuchCollection = errors.New("no such collection") // ErrNoSuchCollectionDefaultPipeline is returned when a collection default // pipeline was requested but there is no such default. ErrNoSuchCollectionDefaultPipeline = errors.New("no such collection default pipeline") )
var ErrDone = errors.New("done")
ErrDone is returned when the iteration is complete.
var ErrNoSuchRecord = errors.New("no such record")
ErrNoSuchRecord is returned when a record was requested but there is no such record.
var NewSendEventRequest = openapi.NewSendEventRequest
Functions ¶
This section is empty.
Types ¶
type AggregateResult ¶
type AggregateResult interface {
// contains filtered or unexported methods
}
AggregateResult is an interface implemented by aggregate results.
type BucketResult ¶
BucketResult is bucket information as reported by an aggregate.
type BucketsResult ¶
type BucketsResult map[string]BucketResult
BucketsResult is a type returned from a query performing bucket aggregate.
type Client ¶
type Client struct { Project string Collection string ClientConn *grpc.ClientConn // contains filtered or unexported fields }
Client is a type which makes requests to Search.io.
func New ¶
New creates a new Client which can be used to make requests to Search.io services.
Example ¶
package main import ( sajari "code.sajari.com/sdk-go" ) func main() { 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() }
Output:
func (*Client) DeleteCollection ¶
DeleteCollection removes a collection identified by the provided ID.
If there is no such collection matching the given ID this method returns an error wrapping ErrNoSuchCollection.
func (*Client) DeleteRecord ¶
DeleteRecord removes a record identified by the key k. Returns non-nil error if there was a communication problem, but fails silently if any key doesn't have a corresponding record.
If there is no such record matching the given key this method returns an error wrapping ErrNoSuchRecord.
Example ¶
package main import ( "context" sajari "code.sajari.com/sdk-go" ) func main() { 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() key := sajari.NewKey("id", "12345") // or using your Key returned from another call err = client.DeleteRecord(context.Background(), key) if err != nil { // handle } }
Output:
func (*Client) GetCollection ¶
GetCollection gets a collection identified by the provided ID.
If there is no such collection matching the given ID this method returns an error wrapping ErrNoSuchCollection.
func (*Client) GetDefaultPipeline ¶
func (c *Client) GetDefaultPipeline(ctx context.Context, id string, typ PipelineType) (string, error)
GetDefaultPipeline gets the default pipeline for a collection.
func (*Client) GetRecord ¶
GetRecord retrieves the record identified by the key k.
If there is no such record matching the given key this method returns an error wrapping ErrNoSuchRecord.
Example ¶
package main import ( "context" sajari "code.sajari.com/sdk-go" ) func main() { 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() key := sajari.NewKey("id", "12345") // or using your Key returned from another call record, err := client.GetRecord(context.Background(), key) if err != nil { // handle } _ = record // use record }
Output:
Example (ErrNoSuchRecord) ¶
package main import ( "context" "errors" sajari "code.sajari.com/sdk-go" ) func main() { 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() key := sajari.NewKey("id", "12345") // or using your Key returned from another call _, err = client.GetRecord(context.Background(), key) if err != nil { if errors.Is(err, sajari.ErrNoSuchRecord) { // handle case where there is no such record } // handle other error cases } }
Output:
func (*Client) Interaction ¶
func (c *Client) Interaction() *Interaction
Interaction creates a new Interaction which can be used to register interactions.
func (*Client) Keys ¶
func (c *Client) Keys(ctx context.Context, field string) *KeyIterator
Keys returns an iterator which will retrieve the given key field value for each record in the collection. If changes to the collection are made whilst iterating, the iterator may become invalid or return keys already visited.
func (*Client) MutateRecord ¶
MutateRecord mutates a record identified by the key k by applying the given record mutation operations.
If there is no such record matching the given key this method returns an error wrapping ErrNoSuchRecord.
Example ¶
package main import ( "context" "time" sajari "code.sajari.com/sdk-go" ) func main() { 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() 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 } }
Output:
func (*Client) SendEvent ¶
func (c *Client) SendEvent(ctx context.Context, r *SendEventRequest) error
SendEvent sends an event to the ranking system after a user interacts with a search result.
func (*Client) UpdateCollection ¶
func (c *Client) UpdateCollection(ctx context.Context, id string, opts ...UpdateCollectionOpt) error
UpdateCollection updates a collection identified by the provided ID.
If there is no such collection matching the given ID this method returns an error wrapping ErrNoSuchCollection.
type Collection ¶
type Collection = openapi.Collection
A Collection stores the records that can be searched.
type CountResult ¶
CountResult is a type returned from a query which has performed a count aggregate.
type Credentials ¶
type Credentials interface {
// contains filtered or unexported methods
}
Credentials is an interface which is implemented by types providing credential information used in requests.
func KeyCredentials ¶
func KeyCredentials(keyID, keySecret string) Credentials
KeyCredentials defines a Credential which uses a Key ID-Secret pair.
type DateResult ¶
DateResult is a type returned from a query which has performed a date aggregate.
type Field ¶
type Field struct { // Name used to identify the field. Name string // Description of the field. Description string // Type of the field. Type FieldType // Mode of the field. Mode FieldMode // Repeated indicates that this field can hold a list of values. Repeated bool // Indexes is a list of the field's indexes. Indexes []FieldIndex }
Field represents a meta field which can be assigned in a collection record.
type FieldIndex ¶
type FieldIndex struct { // Spec is the identifier/specification for the creation of the index. Spec string // Description is a description of the index. Description string }
FieldIndex is a field index.
type FieldIterator ¶
type FieldIterator struct {
// contains filtered or unexported fields
}
FieldIterator iterates through a list of fields.
func (*FieldIterator) Next ¶
func (it *FieldIterator) Next() (Field, error)
Next returns the next field in the iteration. If there are no more fields remaining then an error wrapping ErrDone is returned.
type FieldMutation ¶
type FieldMutation interface {
// contains filtered or unexported methods
}
FieldMutation is an interface which is satisfied by schema field mutations.
func FieldAddIndexMutation ¶
func FieldAddIndexMutation(x FieldIndex) FieldMutation
FieldAddIndexMutation adds a schema field mutation which adds an index to a field.
func FieldModeMutation ¶
func FieldModeMutation(m FieldMode) FieldMutation
FieldModeMutation creates a schema field mutation which changes the unique constraint on a field.
func FieldNameMutation ¶
func FieldNameMutation(name string) FieldMutation
FieldNameMutation creates a schema field mutation which changes the name of a field.
func FieldRepeatedMutation ¶
func FieldRepeatedMutation(repeated bool) FieldMutation
FieldRepeatedMutation creates a schema field mutation which changes the repeated property on a field.
func FieldTypeMutation ¶
func FieldTypeMutation(ty FieldType) FieldMutation
FieldTypeMutation creates a schema field mutation which changes the type of a field.
type Interaction ¶
type Interaction struct {
// contains filtered or unexported fields
}
Interaction is used to register interactions.
func (*Interaction) ConsumeToken ¶
func (i *Interaction) ConsumeToken(ctx context.Context, token string, options InteractionOptions) error
ConsumeToken registers an interaction corresponding to a token.
type InteractionOptions ¶
InteractionOptions are passed with the token.
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key is a unique identifier record.
type KeyIterator ¶
type KeyIterator struct {
// contains filtered or unexported fields
}
KeyIterator iterates through a list of keys.
func (*KeyIterator) Next ¶
func (it *KeyIterator) Next() (*Key, error)
Next returns the next key in the iteration. If there are no more keys remaining then an error wrapping ErrDone is returned.
type NoDefaultPipelineError ¶
type NoDefaultPipelineError struct { // Name of the pipeline used in the attempted operation. Name string }
NoDefaultPipelineError is the error type returned when the collection does not have a default version set for a given pipeline. To resolve errors of this type, the caller should either pass an explicit pipeline version along with their pipeline name, or they should set a default pipeline version using the API or CLI tools.
func (*NoDefaultPipelineError) Error ¶
func (e *NoDefaultPipelineError) Error() string
Error implements error.
type Opt ¶
type Opt func(c *Client)
Opt is a type which defines Client options.
func WithCredentials ¶
func WithCredentials(cr Credentials) Opt
WithCredentials sets the client credentials used in each request.
func WithEndpoint ¶
WithEndpoint configures the client to use a custom endpoint.
func WithGRPCDialOption ¶
func WithGRPCDialOption(opt grpc.DialOption) Opt
WithGRPCDialOption returns an Opt which appends a new grpc.DialOption to an underlying gRPC dial.
func WithV4Endpoint ¶
WithV4Endpoint configures the client to use a v4 endpoint.
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline is a handler for a named pipeline.
func (*Pipeline) CreateRecord ¶
func (p *Pipeline) CreateRecord(ctx context.Context, values map[string]string, r Record) (*Key, map[string]string, error)
CreateRecord uses a pipeline to add a single record to a collection and returns a Key which can be used to retrieve the newly created record.
Example ¶
package main import ( "context" sajari "code.sajari.com/sdk-go" ) func main() { 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() pipeline := client.Pipeline("record", "v5") 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 } _ = key // use key }
Output:
func (*Pipeline) ReplaceRecord ¶
func (p *Pipeline) ReplaceRecord(ctx context.Context, values map[string]string, key *Key, r Record) (*Key, map[string]string, error)
ReplaceRecord uses a pipeline to replace a single record in a collection represented by the given Key.
Example ¶
package main import ( "context" sajari "code.sajari.com/sdk-go" ) func main() { 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() pipeline := client.Pipeline("record", "v5") values := map[string]string{ // ... } key := sajari.NewKey("id", "12345") // or using your Key returned from another call record := sajari.Record{ "id": 12345, "name": "Smart TV", "brand": "Sunny", "price": 899, } key, _, err = pipeline.ReplaceRecord(context.Background(), values, key, record) if err != nil { // handle } _ = key // use key }
Output:
func (*Pipeline) Search ¶
func (p *Pipeline) Search(ctx context.Context, params map[string]string, s Session) (*Results, map[string]string, error)
Search runs a search query defined by a pipeline with the given params and session to run in. Returns the query results and returned params (which could have been modified in the pipeline).
Example ¶
package main import ( "context" "log" sajari "code.sajari.com/sdk-go" ) func main() { 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() pipeline := client.Pipeline("query", "v5") 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) } }
Output:
Example (NoDefaultPipelineError) ¶
package main import ( "context" "errors" sajari "code.sajari.com/sdk-go" ) func main() { 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() pipeline := client.Pipeline("query", "") values := map[string]string{ "q": "your search terms", } _, _, err = pipeline.Search(context.Background(), values, sajari.NonTrackedSession()) if err != nil { var e *sajari.NoDefaultPipelineError if errors.As(err, &e) { // handle case where there is no default pipeline version set } // handle other error cases } }
Output:
type PipelineType ¶
type PipelineType string
PipelineType represents the type of a pipeline.
const ( // RecordPipelineType is the record pipeline type. RecordPipelineType PipelineType = "RECORD" // QueryPipelineType is the query pipeline type. QueryPipelineType PipelineType = "QUERY" )
type Record ¶
type Record map[string]interface{}
Record is a set of field-value pairs representing a record in a collection.
type RecordMutation ¶
type RecordMutation interface {
// contains filtered or unexported methods
}
RecordMutation is an interface satisfied by all record mutations defined in this package.
func SetFieldValue ¶
func SetFieldValue(field string, value interface{}) RecordMutation
SetFieldValue is a RecordMutation which sets field to value. If value is nil then this unsets field.
func SetFields ¶
func SetFields(m map[string]interface{}) []RecordMutation
SetFields is a convenience method for creating field mutations to set a map of values.
type Result ¶
type Result struct { // Values are field values of records. Values map[string]interface{} // Tokens contains any tokens associated with this Result. Tokens map[string]interface{} // Score is the overall score of this Result. Score float64 // IndexScore is the index-matched score of this Result. IndexScore float64 }
Result is an individual query result.
type Results ¶
type Results struct { // Reads is the total number of index values read. Reads int // TotalResults is the total number of results for the query. TotalResults int // Time taken to perform the query. Latency time.Duration // Aggregates computed on the query results (see Aggregate). Aggregates map[string]interface{} // AggregateFilters computed on query results (see Aggregate). AggregateFilters map[string]interface{} // Results of the query. Results []Result }
Results is a collection of results from a Search.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema provides methods for managing collection schemas. Use Client.Schema to create one for a collection.
func (*Schema) CreateField ¶
CreateField creates a new field in the schema.
func (*Schema) Fields ¶
func (s *Schema) Fields(ctx context.Context) *FieldIterator
Fields returns an iterator which retrieves all the fields in the collection.
func (*Schema) MutateField ¶
MutateField mutates the field identified by name.
type SendEventRequest ¶
type SendEventRequest = openapi.SendEventRequest
type Session ¶
type Session interface { // Reset the session. Reset() // contains filtered or unexported methods }
Session is an interface which defines session handling for search.
func NewSession ¶
func NewSession(ty TrackingType, field string, data map[string]string) Session
NewSession creates a Session which generates tracking information for performing queries within a search.
func NonTrackedSession ¶
func NonTrackedSession() Session
NonTrackedSession creates a session with no tracking enabled.
func WebSearchSession ¶
WebSearchSession handles search sessions for website-style searching.
It tracks the search text (pass to the query pipeline via queryLabel), and determines if the session should be reset (i.e. if the query text has changed significantly).
A common session handler would be:
WebSearchSession("q", NewSession())
type Tracking ¶
type Tracking struct { Type TrackingType // Query ID of the query. QueryID string // Sequence number of query. Sequence int // Tracking field used to identify records in the collection. // Must be unique schema field. Field string // Custom values to be included in tracking data. Data map[string]string }
Tracking provides a Session implementation where the details of the tracking object are managed by an external source.
Example ¶
package main import ( "context" sajari "code.sajari.com/sdk-go" ) func main() { 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() pipeline := client.Pipeline("query", "") values := map[string]string{ "q": "your search terms", } _, _, 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 } }
Output:
type TrackingType ¶
type TrackingType string
TrackingType defines different modes of tracking which can be applied to query requests.
const ( TrackingNone TrackingType = "" // No tracking is enabled. TrackingClick TrackingType = "CLICK" // Click tracking is enabled, Click tokens will be returned with results. TrackingPosNeg TrackingType = "POS_NEG" // Positive/negative interaction tokens should be returned with results. )
TrackingType constants.
type UpdateCollectionOpt ¶
type UpdateCollectionOpt func(c *openapi.Collection, updateMask map[string]struct{})
UpdateCollectionOpt is a type which defines options to update a collection.
func SetAuthorizedQueryDomains ¶
func SetAuthorizedQueryDomains(domains []string) UpdateCollectionOpt
SetAuthorizedQueryDomains is a collection mutation that set a collection's authorized query domains.
func SetCollectionDisplayName ¶
func SetCollectionDisplayName(displayName string) UpdateCollectionOpt
SetCollectionDisplayName is a collection mutation that set a collection's display name.