Documentation
¶
Overview ¶
Package tsclient is a client library for the Typesense search engine.
Index ¶
- Constants
- func WithBody(r io.Reader) func(*http.Request) error
- func WithHeader(header http.Header) func(*http.Request) error
- func WithJSONBody(v interface{}) func(*http.Request) error
- func WithURLValues(values url.Values) func(*http.Request) error
- type Client
- func (c *Client) Collection(name string) (col Collection, err error)
- func (c *Client) Collections() (cols []Collection, err error)
- func (c *Client) CreateCollection(name, defaultSortingField string, fields []CreateFieldData) (col Collection, err error)
- func (c *Client) DeleteCollection(name string) (col Collection, err error)
- func (c *Client) DeleteDocument(collection, id string, out interface{}) error
- func (c *Client) DeleteQuery(collection, filter string, batchSize int) (deleted int, err error)
- func (c *Client) Document(collection, id string, out interface{}) (string, error)
- func (c *Client) Health() (ok bool, err error)
- func (c *Client) Import(collection, action string, s interface{}) (ok []bool, err error)
- func (c *Client) ImportSlice(collection, action string, s []interface{}) (ok []bool, err error)
- func (c *Client) Insert(collection string, doc interface{}, out interface{}) (err error)
- func (c *Client) Request(method, endpoint string, opts ...RequestOption) (response []byte, err error)
- func (c *Client) Search(collection string, data SearchData) (res SearchResult, err error)
- func (c *Client) UpdateDocument(collection, id string, doc, out interface{}) error
- func (c *Client) Upsert(collection string, doc interface{}, out interface{}) (err error)
- type Collection
- type CreateFieldData
- type Field
- type Highlight
- type RequestOption
- type SearchData
- type SearchHit
- type SearchResult
Constants ¶
const ( ErrBadRequest = errors.Sentinel("400 bad request") ErrNotFound = errors.Sentinel("404 not found") ErrAlreadyExists = errors.Sentinel("409 resource already exists") ErrUnprocessable = errors.Sentinel("422 unprocessable entity") )
Errors returned by Request
const ErrNotSlice = errors.Sentinel("slice expected")
ErrNotSlice is returned by Import if a type other than a slice is given as input.
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 WithHeader ¶
WithHeader adds headers to the request.
func WithJSONBody ¶
WithJSONBody adds a JSON body to the request. Marshaling errors are ignored.
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 (*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 ¶
DeleteDocument deletes a document in the collection. The deleted document is unmarshaled to `out` if it is not nil.
func (*Client) DeleteQuery ¶
DeleteQuery deletes documents in the collection matching filter. Returns the number of deleted documents.
func (*Client) Document ¶
Document retrieves a document from the collection by ID. The document is unmarshaled to `out` if it is not nil.
func (*Client) Import ¶
Import imports the documents into the collection. It returns an error if s is not a slice.
func (*Client) ImportSlice ¶
ImportSlice imports a slice of documents into the collection.
func (*Client) Insert ¶
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 ¶
UpdateDocument updates a document in the collection by ID. The updated 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 ¶
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) UnmarshalTo ¶
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.