tsclient

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2022 License: BSD-3-Clause Imports: 11 Imported by: 1

README

tsclient

Go Reference

tsclient is an alternative Go client for Typesense.

Why not the official client? We didn't like it, that's all. This client is a lot more similar in usage to Arikawa, the Discord library used in Termora.

Note: this is still a work in progress! The current API is unlikely to change, but no promises about that.

Differences to the official client

  • No interface{}: all methods with variable responses (such as inserting/deleting documents) allow passing in a pointer to unmarshal to.
    • In the few places where the API returns variable responses in otherwise structured data, raw byte slices are used instead of the empty interface.
  • Cleaner layout: no method chaining needed to make requests. For example, client.Collection("companies").Document("123").Delete() in the official client becomes client.DeleteDocument("companies", "123").
  • No generated code: the official client uses a lot of generated code, which, while perfectly passable, isn't very intuitive to use. Everything in this client is written by hand.

Todo

  • Collection endpoints
  • Document endpoints
    • Inserting documents
    • Upserting documents
    • Retrieving documents
    • Updating documents
    • Deleting documents
    • Bulk deleting documents
    • Bulk importing documents
    • Search
    • Group by searches
  • API key endpoints
  • Override endpoints
  • Synonym endpoints
  • Cluster operations
    • Create snapshot
    • Re-elect leader
    • Toggle slow request log
    • Cluster metrics
    • API stats
    • Health

License

BSD 3-Clause License

Copyright (c) 2021, Starshine System
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Documentation

Overview

Package tsclient is a client library for the Typesense search engine.

Index

Constants

View Source
const (
	ErrBadRequest    = errors.Sentinel("400 bad request")
	ErrUnauthorized  = errors.Sentinel("401 unauthorized")
	ErrNotFound      = errors.Sentinel("404 not found")
	ErrAlreadyExists = errors.Sentinel("409 resource already exists")
	ErrUnprocessable = errors.Sentinel("422 unprocessable entity")
	ErrUnavailable   = errors.Sentinel("503 service unavailable")
)

Errors returned by Request

View Source
const ErrNotSlice = errors.Sentinel("slice expected")

ErrNotSlice is returned by Import if a type other than a slice is given as input.

View Source
const VERSION = "0.1.0"

VERSION is unlikely to ever be updated even as the library gets new releases

Variables

This section is empty.

Functions

func WithBody

func WithBody(r io.Reader) func(*http.Request) error

WithBody adds a body to the request.

func WithHeader

func WithHeader(header http.Header) func(*http.Request) error

WithHeader adds headers to the request.

func WithJSONBody

func WithJSONBody(v interface{}) func(*http.Request) error

WithJSONBody adds a JSON body to the request. Marshaling errors are ignored.

func WithURLValues

func WithURLValues(values url.Values) func(*http.Request) error

WithURLValues adds query parameters to the request.

Types

type Client

type Client struct {
	Client *http.Client

	// Debug is a debug logging function. No-op by default.
	Debug func(tmpl string, args ...interface{})

	UserAgent string
	// contains filtered or unexported fields
}

Client is a Typesense API client.

func New

func New(url, apiKey string) (*Client, error)

New creates a new Client and pings the server.

func (*Client) Collection

func (c *Client) Collection(name string) (col Collection, err error)

Collection gets a collection by name.

func (*Client) Collections

func (c *Client) Collections() (cols []Collection, err error)

Collections returns a summary of all your collections. The collections are returned sorted by creation date, with the most recent collections appearing first.

func (*Client) CreateCollection

func (c *Client) CreateCollection(name, defaultSortingField string, fields []CreateFieldData) (col Collection, err error)

CreateCollection creates a collection. defaultSortingField is optional and may be left empty.

func (*Client) DeleteCollection

func (c *Client) DeleteCollection(name string) (col Collection, err error)

DeleteCollection permanently drops a collection. This action cannot be undone. For large collections, this might have an impact on read latencies.

func (*Client) DeleteDocument

func (c *Client) DeleteDocument(collection, id string, out interface{}) error

DeleteDocument deletes a document in the collection. The deleted document is unmarshaled to `out` if it is not nil.

func (*Client) DeleteQuery

func (c *Client) DeleteQuery(collection, filter string, batchSize int) (deleted int, err error)

