-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathFullTextSearchAttribute.cs
111 lines (100 loc) · 4.43 KB
/
FullTextSearchAttribute.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
using System;
using System.Reflection;
namespace Waher.Persistence.FullTextSearch
{
/// <summary>
/// This attribute defines that objects of this type should be indexed in the full-text-search index.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true, Inherited = true)]
public class FullTextSearchAttribute : Attribute
{
private readonly string indexCollection;
private readonly PropertyDefinition[] properties;
private readonly bool hasPropertyDefinitions;
private readonly bool isPropertyReference;
/// <summary>
/// This attribute defines that objects of this type should be indexed in the full-text-search index.
/// </summary>
/// <param name="IndexCollection">Name of full-text-search index collection.</param>
public FullTextSearchAttribute(string IndexCollection)
: this(IndexCollection, new PropertyDefinition[0])
{
}
/// <summary>
/// This attribute defines that objects of this type should be indexed in the full-text-search index.
/// </summary>
/// <param name="IndexCollection">Name of full-text-search index collection.</param>
/// <param name="Properties">Array of property (or field) names used to index objects of this type.
/// If not provided, and a <see cref="ITokenizer"/> exists for objects of this
/// class, that tokenizer will be used instead of the property array, to extract
/// tokens from the object.</param>
public FullTextSearchAttribute(string IndexCollection, params string[] Properties)
: this(IndexCollection, PropertyDefinition.ToArray(Properties))
{
}
/// <summary>
/// This attribute defines that objects of this type should be indexed in the full-text-search index.
/// </summary>
/// <param name="IndexCollection">Name of full-text-search index collection.</param>
/// <param name="Properties">Array of property (or field) definitions used to index objects of this type.
/// If not provided, and a <see cref="ITokenizer"/> exists for objects of this
/// class, that tokenizer will be used instead of the property array, to extract
/// tokens from the object.</param>
public FullTextSearchAttribute(string IndexCollection, params PropertyDefinition[] Properties)
{
this.indexCollection = IndexCollection;
this.properties = Properties;
this.hasPropertyDefinitions = (Properties?.Length ?? 0) > 0;
this.isPropertyReference = false;
}
/// <summary>
/// This attribute defines that objects of this type should be indexed in the full-text-search index.
/// </summary>
/// <param name="IndexCollection">Name of full-text-search index collection.</param>
/// <param name="PropertyReference">If the <paramref name="IndexCollection"/> reference
/// is pointing to a property on the object (true) or is a constant index collection
/// reference (false).
///
/// Note: Classes using dynamic index collection names require custom
/// tokenizers to be tokenized properly.</param>
public FullTextSearchAttribute(string IndexCollection, bool PropertyReference)
{
this.indexCollection = IndexCollection;
this.properties = new PropertyDefinition[0];
this.hasPropertyDefinitions = (this.Properties?.Length ?? 0) > 0;
this.isPropertyReference = PropertyReference;
}
/// <summary>
/// If the index collection is dynamic (i.e. depends on object instance).
/// </summary>
public bool DynamicIndexCollection => this.isPropertyReference;
/// <summary>
/// Name of full-text-search index collection.
/// </summary>
public string GetIndexCollection(object Reference)
{
if (this.isPropertyReference)
{
Type T = Reference.GetType();
PropertyInfo PI = T.GetRuntimeProperty(this.indexCollection)
?? throw new ArgumentException("Object lacks a property named " + this.indexCollection, nameof(Reference));
object Obj = PI.GetValue(Reference);
if (Obj is string s)
return s;
else
throw new ArgumentException("Object property " + this.indexCollection + " does not return a string.", nameof(Reference));
}
else
return this.indexCollection;
}
/// <summary>
/// Array of property (or field) definitions used to index objects of this type.
/// </summary>
public PropertyDefinition[] Properties => this.properties;
/// <summary>
/// If property names are defined for this class (true), or
/// if objects are to be tokenized using a specialized tokenizer (false).
/// </summary>
public bool HasPropertyDefinitions => this.hasPropertyDefinitions;
}
}