From 0d15c28e4944c0b8c5a8e6aa70765fa6be887229 Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Sun, 9 Aug 2020 13:08:50 +0100 Subject: [PATCH] [add] Added ExampleClient_CreateIndexWithIndexDefinition. Deprecated AddHash. Deprecated SynAdd. --- README.md | 2 -- redisearch/client.go | 3 ++ redisearch/document.go | 1 + redisearch/example_client_test.go | 46 +++++++++++++++++++++++++++++++ redisearch/index.go | 2 ++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 335c58c..565e890 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,6 @@ func ExampleClient() { | :--- | ----: | | [FT.CREATE](https://oss.redislabs.com/redisearch/Commands.html#ftcreate) | [CreateIndex](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.CreateIndex) | | [FT.ADD](https://oss.redislabs.com/redisearch/Commands.html#ftadd) | [IndexOptions](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.IndexOptions) | -| [FT.ADDHASH](https://oss.redislabs.com/redisearch/Commands.html#ftaddhash) | [AddHash](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.AddHash) | | [FT.ALTER](https://oss.redislabs.com/redisearch/Commands.html#ftalter) | [AddField](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.AddField) | | [FT.ALIASADD](https://oss.redislabs.com/redisearch/Commands.html#ftaliasadd) | [AliasAdd](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.AliasAdd) | | [FT.ALIASUPDATE](https://oss.redislabs.com/redisearch/Commands.html#ftaliasupdate) | [AliasUpdate](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.AliasUpdate) | @@ -97,7 +96,6 @@ func ExampleClient() { | [FT.SUGGET](https://oss.redislabs.com/redisearch/Commands.html#ftsugget) | [SuggestOpts](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Autocompleter.SuggestOpts) | | [FT.SUGDEL](https://oss.redislabs.com/redisearch/Commands.html#ftsugdel) | [DeleteTerms](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Autocompleter.DeleteTerms) | | [FT.SUGLEN](https://oss.redislabs.com/redisearch/Commands.html#ftsuglen) | [Autocompleter.Length](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Autocompleter.Length) | -| [FT.SYNADD](https://oss.redislabs.com/redisearch/Commands.html#ftsynadd) | [SynAdd](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.SynAdd) | | [FT.SYNUPDATE](https://oss.redislabs.com/redisearch/Commands.html#ftsynupdate) | [SynUpdate](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.SynUpdate) | | [FT.SYNDUMP](https://oss.redislabs.com/redisearch/Commands.html#ftsyndump) | [SynDump](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.SynDump) | | [FT.SPELLCHECK](https://oss.redislabs.com/redisearch/Commands.html#ftspellcheck) | [SpellCheck](https://godoc.org/github.com/RediSearch/redisearch-go/redisearch#Client.SpellCheck) | diff --git a/redisearch/client.go b/redisearch/client.go index cda3274..645e20f 100644 --- a/redisearch/client.go +++ b/redisearch/client.go @@ -575,6 +575,7 @@ func (i *Client) GetTagVals(index string, filedName string) ([]string, error) { } // Adds a synonym group. +// Deprecated: This function is not longer supported on RediSearch 2.0 and above, use SynUpdate instead func (i *Client) SynAdd(indexName string, terms []string) (int64, error) { conn := i.pool.Get() defer conn.Close() @@ -621,6 +622,8 @@ func (i *Client) SynDump(indexName string) (map[string][]int64, error) { } // Adds a document to the index from an existing HASH key in Redis. +// Deprecated: This function is not longer supported on RediSearch 2.0 and above, use HSET instead +// See the example ExampleClient_CreateIndexWithIndexDefinition for a deeper understanding on how to move towards using hashes on your application func (i *Client) AddHash(docId string, score float32, language string, replace bool) (string, error) { conn := i.pool.Get() defer conn.Close() diff --git a/redisearch/document.go b/redisearch/document.go index e39b9bb..1801571 100644 --- a/redisearch/document.go +++ b/redisearch/document.go @@ -27,6 +27,7 @@ type IndexingOptions struct { Language string // If set to true, we will not save the actual document in the database and only index it. + // As of RediSearch 2.0 and above NOSAVE is no longer supported, and will have no effect NoSave bool // If set, we will do an UPSERT style insertion - and delete an older version of the document if it exists. diff --git a/redisearch/example_client_test.go b/redisearch/example_client_test.go index 20616ca..dd93ea8 100644 --- a/redisearch/example_client_test.go +++ b/redisearch/example_client_test.go @@ -63,6 +63,52 @@ func ExampleNewClient() { c.Drop() } +// RediSearch 2.0, marks the re-architecture of the way indices are kept in sync with the data. +// Instead of having to write data through the index (using the FT.ADD command), +// RediSearch will now follow the data written in hashes and automatically index it. +// The following example illustrates how to achieve it with the go client +func ExampleClient_CreateIndexWithIndexDefinition() { + host := "localhost:6379" + password := "" + pool := &redis.Pool{Dial: func() (redis.Conn, error) { + return redis.Dial("tcp", host, redis.DialPassword(password)) + }} + c := redisearch.NewClientFromPool(pool, "products-from-hashes") + // Drop an existing index. If the index does not exist an error is returned + c.Drop() + + // Create a schema + schema := redisearch.NewSchema(redisearch.DefaultOptions). + AddField(redisearch.NewTextFieldOptions("name", redisearch.TextFieldOptions{Sortable: true})). + AddField(redisearch.NewTextFieldOptions("description", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})). + AddField(redisearch.NewNumericField("price")) + + // Create a index definition for automatic indexing on Hash updates. + // In this example we will only index keys started by product: + indexDefinition := redisearch.NewIndexDefinition().AddPrefix("product:") + + // Add the Index Definition + c.CreateIndexWithIndexDefinition(schema, indexDefinition) + + // Get a vanilla connection and create 100 hashes + vanillaConnection := pool.Get() + for productNumber := 0; productNumber < 100; productNumber++ { + vanillaConnection.Do("HSET", fmt.Sprintf("product:%d", productNumber), "name", fmt.Sprintf("product name %d", productNumber), "description", "product description", "price", 10.99) + } + + // Wait for all documents to be indexed + info, _ := c.Info() + for info.IsIndexing { + time.Sleep(time.Second) + info, _ = c.Info() + } + + _, total, _ := c.Search(redisearch.NewQuery("description")) + + fmt.Printf("Total documents containing \"description\": %d.\n", total) + // Output: Total documents containing "description": 100. +} + // exemplifies the NewClientFromPool function func ExampleNewClientFromPool() { host := "localhost:6379" diff --git a/redisearch/index.go b/redisearch/index.go index e1f6ab5..3fffae2 100644 --- a/redisearch/index.go +++ b/redisearch/index.go @@ -137,6 +137,8 @@ func (defintion *IndexDefinition) Serialize(args redis.Args) redis.Args { func SerializeIndexingOptions(opts IndexingOptions, args redis.Args) redis.Args { // apply options + + // As of RediSearch 2.0 and above NOSAVE is no longer supported. if opts.NoSave { args = append(args, "NOSAVE") }