forked from apache/lucenenet
/
QueryValueSource.cs
283 lines (251 loc) · 9.55 KB
/
QueryValueSource.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
// Lucene version compatibility level 4.8.1
using Lucene.Net.Index;
using Lucene.Net.Queries.Function.DocValues;
using Lucene.Net.Search;
using Lucene.Net.Util;
using Lucene.Net.Util.Mutable;
using System;
using System.Collections;
using System.IO;
namespace Lucene.Net.Queries.Function.ValueSources
{
/*
* 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>
/// <see cref="QueryValueSource"/> returns the relevance score of the query
/// </summary>
public class QueryValueSource : ValueSource
{
internal readonly Query q;
internal readonly float defVal;
public QueryValueSource(Query q, float defVal)
{
this.q = q;
this.defVal = defVal;
}
public virtual Query Query => q;
public virtual float DefaultValue => defVal;
public override string GetDescription()
{
return "query(" + q + ",def=" + defVal + ")";
}
public override FunctionValues GetValues(IDictionary fcontext, AtomicReaderContext readerContext)
{
return new QueryDocValues(this, readerContext, fcontext);
}
public override int GetHashCode()
{
return q.GetHashCode() * 29;
}
public override bool Equals(object o)
{
if (!(o is QueryValueSource other))
return false;
return this.q.Equals(other.q) && this.defVal == other.defVal;
}
public override void CreateWeight(IDictionary context, IndexSearcher searcher)
{
Weight w = searcher.CreateNormalizedWeight(q);
context[this] = w;
}
}
internal class QueryDocValues : SingleDocValues
{
internal readonly AtomicReaderContext readerContext;
internal readonly IBits acceptDocs;
internal readonly Weight weight;
internal readonly float defVal;
internal readonly IDictionary fcontext;
internal readonly Query q;
internal Scorer scorer;
internal int scorerDoc; // the document the scorer is on
internal bool noMatches = false;
// the last document requested... start off with high value
// to trigger a scorer reset on first access.
internal int lastDocRequested = int.MaxValue;
public QueryDocValues(QueryValueSource vs, AtomicReaderContext readerContext, IDictionary fcontext)
: base(vs)
{
this.readerContext = readerContext;
this.acceptDocs = readerContext.AtomicReader.LiveDocs;
this.defVal = vs.defVal;
this.q = vs.q;
this.fcontext = fcontext;
Weight w = fcontext is null ? null : (Weight)fcontext[vs];
if (w is null)
{
IndexSearcher weightSearcher;
if (fcontext is null)
{
weightSearcher = new IndexSearcher(ReaderUtil.GetTopLevelContext(readerContext));
}
else
{
weightSearcher = (IndexSearcher)fcontext["searcher"];
if (weightSearcher is null)
{
weightSearcher = new IndexSearcher(ReaderUtil.GetTopLevelContext(readerContext));
}
}
vs.CreateWeight(fcontext, weightSearcher);
w = (Weight)fcontext[vs];
}
weight = w;
}
/// <summary>
/// NOTE: This was floatVal() in Lucene
/// </summary>
public override float SingleVal(int doc)
{
try
{
if (doc < lastDocRequested)
{
if (noMatches)
{
return defVal;
}
scorer = weight.GetScorer(readerContext, acceptDocs);
if (scorer is null)
{
noMatches = true;
return defVal;
}
scorerDoc = -1;
}
lastDocRequested = doc;
if (scorerDoc < doc)
{
scorerDoc = scorer.Advance(doc);
}
if (scorerDoc > doc)
{
// query doesn't match this document... either because we hit the
// end, or because the next doc is after this doc.
return defVal;
}
// a match!
return scorer.GetScore();
}
catch (Exception e) when (e.IsIOException())
{
throw RuntimeException.Create("caught exception in QueryDocVals(" + q + ") doc=" + doc, e);
}
}
public override bool Exists(int doc)
{
try
{
if (doc < lastDocRequested)
{
if (noMatches)
{
return false;
}
scorer = weight.GetScorer(readerContext, acceptDocs);
scorerDoc = -1;
if (scorer is null)
{
noMatches = true;
return false;
}
}
lastDocRequested = doc;
if (scorerDoc < doc)
{
scorerDoc = scorer.Advance(doc);
}
if (scorerDoc > doc)
{
// query doesn't match this document... either because we hit the
// end, or because the next doc is after this doc.
return false;
}
// a match!
return true;
}
catch (Exception e) when (e.IsIOException())
{
throw RuntimeException.Create("caught exception in QueryDocVals(" + q + ") doc=" + doc, e);
}
}
public override object ObjectVal(int doc)
{
try
{
return Exists(doc) ? J2N.Numerics.Single.GetInstance(scorer.GetScore()) : null; // LUCENENET: In Java, the conversion to instance of java.util.Float is implicit, but we need to do an explicit conversion
}
catch (Exception e) when (e.IsIOException())
{
throw RuntimeException.Create("caught exception in QueryDocVals(" + q + ") doc=" + doc, e);
}
}
public override ValueFiller GetValueFiller()
{
//
// TODO: if we want to support more than one value-filler or a value-filler in conjunction with
// the FunctionValues, then members like "scorer" should be per ValueFiller instance.
// Or we can say that the user should just instantiate multiple FunctionValues.
//
return new ValueFiller.AnonymousValueFiller<MutableValueSingle>(new MutableValueSingle(), fillValue: (doc, mutableValue) =>
{
try
{
if (noMatches)
{
mutableValue.Value = defVal;
mutableValue.Exists = false;
return;
}
scorer = weight.GetScorer(readerContext, acceptDocs);
scorerDoc = -1;
if (scorer is null)
{
noMatches = true;
mutableValue.Value = defVal;
mutableValue.Exists = false;
return;
}
lastDocRequested = doc;
if (scorerDoc < doc)
{
scorerDoc = scorer.Advance(doc);
}
if (scorerDoc > doc)
{
// query doesn't match this document... either because we hit the
// end, or because the next doc is after this doc.
mutableValue.Value = defVal;
mutableValue.Exists = false;
return;
}
// a match!
mutableValue.Value = scorer.GetScore();
mutableValue.Exists = true;
}
catch (Exception e) when (e.IsIOException())
{
throw RuntimeException.Create("caught exception in QueryDocVals(" + q + ") doc=" + doc, e);
}
});
}
public override string ToString(int doc)
{
return "query(" + q + ",def=" + defVal + ")=" + SingleVal(doc);
}
}
}