forked from lelandrichardson/LukeMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attributes.cs
219 lines (177 loc) · 6.55 KB
/
Attributes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
namespace LukeMapper
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
class LukeMapperAttribute : Attribute
{
/// <summary>
/// If True, all members of the decorated class will be added into the lucene document
/// with the options [Field.Index.NOT_ANALYZED_NO_NORMS, Field.Store.YES] provided no
/// LukeAttribute is applied to that member
/// </summary>
public bool IgnoreByDefault = false;
public Store DefaultStore = LukeMapper.DefaultStore;
public Index DefaultIndex = LukeMapper.DefaultIndex;
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class LukeAttribute : Attribute
{
/// <summary>
/// How will this member be stored in the Lucene Index
/// </summary>
public Store Store = LukeMapper.DefaultStore;
/// <summary>
/// How will this member be indexed in the Lucene Index
/// </summary>
public Index Index = LukeMapper.DefaultIndex;
/// <summary>
/// If true, this member will not be added to the lucene document
/// </summary>
public bool Ignore = false;
/// <summary>
/// Custom Field Name to store/index this member as in the Lucene Index
/// </summary>
public string FieldName = null;
}
[AttributeUsage(AttributeTargets.Method)]
public class LukeSerializerAttribute : Attribute
{
public string FieldName { get; set; }
public LukeSerializerAttribute(string fieldName = null)
{
FieldName = fieldName;
}
}
/// <summary>
/// Is used to apply to any IEnumerable<T>
/// LukeMapper will serialize the enumerable as a delimited string, with the
/// separated character being the Delimiter property.
///
/// Note: this is implemented using string.Join() and it is left to the user to ensure that the
/// delimited value is not included in the values of the list.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class LukeDelimitedAttribute : Attribute
{
public string Delimeter { get; set; }
public LukeDelimitedAttribute(string delimiter = ",")
{
Delimeter = delimiter;
}
}
/// <summary>
/// Expected a method with parameters of type Document, and expected to properly
/// deserialize + set the corresponding member from the document to the current instance.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class LukeDeserializerAttribute : Attribute
{
public string FieldName { get; set; }
public LukeDeserializerAttribute(string fieldName = null)
{
FieldName = fieldName;
}
}
//TODO: numeric fields...
//TODO: term vectors...
[LukeMapper]
public class ExampleClass
{
public int Id { get; set; }
public string Title { get; set; }
[Luke(Store = Store.YES, Index = Index.ANALYZED, FieldName = "_Body")]
public string Body { get; set; }
//[LukeSerializer]
//private static Document SerializeToDocument(ExampleClass obj)
//{
// var doc = new Document();
// return doc;
//}
//[LukeDeserializer]
//private static ExampleClass DeserializeDocument(Document doc)
//{
//}
}
//[LukeMapper(IgnoreByDefault = true)]
//public class ExampleClass
//{
// // doesn't get indexed/stored
// [Luke(Store = Store.YES)]
// public int Id { get; set; }
// // doesn't get stored, but is indexed in "searchtext" field
// [Luke(Store = Store.NO, Index = Index.ANALYZED, FieldName = "searchtext")]
// public string Title { get; set; }
// // doesn't get stored, but is indexed in "searchtext" field
// [Luke(Store = Store.NO, Index = Index.ANALYZED, FieldName = "searchtext")]
// public string Body { get; set; }
// // doesn't get indexed/stored
// public int IgnoredProperty { get; set; }
//}
//[LukeMapper(DefaultIndex = Index.ANALYZED)]
//public class ExampleClass
//{
// // doesn't get indexed/stored
// [Luke(Index = Index.NOT_ANALYZED_NO_NORMS)]
// public int Id { get; set; }
// // get's analyzed, AND stored
// public string Title { get; set; }
// // get's analyzed, AND stored
// public string Body { get; set; }
//}
//public class ExampleClass
//{
// // everything get's indexed and stored by default
// public int Id { get; set; }
// public string Title { get; set; }
// public string Body { get; set; }
// //opt-in ignored per property/field
// [Luke(Ignore=true)]
// public int Ignored { get; set; }
//}
//public class ExampleClass
//{
// // everything get's indexed and stored by default
// public int Id { get; set; }
// public string Title { get; set; }
// public string Body { get; set; }
// //opt-in ignored per property/field
// public int Ignored { get; set; }
//}
//public class TestCustomSerializerClass
//{
// public int Id { get; set; }
// //this list would typically be ignored
// public List<string> CustomList { get; set; }
// // if you specify a serializer, it will get serialized
// [LukeSerializer("CustomList")]
// public static string CustomListToString(List<string> list)
// {
// return string.Join(",", list);
// }
// // and similarly, deserialized
// [LukeDeserializer("CustomList")]
// public static List<string> StringToCustomList(string serialized)
// {
// return serialized.Split(',').ToList();
// }
//}
//public class TestCustomSerializerClass
//{
// public int Id { get; set; }
// // maybe you just want to index the list for search, but don't need it on .Query()
// [Luke(Store = Store.NO,Index = Index.ANALYZED)]
// public List<string> CustomList { get; set; }
// // in this case, only a serializer is needed
// [LukeSerializer("CustomList")]
// public static string CustomListToString(List<string> list)
// {
// return string.Join(" ", list);
// }
//}
}