-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSearch.cs
More file actions
315 lines (284 loc) · 11.4 KB
/
Search.cs
File metadata and controls
315 lines (284 loc) · 11.4 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Persistence.FullTextSearch.Files;
using Waher.Persistence.FullTextSearch.Keywords;
namespace Waher.Persistence.FullTextSearch
{
/// <summary>
/// Order in which results are returned.
/// </summary>
public enum FullTextSearchOrder
{
/// <summary>
/// Relevant to keywords used.
/// </summary>
Relevance,
/// <summary>
/// Occurrences of keyworkds.
/// </summary>
Occurrences,
/// <summary>
/// From newest to oldest
/// </summary>
Newest,
/// <summary>
/// From oldest to newest
/// </summary>
Oldest
}
/// <summary>
/// How pagination in full-text-searches should be handled.
/// </summary>
public enum PaginationStrategy
{
/// <summary>
/// Pagination is done over objects found in search. Incompatible types
/// are returned as null. Makes pagination quicker, as objects do not need
/// to be preloaded, and can be skipped quicker.
/// </summary>
PaginateOverObjectsNullIfIncompatible,
/// <summary>
/// Pagination is done over objects found in search. Only compatible
/// objects are returned. Amount of objects returned might be less than
/// number of objects found, making evaluation of next offset in paginated
/// search difficult.
/// </summary>
PaginateOverObjectsOnlyCompatible,
/// <summary>
/// Pagination is done over compatible objects found in search. Pagination
/// becomes more resource intensive, as all objects need to be loaded to be
/// checked if they are compatible or not.
/// </summary>
PaginationOverCompatibleOnly
}
/// <summary>
/// Static class for access to Full-Text-Search
/// </summary>
public static class Search
{
internal static Task RaiseObjectAddedToIndex(object Sender, ObjectReferenceEventArgs e)
{
return ObjectAddedToIndex.Raise(Sender, e);
}
/// <summary>
/// Event raised when a new object instance has been indexed in the
/// full-text-search index.
/// </summary>
public static event EventHandlerAsync<ObjectReferenceEventArgs> ObjectAddedToIndex;
internal static Task RaiseObjectRemovedFromIndex(object Sender, ObjectReferenceEventArgs e)
{
return ObjectRemovedFromIndex.Raise(Sender, e);
}
/// <summary>
/// Event raised when an object instance has been removed from the
/// full-text-search index.
/// </summary>
public static event EventHandlerAsync<ObjectReferenceEventArgs> ObjectRemovedFromIndex;
internal static Task RaiseObjectUpdatedInIndex(object Sender, ObjectReferenceEventArgs e)
{
return ObjectUpdatedInIndex.Raise(Sender, e);
}
/// <summary>
/// Event raised when an object instance has been updated in the
/// full-text-search index.
/// </summary>
public static event EventHandlerAsync<ObjectReferenceEventArgs> ObjectUpdatedInIndex;
/// <summary>
/// Parses a search string into keyworkds.
/// </summary>
/// <param name="Search">Search string.</param>
/// <returns>Keywords</returns>
public static Keyword[] ParseKeywords(string Search)
{
return ParseKeywords(Search, false);
}
/// <summary>
/// Parses a search string into keyworkds.
/// </summary>
/// <param name="Search">Search string.</param>
/// <param name="TreatKeywordsAsPrefixes">If keywords should be treated as
/// prefixes. Example: "test" would match "test", "tests" and "testing" if
/// treated as a prefix, but also "tester", "testosterone", etc.
/// Default is false.</param>
/// <returns>Keywords</returns>
public static Keyword[] ParseKeywords(string Search, bool TreatKeywordsAsPrefixes)
{
return FullTextSearchModule.ParseKeywords(Search, TreatKeywordsAsPrefixes);
}
/// <summary>
/// Performs a Full-Text-Search
/// </summary>
/// <param name="IndexCollection">Index collection name.</param>
/// <param name="Offset">Index of first object matching the keywords.</param>
/// <param name="MaxCount">Maximum number of objects to return.</param>
/// <param name="Keywords">Keywords to search for.</param>
/// <param name="Order">Sort order of result set.</param>
/// <returns>Array of objects. Noncompatible (with <typeparamref name="T"/>) items are returned as null
/// in the array..</returns>
public static Task<T[]> FullTextSearch<T>(string IndexCollection,
int Offset, int MaxCount, FullTextSearchOrder Order, params Keyword[] Keywords)
where T : class
{
return FullTextSearch<T>(IndexCollection, Offset, MaxCount, Order,
PaginationStrategy.PaginateOverObjectsNullIfIncompatible, Keywords);
}
/// <summary>
/// Performs a Full-Text-Search
/// </summary>
/// <param name="IndexCollection">Index collection name.</param>
/// <param name="Offset">Index of first object matching the keywords.</param>
/// <param name="MaxCount">Maximum number of objects to return.</param>
/// <param name="Keywords">Keywords to search for.</param>
/// <param name="Order">Sort order of result set.</param>
/// <param name="PaginationStrategy">How to handle noncompatible items.</param>
/// <returns>Array of objects. Depending on choice of
/// <paramref name="PaginationStrategy"/>, null items may be returned
/// if underlying object is not compatible with <typeparamref name="T"/>.</returns>
public static Task<T[]> FullTextSearch<T>(string IndexCollection,
int Offset, int MaxCount, FullTextSearchOrder Order,
PaginationStrategy PaginationStrategy, params Keyword[] Keywords)
where T : class
{
return FullTextSearchModule.FullTextSearch<T>(IndexCollection, Offset, MaxCount,
Order, PaginationStrategy, Keywords);
}
/// <summary>
/// Registers stop-words with the search-engine.
/// Stop-words are ignored in searches.
/// </summary>
/// <param name="StopWords">Stop words.</param>
public static void RegisterStopWords(params string[] StopWords)
{
FullTextSearchModule.RegisterStopWords(StopWords);
}
/// <summary>
/// Checks if a word is a stop word.
/// </summary>
/// <param name="StopWord">Word to check.</param>
/// <returns>If word is a stop word.</returns>
public static bool IsStopWord(string StopWord)
{
return FullTextSearchModule.IsStopWord(StopWord);
}
/// <summary>
/// Tokenizes a set of objects using available tokenizers.
/// Tokenizers are classes with a default contructor, implementing
/// the <see cref="ITokenizer"/> interface.
/// </summary>
/// <param name="Objects">Objects to tokenize.</param>
/// <returns>Tokens</returns>
public static Task<TokenCount[]> Tokenize(IEnumerable<object> Objects)
{
return FullTextSearchModule.Tokenize(Objects);
}
/// <summary>
/// Tokenizes a set of objects using available tokenizers.
/// Tokenizers are classes with a default contructor, implementing
/// the <see cref="ITokenizer"/> interface.
/// </summary>
/// <param name="Objects">Objects to tokenize.</param>
/// <returns>Tokens</returns>
public static Task<TokenCount[]> Tokenize(params object[] Objects)
{
return FullTextSearchModule.Tokenize(Objects);
}
/// <summary>
/// Defines the Full-text-search index collection name, for objects in a given collection.
/// </summary>
/// <param name="IndexCollection">Collection name for full-text-search index of objects in the given collection.</param>
/// <param name="CollectionName">Collection of objects to index.</param>
/// <returns>If the configuration was changed.</returns>
public static Task<bool> SetFullTextSearchIndexCollection(string IndexCollection, string CollectionName)
{
return FullTextSearchModule.SetFullTextSearchIndexCollection(IndexCollection, CollectionName);
}
/// <summary>
/// Adds properties for full-text-search indexation.
/// </summary>
/// <param name="CollectionName">Collection name.</param>
/// <param name="Properties">Properties to index.</param>
/// <returns>If new property names were found and added.</returns>
public static Task<bool> AddFullTextSearch(string CollectionName, params PropertyDefinition[] Properties)
{
return FullTextSearchModule.AddFullTextSearch(CollectionName, Properties);
}
/// <summary>
/// Removes properties from full-text-search indexation.
/// </summary>
/// <param name="CollectionName">Collection name.</param>
/// <param name="Properties">Properties to remove from indexation.</param>
/// <returns>If property names were found and removed.</returns>
public static Task<bool> RemoveFullTextSearch(string CollectionName, params PropertyDefinition[] Properties)
{
return FullTextSearchModule.RemoveFullTextSearch(CollectionName, Properties);
}
/// <summary>
/// Gets indexed properties for full-text-search indexation.
/// </summary>
/// <returns>Dictionary of indexed properties, per collection.</returns>
public static Task<Dictionary<string, PropertyDefinition[]>> GetFullTextSearchIndexedProperties()
{
return FullTextSearchModule.GetFullTextSearchIndexedProperties();
}
/// <summary>
/// Gets indexed properties for full-text-search indexation.
/// </summary>
/// <param name="CollectionName">Collection name.</param>
/// <returns>Array of indexed properties.</returns>
public static Task<PropertyDefinition[]> GetFullTextSearchIndexedProperties(string CollectionName)
{
return FullTextSearchModule.GetFullTextSearchIndexedProperties(CollectionName);
}
/// <summary>
/// Gets the database collections that get indexed into a given index colltion.
/// </summary>
/// <returns>Collection Names indexed in the full-text-search index by index collection.</returns>
public static Task<Dictionary<string, string[]>> GetCollectionNames()
{
return FullTextSearchModule.GetCollectionNames();
}
/// <summary>
/// Gets the database collections that get indexed into a given index colltion.
/// </summary>
/// <param name="IndexCollectionName">Index Collection Name</param>
/// <returns>Collection Names indexed in the full-text-search index
/// defined by <paramref name="IndexCollectionName"/>.</returns>
public static Task<string[]> GetCollectionNames(string IndexCollectionName)
{
return FullTextSearchModule.GetCollectionNames(IndexCollectionName);
}
/// <summary>
/// Reindexes the full-text-search index for a database collection.
/// </summary>
/// <param name="IndexCollectionName">Index Collection</param>
/// <returns>Number of objects reindexed.</returns>
public static Task<long> ReindexCollection(string IndexCollectionName)
{
return FullTextSearchModule.ReindexCollection(IndexCollectionName);
}
/// <summary>
/// Indexes or reindexes files in a folder.
/// </summary>
/// <param name="IndexCollection">Name of index collection.</param>
/// <param name="Folder">Folder name.</param>
/// <param name="Recursive">If processing of files in subfolders should be performed.</param>
/// <param name="ExcludeSubfolders">Any subfolders to exclude (in recursive mode).</param>
/// <returns>Statistics about indexation process.</returns>
public static Task<FolderIndexationStatistics> IndexFolder(string IndexCollection, string Folder, bool Recursive,
params string[] ExcludeSubfolders)
{
return FullTextSearchModule.IndexFolder(IndexCollection, Folder, Recursive, ExcludeSubfolders);
}
/// <summary>
/// Indexes or reindexes a file.
/// </summary>
/// <param name="IndexCollection">Name of index collection.</param>
/// <param name="FileName">File name.</param>
/// <returns>If index was updated.</returns>
public static Task<bool> IndexFile(string IndexCollection, string FileName)
{
return FullTextSearchModule.IndexFile(IndexCollection, FileName);
}
}
}