/
SearchExtensions.cs
129 lines (114 loc) · 5.28 KB
/
SearchExtensions.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
using System;
using Examine.Search;
namespace Examine
{
/// <summary>
/// A set of helpers for working with Lucene.Net in Examine
/// </summary>
public static class SearchExtensions
{
/// <summary>
/// Adds a single character wildcard to the string for Lucene wildcard matching
/// </summary>
/// <param name="s">The string to wildcard.</param>
/// <returns>An IExamineValue for the required operation</returns>
/// <exception cref="System.ArgumentException">Thrown when the string is null or empty</exception>
public static IExamineValue SingleCharacterWildcard(this string s)
{
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentException("Supplied string is null or empty.", nameof(s));
}
return new ExamineValue(Examineness.SimpleWildcard, s);
}
/// <summary>
/// Adds a multi-character wildcard to a string for Lucene wildcard matching
/// </summary>
/// <param name="s">The string to wildcard.</param>
/// <returns>An IExamineValue for the required operation</returns>
/// <exception cref="System.ArgumentException">Thrown when the string is null or empty</exception>
public static IExamineValue MultipleCharacterWildcard(this string s)
{
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentException("Supplied string is null or empty.", nameof(s));
}
return new ExamineValue(Examineness.ComplexWildcard, s);
}
/// <summary>
/// Configures the string for fuzzy matching in Lucene using the default fuzziness level
/// </summary>
/// <param name="s">The string to configure fuzzy matching on.</param>
/// <returns>An IExamineValue for the required operation</returns>
/// <exception cref="System.ArgumentException">Thrown when the string is null or empty</exception>
public static IExamineValue Fuzzy(this string s) => Fuzzy(s, 0.5f);
/// <summary>
/// Configures the string for fuzzy matching in Lucene using the supplied fuzziness level
/// </summary>
/// <param name="s">The string to configure fuzzy matching on.</param>
/// <param name="fuzzieness">The fuzzieness level. A value between 0 and 2</param>
/// <returns>
/// An IExamineValue for the required operation
/// </returns>
/// <exception cref="System.ArgumentException">Thrown when the string is null or empty</exception>
public static IExamineValue Fuzzy(this string s, float fuzzieness)
{
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentException("Supplied string is null or empty.", nameof(s));
}
return new ExamineValue(Examineness.Fuzzy, s, fuzzieness);
}
/// <summary>
/// Configures the string for boosting in Lucene
/// </summary>
/// <param name="s">The string to wildcard.</param>
/// <param name="boost">The boost level.</param>
/// <returns>
/// An IExamineValue for the required operation
/// </returns>
/// <exception cref="System.ArgumentException">Thrown when the string is null or empty</exception>
public static IExamineValue Boost(this string s, float boost)
{
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentException("Supplied string is null or empty.", nameof(s));
}
return new ExamineValue(Examineness.Boosted, s, boost);
}
/// <summary>
/// Configures the string for proximity matching
/// </summary>
/// <param name="s">The string to wildcard.</param>
/// <param name="proximity">The proximity level.</param>
/// <returns>
/// An IExamineValue for the required operation
/// </returns>
/// <exception cref="System.ArgumentException">Thrown when the string is null or empty</exception>
public static IExamineValue Proximity(this string s, int proximity)
{
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentException("Supplied string is null or empty.", nameof(s));
}
return new ExamineValue(Examineness.Proximity, s, Convert.ToSingle(proximity));
}
/// <summary>
/// Escapes the string within Lucene
/// </summary>
/// <param name="s">The string to escape.</param>
/// <returns>An IExamineValue for the required operation</returns>
/// <exception cref="System.ArgumentException">Thrown when the string is null or empty</exception>
public static IExamineValue Escape(this string s)
{
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentException("Supplied string is null or empty.", nameof(s));
}
//NOTE: You would be tempted to use QueryParser.Escape(s) here but that is incorrect because
// inside of LuceneSearchCriteria when we detect Escaped, we use a PhraseQuery which automatically
// escapes invalid chars.
return new ExamineValue(Examineness.Escaped, s);
}
}
}