DeleteQuery deletes documents in the collection matching filter. Returns the number of deleted documents.

func (*Client) Document

func (c *Client) Document(collection, id string, out interface{}) (string, error)

Document retrieves a document from the collection by ID. The document is unmarshaled to `out` if it is not nil.

func (*Client) Health

func (c *Client) Health() (ok bool, err error)

Health performs a health check on the server.

func (*Client) Import

func (c *Client) Import(collection, action string, s interface{}) (ok []bool, err error)

Import imports the documents into the collection. It returns an error if s is not a slice.

func (*Client) ImportSlice

func (c *Client) ImportSlice(collection, action string, s []interface{}) (ok []bool, err error)

ImportSlice imports a slice of documents into the collection.

func (*Client) Insert

func (c *Client) Insert(collection string, doc interface{}, out interface{}) (err error)

Insert inserts a document into the collection. The inserted document is unmarshaled to `out` if it is not nil.

func (*Client) Request

func (c *Client) Request(method, endpoint string, opts ...RequestOption) (response []byte, err error)

Request makes a request returning a JSON body.

func (*Client) Search

func (c *Client) Search(collection string, data SearchData) (res SearchResult, err error)

Search searches the collection.

func (*Client) UpdateDocument

func (c *Client) UpdateDocument(collection, id string, doc, out interface{}) error

UpdateDocument updates a document in the collection by ID. The updated document is unmarshaled to `out` if it is not nil.

func (*Client) Upsert

func (c *Client) Upsert(collection string, doc interface{}, out interface{}) (err error)

Upsert inserts a document into the collection, updating it if it already exists. The inserted document is unmarshaled to `out` if it is not nil.

type Collection

type Collection struct {
	Name                string  `json:"name"`
	NumDocuments        int     `json:"num_documents,omitempty"`
	Fields              []Field `json:"fields"`
	DefaultSortingField string  `json:"default_sorting_field,omitempty"`
}

Collection ...

type CreateFieldData

type CreateFieldData struct {
	Name  string
	Type  string
	Facet bool
	// false = index the field, true = don't index the field
	NoIndex bool
	Infix   bool
}

CreateFieldData is the field data passed to CreateCollection. The Index field is inverted here to avoid needing a bool pointer.

type Field

type Field struct {
	Name  string `json:"name"`
	Type  string `json:"type"`
	Facet bool   `json:"facet"`
	Index bool   `json:"index"`
	Infix bool   `json:"infix"`
}

Field is a field of a collection.

type Highlight

type Highlight struct {
	// The matched field name
	Field string `json:"field"`

	Indices []int `json:"indices"`

	MatchedTokens jsonutil.Raw `json:"matched_tokens"`

	// Only present for non-array string fields
	Snippet string `json:"snippet,omitempty"`

	// Only present for string array fields
	Snippets []string `json:"snippets,omitempty"`
}

Highlight is a highlight in SearchResult.

type RequestOption

type RequestOption func(*http.Request) error

RequestOption is an optional request option.

type SearchData

