|
| 1 | +using System.Linq.Expressions; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| 4 | +using ParadeDB.EntityFrameworkCore.Internal.Metadata; |
| 5 | + |
| 6 | +namespace ParadeDB.EntityFrameworkCore.Extensions; |
| 7 | + |
| 8 | +public static class ParadeDbIndexBuilderExtensions |
| 9 | +{ |
| 10 | + public static Bm25IndexBuilder<TEntity> HasBm25Index<TEntity>( |
| 11 | + this EntityTypeBuilder<TEntity> entityTypeBuilder, |
| 12 | + string name, |
| 13 | + Expression<Func<TEntity, object?>> keyExpression |
| 14 | + ) |
| 15 | + where TEntity : class |
| 16 | + { |
| 17 | + var keyProperty = GetPropertyName(keyExpression); |
| 18 | + var indexBuilder = entityTypeBuilder.HasIndex(keyExpression).HasDatabaseName(name); |
| 19 | + |
| 20 | + indexBuilder.HasAnnotation(ParadeDbAnnotationNames.Bm25KeyProperty, keyProperty); |
| 21 | + indexBuilder.HasAnnotation( |
| 22 | + ParadeDbAnnotationNames.Bm25FieldProperties, |
| 23 | + new[] { keyProperty } |
| 24 | + ); |
| 25 | + // BM25FieldKinds is used to track if each index field is an EF Core property or a SQL expression |
| 26 | + // so that it can be rendered appropriately |
| 27 | + indexBuilder.HasAnnotation(ParadeDbAnnotationNames.Bm25FieldKinds, new[] { "property" }); |
| 28 | + indexBuilder.HasAnnotation(ParadeDbAnnotationNames.Bm25FieldTokenizers, new[] { "" }); |
| 29 | + indexBuilder.HasAnnotation(ParadeDbAnnotationNames.Bm25FieldAliases, new[] { "" }); |
| 30 | + |
| 31 | + return new Bm25IndexBuilder<TEntity>(indexBuilder); |
| 32 | + } |
| 33 | + |
| 34 | + internal static string GetPropertyName<TEntity, TProperty>( |
| 35 | + Expression<Func<TEntity, TProperty>> propertyExpression |
| 36 | + ) |
| 37 | + { |
| 38 | + var body = propertyExpression.Body is UnaryExpression unary |
| 39 | + ? unary.Operand |
| 40 | + : propertyExpression.Body; |
| 41 | + |
| 42 | + return body is MemberExpression member |
| 43 | + ? member.Member.Name |
| 44 | + : throw new ArgumentException("The BM25 key expression must be a property access."); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +public record FieldAlias(string name) { } |
| 49 | + |
| 50 | +// We use a custom index builder class instead of IndexBuilder directly to make it clear that not all |
| 51 | +// normal index creation operations are supported here (e.g. `IsUnique`) |
| 52 | +public sealed class Bm25IndexBuilder<TEntity> |
| 53 | + where TEntity : class |
| 54 | +{ |
| 55 | + private readonly IndexBuilder<TEntity> _indexBuilder; |
| 56 | + |
| 57 | + internal Bm25IndexBuilder(IndexBuilder<TEntity> indexBuilder) |
| 58 | + { |
| 59 | + _indexBuilder = indexBuilder; |
| 60 | + } |
| 61 | + |
| 62 | + public Bm25IndexBuilder<TEntity> HasField<TProperty>( |
| 63 | + Expression<Func<TEntity, TProperty>> propertyExpression |
| 64 | + ) |
| 65 | + { |
| 66 | + AddField( |
| 67 | + ParadeDbIndexBuilderExtensions.GetPropertyName(propertyExpression), |
| 68 | + "property", |
| 69 | + null, |
| 70 | + null |
| 71 | + ); |
| 72 | + |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public Bm25IndexBuilder<TEntity> HasField<TProperty>( |
| 77 | + Expression<Func<TEntity, TProperty>> propertyExpression, |
| 78 | + Tokenizer tokenizer |
| 79 | + ) |
| 80 | + { |
| 81 | + AddField( |
| 82 | + ParadeDbIndexBuilderExtensions.GetPropertyName(propertyExpression), |
| 83 | + "property", |
| 84 | + tokenizer, |
| 85 | + null |
| 86 | + ); |
| 87 | + |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public Bm25IndexBuilder<TEntity> HasField(string sql, Tokenizer tokenizer) |
| 92 | + { |
| 93 | + AddField(sql, "sql", tokenizer, null); |
| 94 | + |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public Bm25IndexBuilder<TEntity> HasField<TProperty>( |
| 99 | + Expression<Func<TEntity, TProperty>> propertyExpression, |
| 100 | + FieldAlias @alias |
| 101 | + ) |
| 102 | + { |
| 103 | + AddField( |
| 104 | + ParadeDbIndexBuilderExtensions.GetPropertyName(propertyExpression), |
| 105 | + "property", |
| 106 | + null, |
| 107 | + @alias.name |
| 108 | + ); |
| 109 | + |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Bm25IndexBuilder<TEntity> HasField(string sql, FieldAlias @alias) |
| 114 | + { |
| 115 | + AddField(sql, "sql", null, @alias.name); |
| 116 | + |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public Bm25IndexBuilder<TEntity> HasFilter(string? sql) |
| 121 | + { |
| 122 | + _indexBuilder.HasFilter(sql); |
| 123 | + |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + public Bm25IndexBuilder<TEntity> IsCreatedConcurrently(bool createdConcurrently = true) |
| 128 | + { |
| 129 | + _indexBuilder.IsCreatedConcurrently(createdConcurrently); |
| 130 | + |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + public Bm25IndexBuilder<TEntity> HasSearchTokenizer(Tokenizer tokenizer) |
| 135 | + { |
| 136 | + _indexBuilder.HasAnnotation( |
| 137 | + ParadeDbAnnotationNames.Bm25SearchTokenizer, |
| 138 | + tokenizer.ToSearchString() |
| 139 | + ); |
| 140 | + |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + private void AddField(string field, string kind, Tokenizer? tokenizer, string? @alias) |
| 145 | + { |
| 146 | + var properties = GetAnnotation(ParadeDbAnnotationNames.Bm25FieldProperties); |
| 147 | + var kinds = GetAnnotation(ParadeDbAnnotationNames.Bm25FieldKinds); |
| 148 | + var tokenizers = GetAnnotation(ParadeDbAnnotationNames.Bm25FieldTokenizers); |
| 149 | + var aliases = GetAnnotation(ParadeDbAnnotationNames.Bm25FieldAliases); |
| 150 | + |
| 151 | + _indexBuilder.HasAnnotation( |
| 152 | + ParadeDbAnnotationNames.Bm25FieldProperties, |
| 153 | + properties.Append(field).ToArray() |
| 154 | + ); |
| 155 | + _indexBuilder.HasAnnotation( |
| 156 | + ParadeDbAnnotationNames.Bm25FieldKinds, |
| 157 | + kinds.Append(kind).ToArray() |
| 158 | + ); |
| 159 | + _indexBuilder.HasAnnotation( |
| 160 | + ParadeDbAnnotationNames.Bm25FieldTokenizers, |
| 161 | + tokenizers.Append(tokenizer?.ToString() ?? "").ToArray() |
| 162 | + ); |
| 163 | + |
| 164 | + _indexBuilder.HasAnnotation( |
| 165 | + ParadeDbAnnotationNames.Bm25FieldAliases, |
| 166 | + aliases.Append(@alias is null ? "" : @alias.Replace("'", "''")).ToArray() |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + private string[] GetAnnotation(string name) => |
| 171 | + (string[]?)_indexBuilder.Metadata.FindAnnotation(name)?.Value ?? []; |
| 172 | +} |
0 commit comments