RediSearch / redisearch-go

Go client for RediSearch
https://redisearch.io
BSD 3-Clause "New" or "Revised" License
298 stars 66 forks source link

How to create multiple schemas at the same time? #140

Closed shenqidebaozi closed 2 years ago

shenqidebaozi commented 2 years ago

When I use redisearch, I may use multiple schemas, but I find it seems impossible to create multiple schemas. He always prompts me index already exists. What should be the right way?

    targetClient := redisearch.NewClient(c.Redis.Addr, "target-task#target")
    // create schema
    targetSchema := redisearch.NewSchema(redisearch.DefaultOptions)
        // some code
    if err := targetClient.CreateIndexWithIndexDefinition(targetSchema, redisearch.NewIndexDefinition().AddPrefix("target:")); err != nil {
        fmt.Println(err)
    }
    groupClient := redisearch.NewClient(c.Redis.Addr, "target-task#group")
    groupSchema := redisearch.NewSchema(redisearch.DefaultOptions).
        // some code
    if err := groupClient.CreateIndexWithIndexDefinition(groupSchema,redisearch.NewIndexDefinition().AddPrefix("group:")); err != nil {
        fmt.Println(err)
    }
gkorland commented 2 years ago

@shenqidebaozi can you please share the full error you get?

shenqidebaozi commented 2 years ago

@gkorland When running to CreateIndexWithIndexDefinition, an exception index already exists will be thrown

shenqidebaozi commented 2 years ago

The effect you want to achieve is that there are target table and group table respectively. After different table data is written, full-text retrieval is carried out respectively

shenqidebaozi commented 2 years ago

I don't know if my usage is incorrect. Please help me have a look.

gkorland commented 2 years ago

I took your example and it worked for me without issues.

package main 
import (
        "fmt"
        "github.com/RediSearch/redisearch-go/redisearch"
)

func main() {
        targetClient := redisearch.NewClient("localhost:6379", "target-task#target")
        // create schema
        targetSchema := redisearch.NewSchema(redisearch.DefaultOptions).
                AddField(redisearch.NewTextField("body")).
                AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})).
                AddField(redisearch.NewNumericField("date"))

        if err := targetClient.CreateIndexWithIndexDefinition(targetSchema, redisearch.NewIndexDefinition().AddPrefix("target:")); err != nil {
                fmt.Println(err)
        }
        groupClient := redisearch.NewClient("localhost:6379", "target-task#group")
        groupSchema := redisearch.NewSchema(redisearch.DefaultOptions).
                AddField(redisearch.NewTextField("body")).
                AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})).
                AddField(redisearch.NewNumericField("date"))

        if err := groupClient.CreateIndexWithIndexDefinition(groupSchema,redisearch.NewIndexDefinition().AddPrefix("group:")); err != nil {
                fmt.Println(err)
        }
}

I even checked the monitor and it seems like the commands are sent as expected:

$ redis-cli 
127.0.0.1:6379> monitor
OK
1646230856.481580 [0 172.17.0.1:49340] "FT.CREATE" "target-task#target" "ON" "HASH" "PREFIX" "1" "target:" "SCHEMA" "body" "TEXT" "title" "TEXT" "WEIGHT" "5" "SORTABLE" "date" "NUMERIC"
1646230856.481805 [0 172.17.0.1:49342] "FT.CREATE" "target-task#group" "ON" "HASH" "PREFIX" "1" "group:" "SCHEMA" "body" "TEXT" "title" "TEXT" "WEIGHT" "5" "SORTABLE" "date" "NUMERIC"
gkorland commented 2 years ago

Are you sure the DB is clear without any index?

shenqidebaozi commented 2 years ago

Our code is almost the same, but the effect of running here is that after the target is executed, it will prompt "index already exists" when the group is executed.

shenqidebaozi commented 2 years ago

I'll try it again and see if it's different from you