go-redis FTSearch 返回空结果

问题描述 投票:0回答:1

我能够使用 go-redis 创建索引并加载数据。另外,我可以搜索,但只能通过 CLI 进行搜索。这意味着索引已正确创建并且可搜索。它返回正确的结果。

但是当我尝试以编程方式搜索数据时,它返回:

redis: 2024/07/26 19:27:10 pool.go:368: Conn has unread data
{Total:0 Docs:[]}

我使用以下示例:https://github.com/redis/go-redis/blob/master/search_test.go#L128-L138

参考代码:

// Create index
_, err := client.FTCreate(ctx, "txt", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "txt", FieldType: redis.SearchFieldTypeText}).Result()

// Add data
client.HSet(ctx, "doc1", "txt", "foo baz")
client.HSet(ctx, "doc2", "txt", "foo bar")

// Search
res, err := client.FTSearchWithArgs(ctx, "txt", "foo ~bar", &redis.FTSearchOptions{WithScores: true, Limit: 50}).Result()
if err != nil {
    fmt.Printf("\nerr: %v", err)
}

client.JSONSet
也是如此。谁能帮助我理解这里出了什么问题?

go redis go-redis ft.search
1个回答
0
投票

创建客户端时将协议更改为 2 - 以下代码有效:

package main

import (
    "context"
    "fmt"
    "github.com/redis/go-redis/v9"
)

var ctx = context.Background()

func main() {
    var client *redis.Client

    client = redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Protocol: 2, // this right here
    })

    _, err := client.FTCreate(ctx, "txt", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "txt", FieldType: redis.SearchFieldTypeText}).Result()
    if err != nil {
        fmt.Printf("\nerr: %v", err)
    }

    // Add data
    client.HSet(ctx, "doc1", "txt", "foo baz")
    client.HSet(ctx, "doc2", "txt", "foo bar")

    // Search
    res, err := client.FTSearchWithArgs(ctx, "txt", "foo ~bar", &redis.FTSearchOptions{WithScores: true, Limit: 50}).Result()
    if err != nil {
        fmt.Printf("\nerr: %v", err)
    } else {
        fmt.Printf("\nRetrieved :%d documents", len(res.Docs))
    }
}

当 go redis 客户端通过 RESP3(redis go 客户端的默认设置)与 Redis 交互时,它读取结果的方式似乎存在问题。

© www.soinside.com 2019 - 2024. All rights reserved.