Skip to content

Commit

Permalink
[add] Added ExampleClient_CreateIndexWithIndexDefinition. Deprecated …
Browse files Browse the repository at this point in the history
…AddHash. Deprecated SynAdd.
  • Loading branch information
filipecosta90 committed Aug 9, 2020
1 parent 55d65fa commit 0d15c28
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand All @@ -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) |
Expand Down
3 changes: 3 additions & 0 deletions redisearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions redisearch/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions redisearch/example_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions redisearch/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 0d15c28

Please sign in to comment.