redisearch

package
v0.0.0-...-269dc11 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2017 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultIndexingOptions = IndexingOptions{
	Language: "",
	NoSave:   false,
	Replace:  false,
}

DefaultIndexingOptions are the default options for document indexing

View Source
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, password, name string) *Autocompleter

NewAutocompleter creates a new Autocompleter with the given host and key name

func (*Autocompleter) AddTerms

func (a *Autocompleter) AddTerms(withPayload bool, 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

func (a *Autocompleter) Suggest(prefix string, num int, fuzzy bool, payload 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

type Client

type Client struct {
	// contains filtered or unexported fields
}

Cleint 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

func NewClient(addr, password, name string) *Client

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

func (i *Client) CreateIndex(s *Schema) error

CreateIndex configues the index and creates it on redis

func (*Client) Drop

func (i *Client) Drop() error

Drop the Currentl just flushes the DB - note that this will delete EVERYTHING on the redis instance

func (*Client) Index

func (i *Client) Index(docs ...Document) error

func (*Client) IndexOptions

func (i *Client) IndexOptions(opts IndexingOptions, docs ...Document) error

Index indexes multiple documents on the index, with optional Options passed to options

func (*Client) Info

func (i *Client) Info() (*IndexInfo, error)

Info - Get information about the index. This can also be used to check if the index exists

func (*Client) Search

func (i *Client) Search(q *Query) (docs []Document, total int, err error)

Search searches the index for the given query, and returns documents, the total number of results, or an error if something went wrong

type ConnPool

type ConnPool interface {
	Get() redis.Conn
}

type Document

type Document struct {
	Id         string
	Score      float32
	Payload    []byte
	Properties map[string]interface{}
}

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

func NewDocument(id string, score float32) Document

NewDocument creates a document with the specific id and score

func (*Document) EstimateSize

func (d *Document) EstimateSize() (sz int)

func (Document) Set

func (d Document) Set(name string, value interface{}) Document

Set sets a property and its value in the document

func (*Document) SetPayload

func (d *Document) SetPayload(payload []byte)

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) Sort

func (l DocumentList) Sort()

Sort the DocumentList

func (DocumentList) Swap

func (l DocumentList) Swap(i, j int)

type Field

type Field struct {
	Name     string
	Type     FieldType
	Sortable bool
	Options  interface{}
}

Field represents a single field's Schema

func NewNumericField

func NewNumericField(name string) Field

NewNumericField creates a new numeric field with the given name

func NewSortableNumericField

func NewSortableNumericField(name string) Field

NewSortableNumericField creates a new numeric field with the given name and a sortable flag

func NewSortableTextField

func NewSortableTextField(name string, weight float32) Field

NewSortableTextField creates a text field with the sortable flag set

func NewTextField

func NewTextField(name string) Field

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 FieldType

type FieldType int

FieldType is an enumeration of field/property types

const (
	// TextField full-text field
	TextField FieldType = iota

	// NumericField numeric range field
	NumericField

	// GeoField geo-indexed point field
	GeoField

	// ValueField as-as short text value to be hashed and indexed
	ValueField

	// NoIndexField A field that shuold not be indexed
	NoIndexField
)

type Flag

type Flag uint64

Flag is a type for query flags

const (
	QueryVerbatim     Flag = 0x1
	QueryNoContent    Flag = 0x2
	QueryWithScores   Flag = 0x4
	QueryInOrder      Flag = 0x08
	QueryWithPayloads Flag = 0x10

	DefaultOffset = 0
	DefaultNum    = 10
)

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

type IndexingOptions struct {
	Language string
	NoSave   bool
	Replace  bool
}

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

type MultiHostPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewMultiHostPool

func NewMultiHostPool(hosts []string) *MultiHostPool

func (*MultiHostPool) Get

func (p *MultiHostPool) Get() redis.Conn

type NumericFieldOptions

type NumericFieldOptions struct {
	Sortable bool
}

NumericFieldOptions Options for numeric fields

type Operator

type Operator string
const (
	Eq Operator = "="

	Gt  Operator = ">"
	Gte Operator = ">="

	Lt  Operator = "<"
	Lte Operator = "<="

	Between          Operator = "BETWEEN"
	BetweenInclusive Operator = "BETWEEEN_EXCLUSIVE"
)

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 Paging

type Paging struct {
	Offset int
	Num    int
}

Paging represents the offset paging of a search result

type Predicate

type Predicate struct {
	Property string
	Operator Operator
	Value    []interface{}
}

func Equals

func Equals(property string, value interface{}) Predicate

func GreaterThan

func GreaterThan(property string, value interface{}) Predicate

func GreaterThanEquals

func GreaterThanEquals(property string, value interface{}) Predicate

func InRange

func InRange(property string, min, max interface{}, inclusive bool) Predicate

func LessThan

func LessThan(property string, value interface{}) Predicate

func LessThanEquals

func LessThanEquals(property string, value interface{}) Predicate

func NewPredicate

func NewPredicate(property string, operator Operator, values ...interface{}) Predicate

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
}

Query is a single search query and all its parameters and predicates

func NewQuery

func NewQuery(raw string) *Query

NewQuery creates a new query for a given index with the given search term. For currently the index parameter is ignored

func (*Query) Limit

func (q *Query) Limit(offset, num int) *Query

Limit sets the paging offset and limit for the query

func (*Query) SetExpander

func (q *Query) SetExpander(exp string) *Query

func (*Query) SetFlags

func (q *Query) SetFlags(flags Flag) *Query

SetFlags sets the query's optional flags

func (*Query) SetInKeys

func (q *Query) SetInKeys(keys ...string) *Query

func (*Query) SetLanguage

func (q *Query) SetLanguage(lang string) *Query

func (*Query) SetPayload

func (q *Query) SetPayload(payload []byte) *Query

func (*Query) SetReturnFields

func (q *Query) SetReturnFields(fields ...string) *Query

func (*Query) SetScorer

func (q *Query) SetScorer(scorer string) *Query

func (*Query) SetSortBy

func (q *Query) SetSortBy(field string, ascending bool) *Query

type Schema

type Schema struct {
	Fields  []Field
	Options Options
}

Schema represents an index schema Schema, or how the index would treat documents sent to it.

func NewSchema

func NewSchema(opts Options) *Schema

NewSchema creates a new Schema object

func (*Schema) AddField

func (m *Schema) AddField(f Field) *Schema

AddField adds a field to the Schema object

type SingleHostPool

type SingleHostPool struct {
	*redis.Pool
}

func NewSingleHostPool

func NewSingleHostPool(host, password string) *SingleHostPool

type SortingKey

type SortingKey struct {
	Field     string
	Ascending bool
}

type Suggestion

type Suggestion struct {
	Term    string
	Score   float64
	Payload string
}

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) Sort

func (l SuggestionList) Sort()

Sort the SuggestionList

func (SuggestionList) Swap

func (l SuggestionList) Swap(i, j int)

type TextFieldOptions

type TextFieldOptions struct {
	Weight   float32
	Sortable bool
	NoStem   bool
}

TextFieldOptions Options for text fields - weight and stemming enabled/disabled.

Jump to

Keyboard shortcuts

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