|
| 1 | +using System.Diagnostics; |
| 2 | + |
| 3 | +namespace Milvus.Client; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Defines a function within a <see cref="CollectionSchema" /> that automatically transforms data during insertion. |
| 7 | +/// </summary> |
| 8 | +/// <remarks> |
| 9 | +/// Functions enable automatic data transformations such as BM25 full-text search indexing, |
| 10 | +/// where text fields are automatically converted to sparse vectors. |
| 11 | +/// </remarks> |
| 12 | +[DebuggerDisplay("{DebuggerDisplay,nq}")] |
| 13 | +public sealed class FunctionSchema |
| 14 | +{ |
| 15 | + private readonly List<string> _inputFieldNames = new(); |
| 16 | + private readonly List<string> _outputFieldNames = new(); |
| 17 | + private readonly Dictionary<string, string> _params = new(); |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Creates a new function schema. |
| 21 | + /// </summary> |
| 22 | + /// <param name="name">The name of the function.</param> |
| 23 | + /// <param name="type">The type of function.</param> |
| 24 | + /// <param name="inputFieldNames">The names of the input fields for this function.</param> |
| 25 | + /// <param name="outputFieldNames">The names of the output fields for this function.</param> |
| 26 | + /// <param name="description">An optional description for the function.</param> |
| 27 | + public FunctionSchema( |
| 28 | + string name, |
| 29 | + FunctionType type, |
| 30 | + IEnumerable<string> inputFieldNames, |
| 31 | + IEnumerable<string> outputFieldNames, |
| 32 | + string description = "") |
| 33 | + { |
| 34 | + Name = name; |
| 35 | + Type = type; |
| 36 | + _inputFieldNames.AddRange(inputFieldNames); |
| 37 | + _outputFieldNames.AddRange(outputFieldNames); |
| 38 | + Description = description; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Creates a BM25 function schema for full-text search. |
| 43 | + /// </summary> |
| 44 | + /// <param name="name">The name of the function.</param> |
| 45 | + /// <param name="inputFieldName"> |
| 46 | + /// The name of the VARCHAR input field. This field must have <see cref="FieldSchema.EnableAnalyzer"/> set to true. |
| 47 | + /// </param> |
| 48 | + /// <param name="outputFieldName"> |
| 49 | + /// The name of the SPARSE_FLOAT_VECTOR output field that will be automatically populated. |
| 50 | + /// </param> |
| 51 | + /// <param name="description">An optional description for the function.</param> |
| 52 | + /// <returns>A new BM25 function schema.</returns> |
| 53 | + public static FunctionSchema CreateBm25( |
| 54 | + string name, |
| 55 | + string inputFieldName, |
| 56 | + string outputFieldName, |
| 57 | + string description = "") |
| 58 | + => new(name, FunctionType.Bm25, new[] { inputFieldName }, new[] { outputFieldName }, description); |
| 59 | + |
| 60 | + internal FunctionSchema( |
| 61 | + long id, |
| 62 | + string name, |
| 63 | + FunctionType type, |
| 64 | + IEnumerable<string> inputFieldNames, |
| 65 | + IEnumerable<string> outputFieldNames, |
| 66 | + string description, |
| 67 | + IEnumerable<KeyValuePair<string, string>> parameters) |
| 68 | + { |
| 69 | + Id = id; |
| 70 | + Name = name; |
| 71 | + Type = type; |
| 72 | + _inputFieldNames.AddRange(inputFieldNames); |
| 73 | + _outputFieldNames.AddRange(outputFieldNames); |
| 74 | + Description = description; |
| 75 | + foreach (var param in parameters) |
| 76 | + { |
| 77 | + _params[param.Key] = param.Value; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// The internal Milvus ID assigned to this function. |
| 83 | + /// </summary> |
| 84 | + public long Id { get; } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// The name of the function. |
| 88 | + /// </summary> |
| 89 | + public string Name { get; } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// The type of function. |
| 93 | + /// </summary> |
| 94 | + public FunctionType Type { get; } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// The names of the input fields for this function. |
| 98 | + /// </summary> |
| 99 | + public IList<string> InputFieldNames => _inputFieldNames; |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// The names of the output fields for this function. |
| 103 | + /// </summary> |
| 104 | + public IList<string> OutputFieldNames => _outputFieldNames; |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// An optional description for the function. |
| 108 | + /// </summary> |
| 109 | + public string Description { get; } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Additional parameters for the function. |
| 113 | + /// </summary> |
| 114 | + public IDictionary<string, string> Params => _params; |
| 115 | + |
| 116 | + private string DebuggerDisplay => $"{Name} ({Type})"; |
| 117 | +} |
0 commit comments