Redis 中文文档 Redis 中文文档
指南
redis.io (opens new window)
指南
redis.io (opens new window)
  • 关于
    • Redis 开源治理
    • Redis 发布周期
    • Redis 赞助商
  • 入门
  • 数据类型
  • Redis Stack
  • 命令
  • 手册
Integration Tests

RediSearch Go Client


Go client for RediSearch, based on redigo.

Installing


  1. ``` shell
  2. go get github.com/RediSearch/redisearch-go/redisearch
  3. ```

Usage Example


  1. ``` go
  2. package main
  3. import (
  4. "fmt"
  5. "log"
  6. "time"

  7. "github.com/RediSearch/redisearch-go/redisearch"
  8. )

  9. func ExampleClient() {

  10. // Create a client. By default a client is schemaless
  11. // unless a schema is provided when creating the index
  12. c := redisearch.NewClient("localhost:6379", "myIndex")

  13. // Create a schema
  14. sc := redisearch.NewSchema(redisearch.DefaultOptions).
  15.   AddField(redisearch.NewTextField("body")).
  16.   AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})).
  17.   AddField(redisearch.NewNumericField("date"))

  18. // Drop an existing index. If the index does not exist an error is returned
  19. c.Drop()

  20. // Create the index with the given schema
  21. if err := c.CreateIndex(sc); err != nil {
  22.   log.Fatal(err)
  23. }

  24. // Create a document with an id and given score
  25. doc := redisearch.NewDocument("doc1", 1.0)
  26. doc.Set("title", "Hello world").
  27.   Set("body", "foo bar").
  28.   Set("date", time.Now().Unix())

  29. // Index the document. The API accepts multiple documents at a time
  30. if err := c.Index([]redisearch.Document{doc}...); err != nil {
  31.   log.Fatal(err)
  32. }

  33. // Searching with limit and sorting
  34. docs, total, err := c.Search(redisearch.NewQuery("hello world").
  35.   Limit(0, 2).
  36.   SetReturnFields("title"))

  37. fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err)
  38. // Output: doc1 Hello world 1
  39. }
  40. ```

Supported RediSearch Commands


Command Recommended API and godoc
:--- :---
FT.CREATE CreateIndex
FT.ADD IndexOptions
FT.ALTER AddField
FT.ALIASADD AliasAdd
FT.ALIASUPDATE AliasUpdate
FT.ALIASDEL AliasDel
FT.INFO Info
FT.SEARCH Search
FT.AGGREGATE AggregateQuery
FT.CURSOR Aggregate + (*WithCursor option set to True)
FT.EXPLAIN Explain
FT.DEL DeleteDocument
FT.GET Get
FT.MGET MultiGet
FT.DROP Drop
FT.TAGVALS GetTagVals
FT.SUGADD AddTerms
FT.SUGGET SuggestOpts
FT.SUGDEL DeleteTerms
FT.SUGLEN Autocompleter.Length
FT.SYNUPDATE SynUpdate
FT.SYNDUMP SynDump
FT.SPELLCHECK SpellCheck
FT.DICTADD DictAdd
FT.DICTDEL DictDel
FT.DICTDUMP DictDump
FT.CONFIG SetConfig、GetConfig
Last Updated: 2023-09-03 19:17:54