forked from alexshyba/SitecoreSearchContrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdvancedDatabaseCrawler.cs
436 lines (374 loc) · 14.6 KB
/
AdvancedDatabaseCrawler.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#region Usings
using System;
using System.Collections.Generic;
using System.Xml;
using Sitecore.Collections;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Diagnostics;
using Lucene.Net.Documents;
using Sitecore.Reflection;
using Sitecore.Search;
using Sitecore.Search.Crawlers;
using Sitecore.Xml;
using System.Linq;
using scSearchContrib.Crawler.DynamicFields;
using scSearchContrib.Crawler.FieldCrawlers;
using scSearchContrib.Searcher.Utilities;
using SCField = Sitecore.Data.Fields.Field;
using LuceneField = Lucene.Net.Documents.Field;
#endregion
namespace scSearchContrib.Crawler.Crawlers
{
public class AdvancedDatabaseCrawler : DatabaseCrawler
{
#region Fields
private List<BaseDynamicField> _dynamicFields = new List<BaseDynamicField>();
private SafeDictionary<string, string> _fieldCrawlers = new SafeDictionary<string, string>();
private readonly SafeDictionary<string, bool> _fieldFilter = new SafeDictionary<string, bool>();
private readonly SafeDictionary<string, bool> _baseTemplateFilter = new SafeDictionary<string, bool>();
private readonly SafeDictionary<string, bool> _pathFilter = new SafeDictionary<string, bool>();
private SafeDictionary<string, SearchField> _fieldTypes = new SafeDictionary<string, SearchField>();
private bool _hasFieldExcludes;
private bool _hasFieldIncludes;
private bool _hasBaseTemplateExcludes;
private bool _hasBaseTemplateIncludes;
private bool _hasPathExcludes;
private bool _hasPathIncludes;
private bool _hasFieldTypeExcludes;
#endregion
#region Override Methods
// checking if item has template
protected override void IndexVersion(Item item, Item latestVersion, IndexUpdateContext context)
{
if (item.Template != null)
{
base.IndexVersion(item, latestVersion, context);
}
else
{
Log.Warn(string.Format("AdvancedDatabaseCrawler: Cannot update item version. Reason: Template is NULL in item '{0}'.", item.Paths.FullPath), this);
}
}
protected override void AddAllFields(Document document, Item item, bool versionSpecific)
{
Assert.ArgumentNotNull(document, "document");
Assert.ArgumentNotNull(item, "item");
var fields = FilteredFields(item);
foreach (var field in fields)
{
ProcessField(field, document);
}
ProcessDynamicFields(document, item);
}
protected virtual void ProcessField(SCField field, Document document)
{
var value = ExtendedFieldCrawlerFactory.GetFieldCrawlerValue(field, FieldCrawlers);
if (string.IsNullOrEmpty(value)) return;
var indexType = GetIndexType(field);
var storageType = GetStorageType(field);
var vectorType = GetVectorType(field);
value = IdHelper.ProcessGUIDs(value);
ProcessField(document, field.Key, value, storageType, indexType, vectorType);
if (indexType == LuceneField.Index.TOKENIZED)
{
ProcessField(document, BuiltinFields.Content, value, LuceneField.Store.NO, LuceneField.Index.TOKENIZED);
}
}
protected override bool IsMatch(Item item)
{
// handle base filters
var isMatch = base.IsMatch(item);
// handle base template filters
var flag = false;
var template = TemplateManager.GetTemplate(item);
if (template.GetBaseTemplates().Any(baseTemplate => _baseTemplateFilter.TryGetValue(baseTemplate.ID.ToString(), out flag)))
{
isMatch = isMatch && flag;
}
else
{
isMatch = isMatch && !HasBaseTemplateIncludes;
}
// handle path filters
flag = true;
IEnumerable<string> sortedPaths = _pathFilter.Keys.OrderBy(path => path.Length);
foreach (var path in sortedPaths.Where(path => item.Paths.FullPath.Contains(path)))
{
flag = _pathFilter[path];
}
return isMatch && flag;
}
#endregion
#region Config Methods
public virtual void AddDynamicFields(XmlNode configNode)
{
Assert.ArgumentNotNull(configNode, "configNode");
var type = XmlUtil.GetAttribute("type", configNode);
var fieldName = XmlUtil.GetAttribute("name", configNode);
var storageType = XmlUtil.GetAttribute("storageType", configNode);
var indexType = XmlUtil.GetAttribute("indexType", configNode);
var vectorType = XmlUtil.GetAttribute("vectorType", configNode);
var boost = XmlUtil.GetAttribute("boost", configNode);
var field = ReflectionUtil.CreateObject(type);
if (field == null || !(field is BaseDynamicField)) return;
var dynamicField = field as BaseDynamicField;
dynamicField.SetStorageType(storageType);
dynamicField.SetIndexType(indexType);
dynamicField.SetVectorType(vectorType);
dynamicField.SetBoost(boost);
dynamicField.FieldKey = fieldName.ToLowerInvariant();
DynamicFields.Add(dynamicField);
}
public virtual void AddFieldCrawlers(XmlNode configNode)
{
Assert.ArgumentNotNull(configNode, "configNode");
var type = XmlUtil.GetAttribute("type", configNode);
var fieldType = XmlUtil.GetAttribute("fieldType", configNode);
FieldCrawlers.Add(fieldType.ToLowerInvariant(), type);
}
public virtual void AddFieldTypes(XmlNode configNode)
{
Assert.ArgumentNotNull(configNode, "configNode");
var fieldName = XmlUtil.GetAttribute("name", configNode);
var storageType = XmlUtil.GetAttribute("storageType", configNode);
var indexType = XmlUtil.GetAttribute("indexType", configNode);
var vectorType = XmlUtil.GetAttribute("vectorType", configNode);
var boost = XmlUtil.GetAttribute("boost", configNode);
var exclude = XmlUtil.GetAttribute("exclude", configNode);
var searchField = new SearchField(storageType, indexType, vectorType, boost, exclude);
FieldTypes.Add(fieldName.ToLowerInvariant(), searchField);
if (searchField.Exclude)
{
_hasFieldTypeExcludes = true;
}
}
#endregion
#region Helper Methods
protected AbstractField CreateField(string name, string value, LuceneField.Store storageType, LuceneField.Index indexType, LuceneField.TermVector vectorType, float boost)
{
var field = new LuceneField(name, value, storageType, indexType, vectorType);
field.SetBoost(boost);
return field;
}
public void ExcludeField(string value)
{
Assert.ArgumentNotNullOrEmpty(value, "fieldId");
Assert.IsTrue(ID.IsID(value), "fieldId parameter is not a valid GUID");
_hasFieldExcludes = true;
_fieldFilter[value] = false;
}
public void IncludeField(string value)
{
Assert.ArgumentNotNullOrEmpty(value, "fieldId");
Assert.IsTrue(ID.IsID(value), "fieldId parameter is not a valid GUID");
_hasFieldIncludes = true;
_fieldFilter[value] = true;
}
public void ExcludeBaseTemplate(string value)
{
Assert.ArgumentNotNullOrEmpty(value, "template");
Assert.IsTrue(ID.IsID(value), "template parameter is not a valid GUID");
_hasBaseTemplateExcludes = true;
_baseTemplateFilter[value] = false;
}
public void IncludeBaseTemplate(string value)
{
Assert.ArgumentNotNullOrEmpty(value, "template");
Assert.IsTrue(ID.IsID(value), "template parameter is not a valid GUID");
_hasBaseTemplateIncludes = true;
_baseTemplateFilter[value] = true;
}
public void ExcludePath(string value)
{
Assert.ArgumentNotNullOrEmpty(value, "path");
_hasPathExcludes = true;
_pathFilter[value] = false;
}
public void IncludePath(string value)
{
Assert.ArgumentNotNullOrEmpty(value, "path");
_hasPathIncludes = true;
_pathFilter[value] = true;
}
protected virtual List<SCField> FilteredFields(Item item)
{
var filteredFields = new List<SCField>();
if (IndexAllFields)
{
item.Fields.ReadAll();
filteredFields.AddRange(item.Fields);
}
else if (HasFieldIncludes)
{
foreach (var includeFieldId in from p in FieldFilter where p.Value select p)
{
filteredFields.Add(item.Fields[ID.Parse(includeFieldId.Key)]);
}
}
if (HasFieldExcludes || HasFieldTypeExcludes)
{
foreach (SCField field in item.Fields)
{
var fieldKey = field.ID.ToString();
if (!(!FieldFilter.ContainsKey(fieldKey) ? true : FieldFilter[fieldKey]))
{
filteredFields.Remove(field);
}
else
{
object searchField = FieldTypes[field.TypeKey];
if (searchField is SearchField && ((SearchField)searchField).Exclude)
{
filteredFields.Remove(field);
}
}
}
}
return filteredFields.Where(f => !String.IsNullOrEmpty(f.Key)).ToList();
}
protected LuceneField.Index GetIndexType(SCField field)
{
if (FieldTypes.ContainsKey(field.TypeKey))
{
object searchField = FieldTypes[field.TypeKey];
if (searchField is SearchField)
{
return (searchField as SearchField).IndexType;
}
}
return LuceneField.Index.UN_TOKENIZED;
}
protected LuceneField.Store GetStorageType(SCField field)
{
if (FieldTypes.ContainsKey(field.TypeKey))
{
var searchField = FieldTypes[field.TypeKey];
return searchField.StorageType;
}
return LuceneField.Store.NO;
}
protected LuceneField.TermVector GetVectorType(SCField field)
{
if (FieldTypes.ContainsKey(field.TypeKey))
{
var searchField = FieldTypes[field.TypeKey];
return searchField.VectorType;
}
return LuceneField.TermVector.NO;
}
protected virtual void ProcessDynamicFields(Document document, Item item)
{
foreach (var dynamicField in DynamicFields)
{
var fieldValue = dynamicField.ResolveValue(item);
if (fieldValue != null)
{
ProcessField(document, dynamicField.FieldKey, fieldValue, dynamicField.StorageType,
dynamicField.IndexType, dynamicField.VectorType, dynamicField.Boost);
}
}
}
protected virtual void ProcessField(Document doc, string fieldKey, string fieldValue, LuceneField.Store storage, LuceneField.Index index)
{
ProcessField(doc, fieldKey, fieldValue, storage, index, LuceneField.TermVector.NO);
}
protected virtual void ProcessField(Document doc, string fieldKey, string fieldValue, LuceneField.Store storage, LuceneField.Index index, LuceneField.TermVector vector)
{
ProcessField(doc, fieldKey, fieldValue, storage, index, vector, 1f);
}
protected virtual void ProcessField(Document doc, string fieldKey, string fieldValue, LuceneField.Store storage, LuceneField.Index index, LuceneField.TermVector vector, float boost)
{
if ((!String.IsNullOrEmpty(fieldKey) && !String.IsNullOrEmpty(fieldValue))
&& (index != LuceneField.Index.NO || storage != LuceneField.Store.NO))
{
doc.Add(CreateField(fieldKey, fieldValue, storage, index, vector, boost));
}
}
#endregion
#region Properties
protected List<BaseDynamicField> DynamicFields
{
get
{
return _dynamicFields;
}
}
protected SafeDictionary<string, string> FieldCrawlers
{
get
{
return _fieldCrawlers;
}
}
protected SafeDictionary<string, bool> FieldFilter
{
get
{
return _fieldFilter;
}
}
protected SafeDictionary<string, SearchField> FieldTypes
{
get
{
return _fieldTypes;
}
}
protected bool HasFieldExcludes
{
get
{
return _hasFieldExcludes;
}
set
{
_hasFieldExcludes = value;
}
}
protected bool HasFieldIncludes
{
get
{
return _hasFieldIncludes;
}
set
{
_hasFieldIncludes = value;
}
}
protected bool HasBaseTemplateExcludes
{
get { return _hasBaseTemplateExcludes; }
set { _hasBaseTemplateExcludes = value; }
}
protected bool HasBaseTemplateIncludes
{
get { return _hasBaseTemplateIncludes; }
set { _hasBaseTemplateIncludes = value; }
}
protected bool HasPathExcludes
{
get { return _hasPathExcludes; }
set { _hasPathExcludes = value; }
}
protected bool HasPathIncludes
{
get { return _hasPathIncludes; }
set { _hasPathIncludes = value; }
}
protected bool HasFieldTypeExcludes
{
get
{
return _hasFieldTypeExcludes;
}
set
{
_hasFieldTypeExcludes = value;
}
}
#endregion
}
}