Skip to content

Commit

Permalink
docs: mention the named parameters in om for no need to escape chars …
Browse files Browse the repository at this point in the history
…for queries
  • Loading branch information
rueian committed Aug 25, 2023
1 parent 8d1fc7f commit 6556f2f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions om/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type Example struct {
Key string `json:"key" redis:",key"` // the redis:",key" is required to indicate which field is the ULID key
Ver int64 `json:"ver" redis:",ver"` // the redis:",ver" is required to do optimistic locking to prevent lost update
Str string `json:"myStr"` // both NewHashRepository and NewJSONRepository use json tag as field name
Str string `json:"str"` // both NewHashRepository and NewJSONRepository use json tag as field name
}

func main() {
Expand Down Expand Up @@ -49,31 +49,31 @@ func main() {
If you have RediSearch, you can create and search the repository against the index.

```golang

if _, ok := repo.(*om.HashRepository[Example]); ok {
repo.CreateIndex(ctx, func(schema om.FtCreateSchema) rueidis.Completed {
return schema.FieldName("myStr").Text().Build() // Note that the Example.Str field is mapped to myStr on redis by its json tag
return schema.FieldName("str").Tag().Build() // Note that the Example.Str field is mapped to str on redis by its json tag
})
}

if _, ok := repo.(*om.JSONRepository[Example]); ok {
repo.CreateIndex(ctx, func(schema om.FtCreateSchema) rueidis.Completed {
return schema.FieldName("$.myStr").Text().Build() // the field name of json index should be a json path syntax
return schema.FieldName("$.str").As("str").Tag().Build() // the FieldName of a json index should be a json path syntax
})
}

exp := repo.NewEntity()
exp.Str = "foo"
exp.Str = "special_chars:[$.-]"
repo.Save(ctx, exp)

n, records, _ := repo.Search(ctx, func(search om.FtSearchIndex) rueidis.Completed {
return search.Query("foo").Build() // you have full query capability by building the command from om.FtSearchIndex
// Note that by using the named parameters with DIALECT >= 2, you won't have to escape chars for building queries.
return search.Query("@str:{$v}").Params().Nargs(2).NameValue().NameValue("v", exp.Str).Dialect(2).Build()
})

fmt.Println("total", n) // n is total number of results matched in redis, which is >= len(records)

for _, v := range records {
fmt.Println(v.Str) // print "foo"
fmt.Println(v.Str) // print "special_chars:[$.-]"
}
```

Expand Down

0 comments on commit 6556f2f

Please sign in to comment.