Skip to content

Commit

Permalink
Supporting the creation of a JSON index from a given generic type.
Browse files Browse the repository at this point in the history
  • Loading branch information
aarnaout12 committed Nov 16, 2022
1 parent bd8f836 commit 999322a
Show file tree
Hide file tree
Showing 20 changed files with 532 additions and 1 deletion.
11 changes: 11 additions & 0 deletions RediSearchClient.SampleData/Dimensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace RediSearchClient.SampleData.TestingTypes
{
public class Dimensions
{
public float Length { get; set; }

public float Width { get; set; }

public float Height { get; set; }
}
}
31 changes: 31 additions & 0 deletions RediSearchClient.SampleData/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using RediSearchClient.Attributes;
using System;
using System.Collections.Generic;

namespace RediSearchClient.SampleData.TestingTypes
{
[DefaultNoIndex]
public class Product
{
[Tag]
[Index]
[Alias("id")]
public string Identifier { get; set; }

[Sortable]
public decimal Price { get; set; }

public string Meta { get; set; }

[SchemaIgnore]
public int Count { get; set; }

public Dimensions Dimensions { get; set; }

public ICollection<Seller> Sellers { get; set; }

public Product RelatedProduct { get; set; } //Will be ignored to avoid recursion

public DateTime DateAdded { get; set; }
}
}
53 changes: 52 additions & 1 deletion RediSearchClient.SampleData/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using System;
using System.Linq;
using NReJSON;
using RediSearchClient.SampleData.TestingTypes;
using System.Collections.Generic;
using RediSearchClient.Query;
using System.Text.Json;

using var muxr = ConnectionMultiplexer.Connect("localhost");
var db = muxr.GetDatabase();
Expand Down Expand Up @@ -178,4 +182,51 @@
, x => x.Text("$.Prizes[*].affiliations[*].country", "InstitutionCountry")
)
.Build()
);
);

var indexName = "product-data-index";

if (db.ListIndexes().Any(x => x == indexName))
{
db.DropIndex(indexName);
}

await db.CreateIndexAsync<Product>("product-doc:", indexName);

var product = new Product
{
Identifier = "testprod1",
Meta = "test meta",
Price = 10.3m,
Dimensions = new Dimensions
{
Width = 10,
Length = 11,
Height = 12
},
Sellers = new List<Seller>
{
new Seller
{
Name = "seller1",
Locations = new List<string> {"location1", "location2"}
},
new Seller
{
Name = "seller2",
Locations = new List<string> {"location3", "location4"}
}
},
DateAdded = DateTime.Now
};

await db.RediSearchJsonSetAsync(product, $"product-doc:{product.Identifier}");

var query = RediSearchQuery
.On(indexName)
.UsingQuery("@id:{ testprod1 }")
.Return("id", "Price", "$.Dimensions", "$.Sellers[*].Name", "$.Sellers[*].Locations", "DateAdded")
.Dialect(3)
.Build();

var queryResult = await db.SearchAsync(query);
13 changes: 13 additions & 0 deletions RediSearchClient.SampleData/Seller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace RediSearchClient.SampleData.TestingTypes
{
public class Seller
{
public string Name { get; set; }

public IEnumerable<Product> SoldProducts { get; set; } //Will be ignored to avoid recursion

public IEnumerable<string> Locations { get; set; }
}
}
14 changes: 14 additions & 0 deletions RediSearchClient/Attributes/AliasAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace RediSearchClient.Attributes
{
public sealed class AliasAttribute : Attribute
{
public string Name { get; private set; }

public AliasAttribute(string name)
{
Name = name;
}
}
}
12 changes: 12 additions & 0 deletions RediSearchClient/Attributes/DefaultNoIndexAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace RediSearchClient.Attributes
{
/// <summary>
/// All properties are indexable by default. This attribute will specify that properties on a type that is used to create a RediSearch JSON Schema are non-indexable by default, use [Index] on target properties to override this behavior
/// </summary>
public sealed class DefaultNoIndexAttribute : Attribute
{

}
}
22 changes: 22 additions & 0 deletions RediSearchClient/Attributes/IndexAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace RediSearchClient.Attributes
{
/// <summary>
/// Use to specify if a property is indexable, defaults to true
/// </summary>
public sealed class IndexAttribute : Attribute
{
public bool Value { get; private set; }

public IndexAttribute(bool value)
{
Value = value;
}

public IndexAttribute()
{
Value = true;
}
}
}
11 changes: 11 additions & 0 deletions RediSearchClient/Attributes/NonStemmableAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace RediSearchClient.Attributes
{
public sealed class NonStemmableAttribute : Attribute
{
public NonStemmableAttribute()
{
}
}
}
15 changes: 15 additions & 0 deletions RediSearchClient/Attributes/PhoneticAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using RediSearchClient.Indexes;
using System;

namespace RediSearchClient.Attributes
{
public sealed class PhoneticAttribute : Attribute
{
public Language Language { get; private set; }

public PhoneticAttribute(Language language)
{
Language = language;
}
}
}
8 changes: 8 additions & 0 deletions RediSearchClient/Attributes/SchemaIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace RediSearchClient.Attributes
{
public sealed class SchemaIgnoreAttribute : Attribute
{
}
}
11 changes: 11 additions & 0 deletions RediSearchClient/Attributes/SortableAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace RediSearchClient.Attributes
{
public sealed class SortableAttribute : Attribute
{
public SortableAttribute()
{
}
}
}
19 changes: 19 additions & 0 deletions RediSearchClient/Attributes/TagAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace RediSearchClient.Attributes
{
public sealed class TagAttribute : Attribute
{
public string Separator { get; private set; } = ",";

public TagAttribute(string separator)
{
Separator = separator;
}

public TagAttribute()
{

}
}
}
14 changes: 14 additions & 0 deletions RediSearchClient/Attributes/WeightAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace RediSearchClient.Attributes
{
public sealed class WeightAttribute : Attribute
{
public int Weight { get; private set; }

public WeightAttribute(int weight)
{
Weight = weight;
}
}
}
22 changes: 22 additions & 0 deletions RediSearchClient/Converters/DateTimeToNumericConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace RediSearchClient.Converters
{
/// <summary>
/// RediSearch does not natively support DateTime types, therefore we need to convert DateTimes to numerics in this case
/// </summary>
internal sealed class DateTimeToNumericConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteNumberValue((value - DateTime.MinValue).TotalSeconds);
}
}
}
Loading

0 comments on commit 999322a

Please sign in to comment.