-
Notifications
You must be signed in to change notification settings - Fork 623
/
SrndQuery.cs
137 lines (118 loc) · 4.73 KB
/
SrndQuery.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
using Lucene.Net.Search;
using Lucene.Net.Support;
using System;
using System.Globalization;
using System.Text;
using Float = J2N.Numerics.Single;
namespace Lucene.Net.QueryParsers.Surround.Query
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// Lowest level base class for surround queries
/// </summary>
public abstract class SrndQuery // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
//public SrndQuery() { }
private float weight = (float)1.0;
private bool weighted = false;
public virtual bool IsWeighted => weighted;
public virtual float Weight
{
get => weight;
set
{
weight = value; /* as parsed from the query text */
weighted = true;
}
}
public virtual string WeightString => Float.ToString(Weight, NumberFormatInfo.InvariantInfo);
public virtual string WeightOperator => "^";
protected virtual void WeightToString(StringBuilder r)
{
/* append the weight part of a query */
if (IsWeighted)
{
r.Append(WeightOperator);
r.Append(WeightString);
}
}
public virtual Search.Query MakeLuceneQueryField(string fieldName, BasicQueryFactory qf)
{
Search.Query q = MakeLuceneQueryFieldNoBoost(fieldName, qf);
if (IsWeighted)
{
q.Boost=(Weight * q.Boost); /* weight may be at any level in a SrndQuery */
}
return q;
}
public abstract Search.Query MakeLuceneQueryFieldNoBoost(string fieldName, BasicQueryFactory qf);
/// <summary>
/// This method is used by <see cref="GetHashCode()"/> and <see cref="Equals(Object)"/>,
/// see LUCENE-2945.
/// </summary>
public abstract override string ToString();
public virtual bool IsFieldsSubQueryAcceptable => true;
/// <summary> Shallow clone. Subclasses must override this if they
/// need to clone any members deeply,
/// </summary>
public virtual object Clone()
{
return MemberwiseClone(); // LUCENENET: never throws in .NET
}
/// <summary>
/// For subclasses of <see cref="SrndQuery"/> within the namespace
/// <see cref="Lucene.Net.QueryParsers.Surround.Query"/>
/// it is not necessary to override this method, <see cref="ToString()"/>
/// </summary>
public override int GetHashCode()
{
return GetType().GetHashCode() ^ ToString().GetHashCode();
}
/// <summary>
/// For subclasses of <see cref="SrndQuery"/> within the namespace
/// <see cref="Lucene.Net.QueryParsers.Surround.Query"/>
/// it is not necessary to override this method, <see cref="ToString()"/>
/// </summary>
public override bool Equals(object obj)
{
if (obj is null)
return false;
if (!GetType().Equals(obj.GetType()))
return false;
return ToString().Equals(obj.ToString(), StringComparison.Ordinal);
}
/// <summary> An empty Lucene query </summary>
public readonly static Search.Query TheEmptyLcnQuery = new EmptyLcnQuery(); /* no changes allowed */
internal sealed class EmptyLcnQuery : BooleanQuery
{
public override float Boost
{
get => base.Boost;
set => throw UnsupportedOperationException.Create();
}
public override void Add(BooleanClause clause)
{
throw UnsupportedOperationException.Create();
}
public override void Add(Search.Query query, Occur occur)
{
throw UnsupportedOperationException.Create();
}
}
}
}