Skip to content

Commit

Permalink
Allowing SearchableAttribute to apply multiple times on one field (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Jul 29, 2024
1 parent 893834a commit 7a35e47
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
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));
}
}
}

0 comments on commit 7a35e47

Please sign in to comment.