goreds

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: MIT Imports: 2 Imported by: 0

README

goreds

Build Status GoDoc

goreds is a Go port of tj/reds for Node.js

About

via https://github.com/tj/reds#about

Currently reds strips stop words and applies the metaphone and porter stemmer algorithms to the remaining words before mapping the constants in Redis sets. For example the following text:

Tobi is a ferret and he only wants four dollars

Converts to the following constant map:

map[Tobi:TB ferret:FRT wants:WNTS four:FR dollars:DLRS]

This also means that phonetically similar words will match, for example "stefen", "stephen", "steven" and "stefan" all resolve to the constant "STFN". Reds takes this further and applies the porter stemming algorithm to "stem" words, for example "counts", and "counting" become "count".

Consider we have the following bodies of text:

Tobi really wants four dollars

For some reason tobi is always wanting four dollars

The following search query will then match both of these bodies, and "wanting", and "wants" both reduce to "want".

tobi wants four dollars

Example

redis, _ := redis.DialURL("redis://localhost:6379")
search := goreds.NewClient(redis, "namespace")

// index some text and assign to their id
search.Index("example index text", "1")
search.Index("example text being indexed for the sake of this example", "2")
search.Index("example testing with a lot more text", "3")
search.Index("index some more sample text data", "4")

// remove an item from the search index
search.Remove("2")

// query the search index that should match ids 1 and 4
ids, _ := search.Query("index text", goreds.AND)
fmt.Println(ids)
// Output: [4 1]

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Redis     redis.Conn
	Namespace string
}

Client is a goreds client that uses a Redis client and Namespace to perform searches

func NewClient

func NewClient(redis redis.Conn, namespace string) *Client

NewClient will create a new search client with the given redigo connection and namespace (defaults to "goreds"). You may create multiple clients with different namespaces for separate search indexes.

Example
package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"

	"blainsmith.com/go/goreds"
)

func main() {
	redis, _ := redis.DialURL("redis://localhost:6379")
	search := goreds.NewClient(redis, "namespace")

	// index some text and assign to their id
	search.Index("example index text", "1")
	search.Index("example text being indexed for the sake of this example", "2")
	search.Index("example testing with a lot more text", "3")
	search.Index("index some more sample text data", "4")

	// remove an item from the search index
	search.Remove("2")

	// query the search index that should match ids 1 and 4
	ids, _ := search.Query("index text", goreds.AND)
	fmt.Println(ids)
}
Output:

[4 1]

func (*Client) Index

func (client *Client) Index(text, id string) error

Index will store the `id` within the database and use the `text` as the searchable text

func (*Client) Query

func (client *Client) Query(text string, operator Operator) ([]string, error)

Query performs a search against the database and returns a slice of ids that match

func (*Client) Remove

func (client *Client) Remove(id string) error

Remove will delete the `id` from the database so it is not longer searchable

type Operator

type Operator string
const (
	AND Operator = "zinterstore"
	OR  Operator = "zunionstore"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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