modulepackage
0.2.4
Repository: https://github.com/gustavotero7/redisearch.git
Documentation: pkg.go.dev
# README
Redisearch go client
Init client
search := redisearch.New(&redis.Options{
Network: "tcp",
Addr: "redisAddress",
Password: "redisPassword",
DB: 0,
MaxRetries: 5,
})
Create Index
err := search.CreateIndex(context.Background(), redisearch.IndexOptions{
IndexName: "cities",
Prefix: []string{"city:"},
Schema: map[string]redisearch.FieldSchema{
"name": {
Type: redisearch.FieldTypeText,
Options: []redisearch.SchemaOpt{
redisearch.SchemaOptWeight(2.0),
},
},
"tags": {
Type: redisearch.FieldTypeTag,
Options: []redisearch.SchemaOpt{
redisearch.SchemaOptTagSeparator(','),
},
},
"population": {
Type: redisearch.FieldTypeNumeric,
Options: []redisearch.SchemaOpt{
redisearch.SchemaOptSortable(),
},
},
},
}, true) // warning, if 2nd argument is TRUE, all data matching the index prefix will be deleted from redis
if err != nil {
println("got error: ", err.Error())
return
}
println("index created")
Add item to index
// warning: if 3rd argument (override) is true, existing key/val will be deleted BEFORE writing new value to redis
err = search.Add(ctx, "city:popayan", map[string]interface{}{
"name": "Popayan",
"tags": "colombia,cauca",
"population": 320000,
}, false)
if err != nil {
println("got error: ", err.Error())
return
}
println("value successfully added")
// Note: This is a wrapper of HSET command, and it's usage is optional
Search
// Search results can be parsed in a list of structs or maps
// When using structs,the field names to parse from redis are taken from json tags (if set)
// by default field names will be taken from exported struct fields
var out []struct {
Name string `json:"name"`
Tags string `json:"tags"`
Population int `json:"population"`
}
res, err := search.Search(ctx, redisearch.SearchOptions{
IndexName: "cities",
Query: "Popayan",
}, &out)
if err != nil {
println("got error: ", err.Error())
return
}
fmt.Printf("search results: %+v", res)
// Search storing results in map list
var outMap []map[string]string
res, err = search.Search(ctx, redisearch.SearchOptions{
IndexName: "cities",
Query: "Popayan",
}, &outMap)
if err != nil {
println("got error: ", err.Error())
return
}
fmt.Printf("search results: %+v", res)
Drop index
// Remove the given index from redisearch.
// if purgeIndexData (last argument) is true, all the data(sets) related to the index will be deleted from redis
err = search.DropIndex(ctx, "cities", false)
if err != nil {
println("got error: ", err.Error())
return
}
println("index deleted")
Full example
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/gustavotero7/redisearch"
)
func main() {
// Init client
search := redisearch.New(&redis.Options{
Network: "tcp",
Addr: "redisAddress",
Password: "redisPassword",
DB: 0,
MaxRetries: 5,
})
ctx := context.Background()
err := search.CreateIndex(ctx, redisearch.IndexOptions{
IndexName: "cities",
Prefix: []string{"city:"},
Schema: map[string]redisearch.FieldSchema{
"name": {
Type: redisearch.FieldTypeText,
Options: []redisearch.SchemaOpt{
redisearch.SchemaOptWeight(2.0),
},
},
"tags": {
Type: redisearch.FieldTypeTag,
Options: []redisearch.SchemaOpt{
redisearch.SchemaOptTagSeparator(','),
},
},
"population": {
Type: redisearch.FieldTypeNumeric,
Options: []redisearch.SchemaOpt{
redisearch.SchemaOptSortable(),
},
},
},
}, true) // warning, if 2nd argument is TRUE, all data matching the index prefix will be deleted from redis
if err != nil {
println("got error: ", err.Error())
return
}
println("index created")
// Add new item to redis
// warning: if 3rd argument (override) is true, existing key/val will be deleted BEFORE writing new value to redis
err = search.Add(ctx, "city:popayan", map[string]interface{}{
"name": "Popayan",
"tags": "colombia,cauca",
"population": 320000,
}, false)
if err != nil {
println("got error: ", err.Error())
return
}
println("value successfully added")
// Search storing results in struct list
var out []struct {
Name string `json:"name"`
Tags string `json:"tags"`
Population int `json:"population"`
}
res, err := search.Search(ctx, redisearch.SearchOptions{
IndexName: "cities",
Query: "Popayan",
}, &out)
if err != nil {
println("got error: ", err.Error())
return
}
fmt.Printf("search results: %+v", res)
// Search storing results in map list
var outMap []map[string]string
res, err = search.Search(ctx, redisearch.SearchOptions{
IndexName: "cities",
Query: "Popayan",
}, &outMap)
if err != nil {
println("got error: ", err.Error())
return
}
fmt.Printf("search results: %+v", res)
// Drop existing index
// warning, if 2nd argument is TRUE, all data matching the index prefix will be deleted from redis
err = search.DropIndex(ctx, "cities", false)
if err != nil {
println("got error: ", err.Error())
return
}
println("index deleted")
}
TODO
- Update parser to support search flags that modifies the number of returned items in search response (NOCONTENT, WITHSCORES, WITHPAYLOADS, WITHSORTKEYS)
- Query builder
# Functions
New return a new redisearch implementation instance.
SchemaOptNoIndex Fields can have the NOINDEX option, which means they will not be indexed.
SchemaOptNoStem Text fields can have the NOSTEM argument which will disable stemming when indexing its values.
SchemaOptPhonetic Declaring a text field as PHONETIC will perform phonetic matching on it in searches by default.
SchemaOptSortable Numeric, tag or text fields can have the optional SORTABLE argument that allows the user to later sort the results by the value of this field (this adds memory overhead so do not declare it on large text fields).
SchemaOptTagSeparator or TAG fields, indicates how the text contained in the field is to be split into individual tags.
SchemaOptWeight For TEXT fields, declares the importance of this field when calculating result accuracy.
# Constants
FieldTypeGeo Allows geographic range queries against the value in this field.
FieldTypeNumeric Allows numeric range queries against the value in this field.
FieldTypeTag Allows exact-match queries, such as categories or primary keys, against the value in this field.
FieldTypeText Allows full-text search queries against the value in this field.
IndexFlagMaxTextFields For efficiency, RediSearch encodes indexes differently if they are created with less than 32 text fields.
IndexFlagNoFields If set, we do not store field bits for each term.
IndexFlagNoFreqs If set, we avoid saving the term frequencies in the index.
IndexFlagNoHl Conserves storage space and memory by disabling highlighting support.
IndexFlagNoOffsets If set, we do not store term offsets for documents (saves memory, does not allow exact searches or highlighting).
IndexFlagSkipInitialScan If set, we do not scan and index.
SearchFlagNoStopWords If set, we do not filter stopwords from the query.
SearchFlagVerbatim if set, we do not try to use stemming for query expansion but search the query terms verbatim.
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RediSearch implements Client.
No description provided by the author
No description provided by the author
No description provided by the author
# Interfaces
Client hold basic methods to interact with redisearch module for redis.
# Type aliases
No description provided by the author