|
1 | | -// .NET port of https://github.com/RedisLabs/JRediSearch/ |
2 | | - |
3 | | -using System; |
4 | | -using System.Collections.Generic; |
5 | | - |
6 | | -namespace NRediSearch |
7 | | -{ |
8 | | - /// <summary> |
9 | | - /// Schema abstracts the schema definition when creating an index. |
10 | | - /// Documents can contain fields not mentioned in the schema, but the index will only index pre-defined fields |
11 | | - /// </summary> |
12 | | - public sealed class Schema |
13 | | - { |
14 | | - public enum FieldType |
15 | | - { |
16 | | - FullText, |
17 | | - Geo, |
18 | | - Numeric, |
19 | | - Tag |
20 | | - } |
21 | | - |
22 | | - public class Field |
23 | | - { |
24 | | - public String Name { get; } |
25 | | - public FieldType Type { get; } |
26 | | - public bool Sortable {get;} |
27 | | - |
28 | | - internal Field(string name, FieldType type, bool sortable) |
29 | | - { |
30 | | - Name = name; |
31 | | - Type = type; |
32 | | - Sortable = sortable; |
33 | | - } |
34 | | - |
35 | | - internal virtual void SerializeRedisArgs(List<object> args) |
36 | | - { |
37 | | - object GetForRedis(FieldType type) |
38 | | - { |
39 | | - switch (type) |
40 | | - { |
41 | | - case FieldType.FullText: return "TEXT".Literal(); |
42 | | - case FieldType.Geo: return "GEO".Literal(); |
43 | | - case FieldType.Numeric: return "NUMERIC".Literal(); |
44 | | - case FieldType.Tag: return "TAG".Literal(); |
45 | | - default: throw new ArgumentOutOfRangeException(nameof(type)); |
46 | | - } |
47 | | - } |
48 | | - args.Add(Name); |
49 | | - args.Add(GetForRedis(Type)); |
50 | | - if(Sortable){args.Add("SORTABLE");} |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - public class TextField : Field |
55 | | - { |
56 | | - public double Weight { get; } |
57 | | - internal TextField(string name, double weight = 1.0) : base(name, FieldType.FullText, false) |
58 | | - { |
59 | | - Weight = weight; |
60 | | - } |
61 | | - |
62 | | - internal TextField(string name, bool sortable, double weight = 1.0) : base(name, FieldType.FullText, sortable) |
63 | | - { |
64 | | - Weight = weight; |
65 | | - } |
66 | | - |
67 | | - internal override void SerializeRedisArgs(List<object> args) |
68 | | - { |
69 | | - base.SerializeRedisArgs(args); |
70 | | - if (Weight != 1.0) |
71 | | - { |
72 | | - args.Add("WEIGHT".Literal()); |
73 | | - args.Add(Weight); |
74 | | - } |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - public List<Field> Fields { get; } = new List<Field>(); |
79 | | - |
80 | | - /// <summary> |
81 | | - /// Add a text field to the schema with a given weight |
82 | | - /// </summary> |
83 | | - /// <param name="name">the field's name</param> |
84 | | - /// <param name="weight">its weight, a positive floating point number</param> |
85 | | - /// <returns>the schema object</returns> |
86 | | - public Schema AddTextField(string name, double weight = 1.0) |
87 | | - { |
88 | | - Fields.Add(new TextField(name, weight)); |
89 | | - return this; |
90 | | - } |
91 | | - |
92 | | - /// <summary> |
93 | | - /// Add a text field that can be sorted on |
94 | | - /// </summary> |
95 | | - /// <param name="name">the field's name</param> |
96 | | - /// <param name="weight">its weight, a positive floating point number</param> |
97 | | - /// <returns>the schema object</returns> |
98 | | - public Schema AddSortableTextField(string name, double weight = 1.0) |
99 | | - { |
100 | | - Fields.Add(new TextField(name, true, weight)); |
101 | | - return this; |
102 | | - } |
103 | | - |
104 | | - /// <summary> |
105 | | - /// Add a numeric field to the schema |
106 | | - /// </summary> |
107 | | - /// <param name="name">the field's name</param> |
108 | | - /// <returns>the schema object</returns> |
109 | | - public Schema AddGeoField(string name) |
110 | | - { |
111 | | - Fields.Add(new Field(name, FieldType.Geo, false)); |
112 | | - return this; |
113 | | - } |
114 | | - |
115 | | - /// <summary> |
116 | | - /// Add a numeric field to the schema |
117 | | - /// </summary> |
118 | | - /// <param name="name">the field's name</param> |
119 | | - /// <returns>the schema object</returns> |
120 | | - public Schema AddNumericField(string name) |
121 | | - { |
122 | | - Fields.Add(new Field(name, FieldType.Numeric, false)); |
123 | | - return this; |
124 | | - } |
125 | | - |
126 | | - /// <summary> |
127 | | - /// Add a numeric field that can be sorted on |
128 | | - /// </summary> |
129 | | - /// <param name="name">the field's name</param> |
130 | | - /// <returns>the schema object</returns> |
131 | | - public Schema AddSortableNumericField(string name) |
132 | | - { |
133 | | - Fields.Add(new Field(name, FieldType.Numeric, true)); |
134 | | - return this; |
135 | | - } |
136 | | - |
137 | | - public class TagField : Field |
138 | | - { |
139 | | - public string Separator { get; } |
140 | | - internal TagField(string name, string separator = ",") : base(name, FieldType.Tag, false) |
141 | | - { |
142 | | - Separator = separator; |
143 | | - } |
144 | | - |
145 | | - internal override void SerializeRedisArgs(List<object> args) |
146 | | - { |
147 | | - base.SerializeRedisArgs(args); |
148 | | - if (Separator != ",") |
149 | | - { |
150 | | - args.Add("SEPARATOR".Literal()); |
151 | | - args.Add(Separator); |
152 | | - } |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - /// <summary> |
157 | | - /// Add a TAG field |
158 | | - /// </summary> |
159 | | - /// <param name="name">the field's name</param> |
160 | | - /// <param name="separator">tag separator</param> |
161 | | - /// <returns>the schema object</returns> |
162 | | - public Schema AddTagField(string name, string separator = ",") |
163 | | - { |
164 | | - Fields.Add(new TagField(name, separator)); |
165 | | - return this; |
166 | | - } |
167 | | - } |
168 | | -} |
| 1 | +// .NET port of https://github.com/RedisLabs/JRediSearch/ |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace NRediSearch |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Schema abstracts the schema definition when creating an index. |
| 10 | + /// Documents can contain fields not mentioned in the schema, but the index will only index pre-defined fields |
| 11 | + /// </summary> |
| 12 | + public sealed class Schema |
| 13 | + { |
| 14 | + public enum FieldType |
| 15 | + { |
| 16 | + FullText, |
| 17 | + Geo, |
| 18 | + Numeric, |
| 19 | + Tag |
| 20 | + } |
| 21 | + |
| 22 | + public class Field |
| 23 | + { |
| 24 | + public String Name { get; } |
| 25 | + public FieldType Type { get; } |
| 26 | + public bool Sortable {get;} |
| 27 | + |
| 28 | + internal Field(string name, FieldType type, bool sortable) |
| 29 | + { |
| 30 | + Name = name; |
| 31 | + Type = type; |
| 32 | + Sortable = sortable; |
| 33 | + } |
| 34 | + |
| 35 | + internal virtual void SerializeRedisArgs(List<object> args) |
| 36 | + { |
| 37 | + object GetForRedis(FieldType type) |
| 38 | + { |
| 39 | + switch (type) |
| 40 | + { |
| 41 | + case FieldType.FullText: return "TEXT".Literal(); |
| 42 | + case FieldType.Geo: return "GEO".Literal(); |
| 43 | + case FieldType.Numeric: return "NUMERIC".Literal(); |
| 44 | + case FieldType.Tag: return "TAG".Literal(); |
| 45 | + default: throw new ArgumentOutOfRangeException(nameof(type)); |
| 46 | + } |
| 47 | + } |
| 48 | + args.Add(Name); |
| 49 | + args.Add(GetForRedis(Type)); |
| 50 | + if(Sortable){args.Add("SORTABLE");} |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public class TextField : Field |
| 55 | + { |
| 56 | + public double Weight { get; } |
| 57 | + internal TextField(string name, double weight = 1.0) : base(name, FieldType.FullText, false) |
| 58 | + { |
| 59 | + Weight = weight; |
| 60 | + } |
| 61 | + |
| 62 | + internal TextField(string name, bool sortable, double weight = 1.0) : base(name, FieldType.FullText, sortable) |
| 63 | + { |
| 64 | + Weight = weight; |
| 65 | + } |
| 66 | + |
| 67 | + internal override void SerializeRedisArgs(List<object> args) |
| 68 | + { |
| 69 | + base.SerializeRedisArgs(args); |
| 70 | + if (Weight != 1.0) |
| 71 | + { |
| 72 | + args.Add("WEIGHT".Literal()); |
| 73 | + args.Add(Weight); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public List<Field> Fields { get; } = new List<Field>(); |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Add a text field to the schema with a given weight |
| 82 | + /// </summary> |
| 83 | + /// <param name="name">the field's name</param> |
| 84 | + /// <param name="weight">its weight, a positive floating point number</param> |
| 85 | + /// <returns>the schema object</returns> |
| 86 | + public Schema AddTextField(string name, double weight = 1.0) |
| 87 | + { |
| 88 | + Fields.Add(new TextField(name, weight)); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Add a text field that can be sorted on |
| 94 | + /// </summary> |
| 95 | + /// <param name="name">the field's name</param> |
| 96 | + /// <param name="weight">its weight, a positive floating point number</param> |
| 97 | + /// <returns>the schema object</returns> |
| 98 | + public Schema AddSortableTextField(string name, double weight = 1.0) |
| 99 | + { |
| 100 | + Fields.Add(new TextField(name, true, weight)); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Add a numeric field to the schema |
| 106 | + /// </summary> |
| 107 | + /// <param name="name">the field's name</param> |
| 108 | + /// <returns>the schema object</returns> |
| 109 | + public Schema AddGeoField(string name) |
| 110 | + { |
| 111 | + Fields.Add(new Field(name, FieldType.Geo, false)); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Add a numeric field to the schema |
| 117 | + /// </summary> |
| 118 | + /// <param name="name">the field's name</param> |
| 119 | + /// <returns>the schema object</returns> |
| 120 | + public Schema AddNumericField(string name) |
| 121 | + { |
| 122 | + Fields.Add(new Field(name, FieldType.Numeric, false)); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Add a numeric field that can be sorted on |
| 128 | + /// </summary> |
| 129 | + /// <param name="name">the field's name</param> |
| 130 | + /// <returns>the schema object</returns> |
| 131 | + public Schema AddSortableNumericField(string name) |
| 132 | + { |
| 133 | + Fields.Add(new Field(name, FieldType.Numeric, true)); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + public class TagField : Field |
| 138 | + { |
| 139 | + public string Separator { get; } |
| 140 | + internal TagField(string name, string separator = ",") : base(name, FieldType.Tag, false) |
| 141 | + { |
| 142 | + Separator = separator; |
| 143 | + } |
| 144 | + |
| 145 | + internal override void SerializeRedisArgs(List<object> args) |
| 146 | + { |
| 147 | + base.SerializeRedisArgs(args); |
| 148 | + if (Separator != ",") |
| 149 | + { |
| 150 | + args.Add("SEPARATOR".Literal()); |
| 151 | + args.Add(Separator); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Add a TAG field |
| 158 | + /// </summary> |
| 159 | + /// <param name="name">the field's name</param> |
| 160 | + /// <param name="separator">tag separator</param> |
| 161 | + /// <returns>the schema object</returns> |
| 162 | + public Schema AddTagField(string name, string separator = ",") |
| 163 | + { |
| 164 | + Fields.Add(new TagField(name, separator)); |
| 165 | + return this; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments