Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing SearchableAttribute to apply multiple times on one field #473

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Redis.OM/Modeling/SearchableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Redis.OM.Modeling
/// <summary>
/// Marks a field as searchable within a Redis Document.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public sealed class SearchableAttribute : SearchFieldAttribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Redis.OM.Modeling;

namespace Redis.OM.Unit.Tests.RediSearchTests;

[Document(StorageType = StorageType.Json)]
public class ObjectWithMultipleSearchableAttributes
{
[RedisIdField] public string Id { get; set; }

[Searchable(JsonPath = "$.City")]
[Searchable(JsonPath = "$.State")]
public Address Address { get; set; }
}
18 changes: 18 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,5 +1227,23 @@ public void SaveDateTimeOffset()
Assert.Equal(intermediate.Offset,final.Offset);
Assert.Equal(intermediate.DateTime,final.DateTime);
}

[Fact]
public void TestMultipleSearchAttributesOnEmbeddedDoc()
{
var obj = new ObjectWithMultipleSearchableAttributes()
{
Address = new Address
{
City = "Long Beach Island",
State = "New Jersey"
}
};

var collection = new RedisCollection<ObjectWithMultipleSearchableAttributes>(_connection);
collection.Insert(obj);
var res = collection.First(x => x.Address.City == "Long" && x.Address.State == "New");
Assert.Equal(obj.Id, res.Id);
}
}
}
32 changes: 32 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3839,5 +3839,37 @@ public void TestBasicQueryCastToIQueryable()
"0",
"1234");
}

[Fact]
public async Task TestIndexCreationWIthMultipleSearchFieldsForEmbeddedDoc()
{
_substitute.ExecuteAsync("FT.CREATE", Arg.Any<object[]>()).Returns("OK");
await _substitute.CreateIndexAsync(typeof(ObjectWithMultipleSearchableAttributes));
await _substitute.Received().ExecuteAsync("FT.CREATE",
"objectwithmultiplesearchableattributes-idx",
"ON",
"Json",
"PREFIX",
"1",
"Redis.OM.Unit.Tests.RediSearchTests.ObjectWithMultipleSearchableAttributes:",
"SCHEMA",
"$.Address.City", "AS", "Address_City", "TEXT",
"$.Address.State", "AS", "Address_State", "TEXT");
}

[Fact]
public async Task TestMultipleTextSearchOnEmbeddedDoc()
{
_substitute.ClearSubstitute();
_substitute.ExecuteAsync(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);
var collection = new RedisCollection<ObjectWithMultipleSearchableAttributes>(_substitute);
await collection.Where(x => x.Address.City == "Long" && x.Address.State == "New").ToListAsync();
await _substitute.Received().ExecuteAsync("FT.SEARCH",
"objectwithmultiplesearchableattributes-idx",
"((@Address_City:\"Long\") (@Address_State:\"New\"))",
"LIMIT",
"0",
"100");
}
}
}
2 changes: 2 additions & 0 deletions test/Redis.OM.Unit.Tests/RedisSetupCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public RedisSetup()
Connection.CreateIndex(typeof(BasicJsonObjectTestSave));
Connection.CreateIndex(typeof(SelectTestObject));
Connection.CreateIndex(typeof(ObjectWithDateTimeOffsetJson));
Connection.CreateIndex(typeof(ObjectWithMultipleSearchableAttributes));
}

private IRedisConnectionProvider _provider;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void Dispose()
Connection.DropIndexAndAssociatedRecords(typeof(BasicJsonObjectTestSave));
Connection.DropIndexAndAssociatedRecords(typeof(SelectTestObject));
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTimeOffsetJson));
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithMultipleSearchableAttributes));
}
}
}
Loading