type SearchData struct {
	// The query text to search for in the collection.
	Query string
	// One or more string / string[] fields that should be queried against.
	QueryBy []string

	// The relative weight to give each query_by field when ranking results.
	// This can be used to boost fields in priority, when looking for matches.
	QueryByWeights []int

	// Whether to enable infix searching for each field, works the same as QueryByWeights
	Infix []string

	// If true or missing, indicates that the last word in the query should be treated as a prefix,
	// and not as a whole word.
	// This is necessary for building autocomplete and instant search interfaces.
	// Set this to false to disable prefix searching for all queried fields.
	//
	// You can also control the behavior of prefix search on a per field basis.
	// This API is so confusing...
	Prefix []bool

	// Filter conditions for refining your search results.
	FilterBy string

	// A list of numerical fields and their corresponding sort orders that will be used for ordering your results.
	// Up to 3 sort fields can be specified.
	SortBy []string

	// A list of fields that will be used for faceting your results on.
	FacetBy []string

	// Maximum number of facet values to be returned.
	MaxFacetValues int

	// Facet values that are returned can now be filtered via this parameter.
	// The matching facet text is also highlighted.
	FacetQuery string

	// By default, Typesense prioritizes documents whose field value matches exactly with the query.
	// Set this parameter to true to disable this behavior.
	NoPrioritizeExactMatch bool

	// Results from this specific page number would be fetched.
	Page int
	// Number of results to fetch per page.
	PerPage int

	// You can aggregate search results into groups or buckets by specify one or more group_by fields.
	// NOTE: To group on a particular field, it must be a faceted field.
	GroupBy []string

	// Maximum number of hits to be returned for every group.
	// If GroupLimit is set as K, only the top K hits in each group are returned in the response.
	GroupLimit int

	// list of fields from the document to include in the search result.
	IncludeFields []string
	// list of fields from the document to exclude in the search result.
	ExcludeFields []string

	// list of fields that should be highlighted with snippetting.
	// You can use this parameter to highlight fields that you don't query for, as well.
	// Default: all queried fields will be highlighted.
	HighlightFields []string

	// list of fields which should be highlighted fully without snippeting.
	// Default: all fields will be snippeted.
	HighlightFullFields []string

	// The number of tokens that should surround the highlighted text on each side.
	// Default: 4
	HighlightAffixNumTokens int

	// The start and end tag used for the highlighted snippets.
	HighlightStartTag *string
	HighlightEndTag   *string

	// Field values under this length will be fully highlighted, instead of showing a snippet of relevant portion.
	SnippetThreshold int

	// Maximum number of typographical errors (0, 1 or 2) that would be tolerated.
	NumTypos *int

	// If at least typo_tokens_threshold number of results are not found for a specific query,
	// Typesense will attempt to look for results with more typos until num_typos is reached
	// or enough results are found. Set to 0 to disable typo tolerance.
	// Default: 100
	TypoTokensThreshold *int

	// If at least drop_tokens_threshold number of results are not found for a specific query,
	// Typesense will attempt to drop tokens (words) in the query until enough results are found.
	// Tokens that have the least individual hits are dropped first.
	// Set to 0 to disable dropping of tokens.
	DropTokensThreshold *int

	// A list of records to unconditionally include in the search results at specific positions.
	// An example use case would be to feature or promote certain items on the top of search results.
	//
	// A comma separated list of record_id:hit_position.
	// Eg: to include a record with ID 123 at Position 1
	// and another record with ID 456 at Position 5, you'd specify 123:1,456:5.
	PinnedHits []string

	// A list of record_ids to unconditionally hide from search results.
	HiddenHits []string

	// If you have some overrides defined but want to disable all of them for a particular search query, set this to true.
	DisableOverrides bool

	// Set this parameter to true if you wish to split the search query into space separated words yourself.
	// When set to true, we will only split the search query by space, instead of using the locale-aware, built-in tokenizer.
	NoPreSegmentedQuery bool

	// Maximum number of hits that can be fetched from the collection. Eg: 200
	// page * per_page should be less than this number for the search request to return results.
	LimitHits int
}

SearchData is used in (*Client).Search

type SearchHit

type SearchHit struct {
	Document jsonutil.Raw `json:"document"`

	Highlights []Highlight `json:"highlights"`

	TextMatch int `json:"text_match"`
}

SearchHit is a single hit in SearchResult. Document is raw JSON data, call UnmarshalTo to unmarshal it to a struct, or Map to unmarshal it to a map[string]interface{}.

func (SearchHit) Map

func (s SearchHit) Map() (map[string]interface{}, error)

Map returns the Document as a map[string]interface{}.

func (SearchHit) UnmarshalTo

func (s SearchHit) UnmarshalTo(v interface{}) error

UnmarshalTo unmarshals m into v.

type SearchResult

type SearchResult struct {
	FacetCounts []int `json:"facet_counts"`

	// Number of found documents
	Found int `json:"found"`
	OutOf int `json:"out_of"`
	Page  int `json:"page"`

	// Search time in milliseconds
	SearchTime int `json:"search_time_ms"`

	Hits []SearchHit `json:"hits"`
}

SearchResult is the result returned from a search.

Directories

Path Synopsis
utils
jsonutil
Package jsonutil implements types and functions to make it easier to handle the JSON returned by Typesense.
Package jsonutil implements types and functions to make it easier to handle the JSON returned by Typesense.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL