From 9ed207a6310bbb912d884ba5216a0c839e63c5c4 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Mon, 15 Jul 2024 04:09:41 +0300 Subject: [PATCH 1/2] Support RediSearch empty values --- search_commands.go | 9 +++++ search_test.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/search_commands.go b/search_commands.go index 8214a570be..f5118c77e2 100644 --- a/search_commands.go +++ b/search_commands.go @@ -75,6 +75,8 @@ type FieldSchema struct { WithSuffixtrie bool VectorArgs *FTVectorArgs GeoShapeFieldType string + IndexEmpty bool + IndexMissing bool } type FTVectorArgs struct { @@ -1002,6 +1004,13 @@ func (c cmdable) FTCreate(ctx context.Context, index string, options *FTCreateOp if schema.WithSuffixtrie { args = append(args, "WITHSUFFIXTRIE") } + if schema.IndexEmpty { + args = append(args, "INDEXEMPTY") + } + if schema.IndexMissing { + args = append(args, "INDEXMISSING") + + } } cmd := NewStatusCmd(ctx, args...) _ = c(ctx, cmd) diff --git a/search_test.go b/search_test.go index 60888ef5c2..3d19492f65 100644 --- a/search_test.go +++ b/search_test.go @@ -1043,6 +1043,96 @@ var _ = Describe("RediSearch commands", Label("search"), func() { Expect(err).NotTo(HaveOccurred()) Expect(res2.Total).To(BeEquivalentTo(int64(2))) }) + + It("should search missing fields", Label("search", "ftcreate", "ftsearch"), func() { + val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{Prefix: []interface{}{"property:"}}, + &redis.FieldSchema{FieldName: "title", FieldType: redis.SearchFieldTypeText, Sortable: true}, + &redis.FieldSchema{FieldName: "features", FieldType: redis.SearchFieldTypeTag, IndexMissing: true}, + &redis.FieldSchema{FieldName: "description", FieldType: redis.SearchFieldTypeText, IndexMissing: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(BeEquivalentTo("OK")) + WaitForIndexing(client, "idx1") + + client.HSet(ctx, "property:1", map[string]interface{}{ + "title": "Luxury Villa in Malibu", + "features": "pool,sea view,modern", + "description": "A stunning modern villa overlooking the Pacific Ocean.", + }) + + client.HSet(ctx, "property:2", map[string]interface{}{ + "title": "Downtown Flat", + "description": "Modern flat in central Paris with easy access to metro.", + }) + + client.HSet(ctx, "property:3", map[string]interface{}{ + "title": "Beachfront Bungalow", + "features": "beachfront,sun deck", + }) + + res, err := client.FTSearchWithArgs(ctx, "idx1", "ismissing(@features)", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:2")) + + res, err = client.FTSearchWithArgs(ctx, "idx1", "-ismissing(@features)", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:1")) + Expect(res.Docs[1].ID).To(BeEquivalentTo("property:3")) + + res, err = client.FTSearchWithArgs(ctx, "idx1", "ismissing(@description)", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:3")) + + res, err = client.FTSearchWithArgs(ctx, "idx1", "-ismissing(@description)", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:1")) + Expect(res.Docs[1].ID).To(BeEquivalentTo("property:2")) + }) + + It("should search empty fields", Label("search", "ftcreate", "ftsearch"), func() { + val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{Prefix: []interface{}{"property:"}}, + &redis.FieldSchema{FieldName: "title", FieldType: redis.SearchFieldTypeText, Sortable: true}, + &redis.FieldSchema{FieldName: "features", FieldType: redis.SearchFieldTypeTag, IndexEmpty: true}, + &redis.FieldSchema{FieldName: "description", FieldType: redis.SearchFieldTypeText, IndexEmpty: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(BeEquivalentTo("OK")) + WaitForIndexing(client, "idx1") + + client.HSet(ctx, "property:1", map[string]interface{}{ + "title": "Luxury Villa in Malibu", + "features": "pool,sea view,modern", + "description": "A stunning modern villa overlooking the Pacific Ocean.", + }) + + client.HSet(ctx, "property:2", map[string]interface{}{ + "title": "Downtown Flat", + "features": "", + "description": "Modern flat in central Paris with easy access to metro.", + }) + + client.HSet(ctx, "property:3", map[string]interface{}{ + "title": "Beachfront Bungalow", + "features": "beachfront,sun deck", + "description": "", + }) + + res, err := client.FTSearchWithArgs(ctx, "idx1", "@features:{\"\"}", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:2")) + + res, err = client.FTSearchWithArgs(ctx, "idx1", "-@features:{\"\"}", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:1")) + Expect(res.Docs[1].ID).To(BeEquivalentTo("property:3")) + + res, err = client.FTSearchWithArgs(ctx, "idx1", "@description:''", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:3")) + + res, err = client.FTSearchWithArgs(ctx, "idx1", "-@description:''", &redis.FTSearchOptions{DialectVersion: 4, Return: []redis.FTSearchReturn{{FieldName: "id"}}, NoContent: true}).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(res.Docs[0].ID).To(BeEquivalentTo("property:1")) + Expect(res.Docs[1].ID).To(BeEquivalentTo("property:2")) + }) }) // It("should FTProfile Search and Aggregate", Label("search", "ftprofile"), func() { From 9d3b580feb978618dfb7328d8e1317bc98cd66c5 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Mon, 15 Jul 2024 10:51:09 +0300 Subject: [PATCH 2/2] Remove from enterprise --- search_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search_test.go b/search_test.go index 3d19492f65..d80aae0461 100644 --- a/search_test.go +++ b/search_test.go @@ -1044,7 +1044,7 @@ var _ = Describe("RediSearch commands", Label("search"), func() { Expect(res2.Total).To(BeEquivalentTo(int64(2))) }) - It("should search missing fields", Label("search", "ftcreate", "ftsearch"), func() { + It("should search missing fields", Label("search", "ftcreate", "ftsearch", "NonRedisEnterprise"), func() { val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{Prefix: []interface{}{"property:"}}, &redis.FieldSchema{FieldName: "title", FieldType: redis.SearchFieldTypeText, Sortable: true}, &redis.FieldSchema{FieldName: "features", FieldType: redis.SearchFieldTypeTag, IndexMissing: true}, @@ -1088,7 +1088,7 @@ var _ = Describe("RediSearch commands", Label("search"), func() { Expect(res.Docs[1].ID).To(BeEquivalentTo("property:2")) }) - It("should search empty fields", Label("search", "ftcreate", "ftsearch"), func() { + It("should search empty fields", Label("search", "ftcreate", "ftsearch", "NonRedisEnterprise"), func() { val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{Prefix: []interface{}{"property:"}}, &redis.FieldSchema{FieldName: "title", FieldType: redis.SearchFieldTypeText, Sortable: true}, &redis.FieldSchema{FieldName: "features", FieldType: redis.SearchFieldTypeTag, IndexEmpty: true},