Documentation ¶
Overview ¶
Package redisearch provides a Go client for the RediSearch search engine.
For the full documentation of RediSearch, see [http://redisearch.io](http://redisearch.io)
Example Usage ¶
```go
import ( "github.com/RedisLabs/redisearch-go/redisearch" "log" "fmt" ) func ExampleClient() { // Create a client. By default a client is schemaless // unless a schema is provided when creating the index c := createClient("myIndex") // Create a schema sc := redisearch.NewSchema(redisearch.DefaultOptions). AddField(redisearch.NewTextField("body")). AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})). AddField(redisearch.NewNumericField("date")) // Drop an existing index. If the index does not exist an error is returned c.Drop() // Create the index with the given schema if err := c.CreateIndex(sc); err != nil { log.Fatal(err) } // Create a document with an id and given score doc := redisearch.NewDocument("doc1", 1.0) doc.Set("title", "Hello world"). Set("body", "foo bar"). Set("date", time.Now().Unix()) // Index the document. The API accepts multiple documents at a time if err := c.IndexOptions(redisearch.DefaultIndexingOptions, doc); err != nil { log.Fatal(err) } // Searching with limit and sorting docs, total, err := c.Search(redisearch.NewQuery("hello world"). Limit(0, 2). SetReturnFields("title")) fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err) // Output: doc1 Hello world 1 <nil> }
```
Index ¶
- Variables
- type Autocompleter
- type Client
- func (i *Client) CreateIndex(s *Schema) error
- func (i *Client) Drop() error
- func (i *Client) Explain(q *Query) (string, error)
- func (i *Client) Index(docs ...Document) error
- func (i *Client) IndexOptions(opts IndexingOptions, docs ...Document) error
- func (i *Client) Info() (*IndexInfo, error)
- func (i *Client) Search(q *Query) (docs []Document, total int, err error)
- type ConnPool
- type Document
- type DocumentList
- type Field
- func NewNumericField(name string) Field
- func NewNumericFieldOptions(name string, options NumericFieldOptions) Field
- func NewSortableNumericField(name string) Field
- func NewSortableTextField(name string, weight float32) Field
- func NewTagField(name string) Field
- func NewTagFieldOptions(name string, opts TagFieldOptions) Field
- func NewTextField(name string) Field
- func NewTextFieldOptions(name string, opts TextFieldOptions) Field
- type FieldType
- type Flag
- type HighlightOptions
- type IndexInfo
- type IndexingOptions
- type MultiError
- type MultiHostPool
- type NumericFieldOptions
- type Operator
- type Options
- type Paging
- type Predicate
- func Equals(property string, value interface{}) Predicate
- func GreaterThan(property string, value interface{}) Predicate
- func GreaterThanEquals(property string, value interface{}) Predicate
- func InRange(property string, min, max interface{}, inclusive bool) Predicate
- func LessThan(property string, value interface{}) Predicate
- func LessThanEquals(property string, value interface{}) Predicate
- func NewPredicate(property string, operator Operator, values ...interface{}) Predicate
- type Query
- func (q *Query) Highlight(fields []string, openTag, closeTag string) *Query
- func (q *Query) Limit(offset, num int) *Query
- func (q *Query) SetExpander(exp string) *Query
- func (q *Query) SetFlags(flags Flag) *Query
- func (q *Query) SetInKeys(keys ...string) *Query
- func (q *Query) SetLanguage(lang string) *Query
- func (q *Query) SetPayload(payload []byte) *Query
- func (q *Query) SetReturnFields(fields ...string) *Query
- func (q *Query) SetScorer(scorer string) *Query
- func (q *Query) SetSortBy(field string, ascending bool) *Query
- func (q *Query) Summarize(fields ...string) *Query
- func (q *Query) SummarizeOptions(opts SummaryOptions) *Query
- type Schema
- type SingleHostPool
- type SortingKey
- type SuggestOptions
- type Suggestion
- type SuggestionList
- type SummaryOptions
- type TagFieldOptions
- type TextFieldOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultIndexingOptions = IndexingOptions{ Language: "", NoSave: false, Replace: false, Partial: false, }
DefaultIndexingOptions are the default options for document indexing
var DefaultOptions = Options{ NoSave: false, NoFieldFlags: false, NoFrequencies: false, NoOffsetVectors: false, Stopwords: nil, }
DefaultOptions represents the default options
Functions ¶
This section is empty.
Types ¶
type Autocompleter ¶
type Autocompleter struct {
// contains filtered or unexported fields
}
Autocompleter implements a redisearch auto-completer API
func NewAutocompleter ¶
func NewAutocompleter(addr, name string) *Autocompleter
NewAutocompleter creates a new Autocompleter with the given host and key name
func (*Autocompleter) AddTerms ¶
func (a *Autocompleter) AddTerms(terms ...Suggestion) error
AddTerms pushes new term suggestions to the index
func (*Autocompleter) Delete ¶
func (a *Autocompleter) Delete() error
Delete deletes the Autocompleter key for this AC
func (*Autocompleter) Suggest
deprecated
func (a *Autocompleter) Suggest(prefix string, num int, fuzzy bool) ([]Suggestion, error)
Suggest gets completion suggestions from the Autocompleter dictionary to the given prefix. If fuzzy is set, we also complete for prefixes that are in 1 Levenshten distance from the given prefix
Deprecated: Please use SuggestOpts() instead
func (*Autocompleter) SuggestOpts ¶
func (a *Autocompleter) SuggestOpts(prefix string, opts SuggestOptions) ([]Suggestion, error)
SuggestOpts gets completion suggestions from the Autocompleter dictionary to the given prefix. SuggestOptions are passed allowing you specify if the returned values contain a payload, and scores. If SuggestOptions.Fuzzy is set, we also complete for prefixes that are in 1 Levenshten distance from the given prefix
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an interface to redisearch's redis commands
Example ¶
package main import ( "fmt" "log" "os" "time" "github.com/RedisLabs/redisearch-go/redisearch" ) func createClient(indexName string) *redisearch.Client { value, exists := os.LookupEnv("REDISEARCH_TEST_HOST") host := "localhost:6379" if exists && value != "" { host = value } return redisearch.NewClient(host, indexName) } func main() { // Create a client. By default a client is schemaless // unless a schema is provided when creating the index c := createClient("myIndex") // Create a schema sc := redisearch.NewSchema(redisearch.DefaultOptions). AddField(redisearch.NewTextField("body")). AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})). AddField(redisearch.NewNumericField("date")) // Drop an existing index. If the index does not exist an error is returned c.Drop() // Create the index with the given schema if err := c.CreateIndex(sc); err != nil { log.Fatal(err) } // Create a document with an id and given score doc := redisearch.NewDocument("doc1", 1.0) doc.Set("title", "Hello world"). Set("body", "foo bar"). Set("date", time.Now().Unix()) // Index the document. The API accepts multiple documents at a time if err := c.IndexOptions(redisearch.DefaultIndexingOptions, doc); err != nil { log.Fatal(err) } // Searching with limit and sorting docs, total, err := c.Search(redisearch.NewQuery("hello world"). Limit(0, 2). SetReturnFields("title")) fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err) }
Output: doc1 Hello world 1 <nil>
func NewClient ¶
NewClient creates a new client connecting to the redis host, and using the given name as key prefix. Addr can be a single host:port pair, or a comma separated list of host:port,host:port... In the case of multiple hosts we create a multi-pool and select connections at random
func (*Client) CreateIndex ¶
CreateIndex configues the index and creates it on redis
func (*Client) Drop ¶
Drop the Currentl just flushes the DB - note that this will delete EVERYTHING on the redis instance
func (*Client) IndexOptions ¶
func (i *Client) IndexOptions(opts IndexingOptions, docs ...Document) error
IndexOptions indexes multiple documents on the index, with optional Options passed to options
type Document ¶
Document represents a single document to be indexed or returned from a query. Besides a score and id, the Properties are completely arbitrary
func NewDocument ¶
NewDocument creates a document with the specific id and score
func (*Document) EstimateSize ¶
func (*Document) SetPayload ¶
SetPayload Sets the document payload
type DocumentList ¶
type DocumentList []Document
DocumentList is used to sort documents by descending score
func (DocumentList) Len ¶
func (l DocumentList) Len() int
func (DocumentList) Less ¶
func (l DocumentList) Less(i, j int) bool
func (DocumentList) Swap ¶
func (l DocumentList) Swap(i, j int)
type Field ¶
Field represents a single field's Schema
func NewNumericField ¶
NewNumericField creates a new numeric field with the given name
func NewNumericFieldOptions ¶
func NewNumericFieldOptions(name string, options NumericFieldOptions) Field
NewNumericFieldOptions defines a numeric field with additional options
func NewSortableNumericField ¶
NewSortableNumericField creates a new numeric field with the given name and a sortable flag
func NewSortableTextField ¶
NewSortableTextField creates a text field with the sortable flag set
func NewTagField ¶
NewTagField creates a new text field with default options (separator: ,)
func NewTagFieldOptions ¶
func NewTagFieldOptions(name string, opts TagFieldOptions) Field
NewTagFieldOptions creates a new tag field with the given options
func NewTextField ¶
NewTextField creates a new text field with the given weight
func NewTextFieldOptions ¶
func NewTextFieldOptions(name string, opts TextFieldOptions) Field
NewTextFieldOptions creates a new text field with given options (weight/sortable)
type Flag ¶
type Flag uint64
Flag is a type for query flags
const ( // Treat the terms verbatim and do not perform expansion QueryVerbatim Flag = 0x1 // Do not load any content from the documents, return just IDs QueryNoContent Flag = 0x2 // Fetch document scores as well as IDs and fields QueryWithScores Flag = 0x4 // The query terms must appear in order in the document QueryInOrder Flag = 0x08 // Fetch document payloads as well as fields. See documentation for payloads on redisearch.io QueryWithPayloads Flag = 0x10 DefaultOffset = 0 DefaultNum = 10 )
Query Flags
type HighlightOptions ¶
HighlightOptions represents the options to higlight specific document fields. See http://redisearch.io/Highlight/
type IndexInfo ¶
type IndexInfo struct { Schema Schema Name string `redis:"index_name"` DocCount uint64 `redis:"num_docs"` RecordCount uint64 `redis:"num_records"` TermCount uint64 `redis:"num_terms"` MaxDocID uint64 `redis:"max_doc_id"` InvertedIndexSizeMB float64 `redis:"inverted_sz_mb"` OffsetVectorSizeMB float64 `redis:"offset_vector_sz_mb"` DocTableSizeMB float64 `redis:"doc_table_size_mb"` KeyTableSizeMB float64 `redis:"key_table_size_mb"` RecordsPerDocAvg float64 `redis:"records_per_doc_avg"` BytesPerRecordAvg float64 `redis:"bytes_per_record_avg"` OffsetsPerTermAvg float64 `redis:"offsets_per_term_avg"` OffsetBitsPerTermAvg float64 `redis:"offset_bits_per_record_avg"` }
IndexInfo - Structure showing information about an existing index
type IndexingOptions ¶
IndexingOptions represent the options for indexing a single document
type MultiError ¶
type MultiError []error
MultiError Represents one or more errors
func NewMultiError ¶
func NewMultiError(len int) MultiError
NewMultiError initializes a multierror with the given len, and all sub-errors set to nil
func (MultiError) Error ¶
func (e MultiError) Error() string
Error returns a string representation of the error, in this case it just chains all the sub errors if they are not nil
type MultiHostPool ¶
func NewMultiHostPool ¶
func NewMultiHostPool(hosts []string) *MultiHostPool
func (*MultiHostPool) Get ¶
func (p *MultiHostPool) Get() redis.Conn
type NumericFieldOptions ¶
NumericFieldOptions Options for numeric fields
type Options ¶
type Options struct { // If set, we will not save the documents contents, just index them, for fetching ids only NoSave bool NoFieldFlags bool NoFrequencies bool NoOffsetVectors bool Stopwords []string }
Options are flags passed to the the abstract Index call, which receives them as interface{}, allowing for implementation specific options
type Predicate ¶
func GreaterThan ¶
func GreaterThanEquals ¶
func LessThanEquals ¶
func NewPredicate ¶
type Query ¶
type Query struct { Raw string Paging Paging Flags Flag Slop int Filters []Predicate InKeys []string ReturnFields []string Language string Expander string Scorer string Payload []byte SortBy *SortingKey HighlightOpts *HighlightOptions SummarizeOpts *SummaryOptions }
Query is a single search query and all its parameters and predicates
func NewQuery ¶
NewQuery creates a new query for a given index with the given search term. For currently the index parameter is ignored
func (*Query) Highlight ¶
Highlight sets highighting on given fields. Highlighting marks all the query terms with the given open and close tags (i.e. <b> and </b> for HTML)
func (*Query) SetExpander ¶
SetExpander sets a custom user query expander to be used
func (*Query) SetInKeys ¶
SetInKeys sets the INKEYS argument of the query - limiting the search to a given set of IDs
func (*Query) SetLanguage ¶
SetLanguage sets the query language, used by the stemmer to expand the query
func (*Query) SetPayload ¶
SetPayload sets a binary payload to the query, that can be used by custom scoring functions
func (*Query) SetReturnFields ¶
SetReturnFields sets the fields that should be returned from each result. By default we return everything
func (*Query) SetScorer ¶
SetScorer sets an alternative scoring function to be used. The only pre-compiled supported one at the moment is DISMAX
func (*Query) Summarize ¶
Summarize sets summarization on the given list of fields. It will instruct the engine to extract the most relevant snippets from the fields and return them as the field content. This function works with the default values of the engine, and only sets the fields. There is a function that accepts all options - SummarizeOptions
func (*Query) SummarizeOptions ¶
func (q *Query) SummarizeOptions(opts SummaryOptions) *Query
SummarizeOptions sets summarization on the given list of fields. It will instruct the engine to extract the most relevant snippets from the fields and return them as the field content.
This function accepts advanced settings for snippet length, separators and number of snippets
type Schema ¶
Schema represents an index schema Schema, or how the index would treat documents sent to it.
type SingleHostPool ¶
func NewSingleHostPool ¶
func NewSingleHostPool(host string) *SingleHostPool
type SortingKey ¶
SortingKey represents the sorting option if the query needs to be sorted based on a sortable fields and not a ranking function. See http://redisearch.io/Sorting/
type SuggestOptions ¶
SuggestOptions are options which are passed when recieving suggestions from the Autocompleter
type Suggestion ¶
Suggestion is a single suggestion being added or received from the Autocompleter
type SuggestionList ¶
type SuggestionList []Suggestion
SuggestionList is a sortable list of suggestions returned from an engine
func (SuggestionList) Len ¶
func (l SuggestionList) Len() int
func (SuggestionList) Less ¶
func (l SuggestionList) Less(i, j int) bool
func (SuggestionList) Swap ¶
func (l SuggestionList) Swap(i, j int)
type SummaryOptions ¶
type SummaryOptions struct { Fields []string FragmentLen int // default 20 NumFragments int // default 3 Separator string // default "..." }
SummaryOptions represents the configuration used to create field summaries. See http://redisearch.io/Highlight/
type TagFieldOptions ¶
type TagFieldOptions struct { // Separator is the custom separator between tags. defaults to comma (,) Separator byte NoIndex bool Sortable bool }
TagFieldOptions options for indexing tag fields