/
ToChildBlockJoinQuery.cs
389 lines (339 loc) · 15.4 KB
/
ToChildBlockJoinQuery.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
// Lucene version compatibility level 4.8.1
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Search.Join
{
/*
* 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>
/// Just like <see cref="ToParentBlockJoinQuery"/>, except this
/// query joins in reverse: you provide a <see cref="Query"/> matching
/// parent documents and it joins down to child
/// documents.
///
/// @lucene.experimental
/// </summary>
public class ToChildBlockJoinQuery : Query
{
/// <summary>
/// Message thrown from <see cref="ToChildBlockJoinScorer.ValidateParentDoc"/>
/// on mis-use, when the parent query incorrectly returns child docs.
/// </summary>
internal const string INVALID_QUERY_MESSAGE = "Parent query yields document which is not matched by parents filter, docID=";
private readonly Filter _parentsFilter;
private readonly Query _parentQuery;
// If we are rewritten, this is the original parentQuery we
// were passed; we use this for .equals() and
// .hashCode(). This makes rewritten query equal the
// original, so that user does not have to .rewrite() their
// query before searching:
private readonly Query _origParentQuery;
private readonly bool _doScores;
/// <summary>
/// Create a <see cref="ToChildBlockJoinQuery"/>.
/// </summary>
/// <param name="parentQuery"><see cref="Query"/> that matches parent documents</param>
/// <param name="parentsFilter"><see cref="Filter"/> (must produce FixedBitSet per-segment, like <see cref="FixedBitSetCachingWrapperFilter"/>)
/// identifying the parent documents.</param>
/// <param name="doScores">True if parent scores should be calculated.</param>
public ToChildBlockJoinQuery(Query parentQuery, Filter parentsFilter, bool doScores)
: base()
{
_origParentQuery = parentQuery;
_parentQuery = parentQuery;
_parentsFilter = parentsFilter;
_doScores = doScores;
}
private ToChildBlockJoinQuery(Query origParentQuery, Query parentQuery, Filter parentsFilter, bool doScores)
: base()
{
_origParentQuery = origParentQuery;
_parentQuery = parentQuery;
_parentsFilter = parentsFilter;
_doScores = doScores;
}
public override Weight CreateWeight(IndexSearcher searcher)
{
return new ToChildBlockJoinWeight(this, _parentQuery.CreateWeight(searcher), _parentsFilter, _doScores);
}
private class ToChildBlockJoinWeight : Weight
{
private readonly Query _joinQuery;
private readonly Weight _parentWeight;
private readonly Filter _parentsFilter;
private readonly bool _doScores;
public ToChildBlockJoinWeight(Query joinQuery, Weight parentWeight, Filter parentsFilter, bool doScores)
: base()
{
_joinQuery = joinQuery;
_parentWeight = parentWeight;
_parentsFilter = parentsFilter;
_doScores = doScores;
}
public override Query Query => _joinQuery;
public override float GetValueForNormalization()
{
return _parentWeight.GetValueForNormalization() * _joinQuery.Boost*_joinQuery.Boost;
}
public override void Normalize(float norm, float topLevelBoost)
{
_parentWeight.Normalize(norm, topLevelBoost * _joinQuery.Boost);
}
// NOTE: acceptDocs applies (and is checked) only in the child document space
public override Scorer GetScorer(AtomicReaderContext readerContext, IBits acceptDocs)
{
Scorer parentScorer = _parentWeight.GetScorer(readerContext, null);
if (parentScorer is null)
{
// No matches
return null;
}
// NOTE: we cannot pass acceptDocs here because this
// will (most likely, justifiably) cause the filter to
// not return a FixedBitSet but rather a
// BitsFilteredDocIdSet. Instead, we filter by
// acceptDocs when we score:
DocIdSet parents = _parentsFilter.GetDocIdSet(readerContext, null);
if (parents is null)
{
// No matches
return null;
}
if (!(parents is FixedBitSet))
{
throw IllegalStateException.Create("parentFilter must return FixedBitSet; got " + parents);
}
return new ToChildBlockJoinScorer(this, parentScorer, (FixedBitSet)parents, _doScores, acceptDocs);
}
public override Explanation Explain(AtomicReaderContext reader, int doc)
{
// TODO
throw UnsupportedOperationException.Create(GetType().Name + " cannot explain match on parent document");
}
public override bool ScoresDocsOutOfOrder => false;
}
private sealed class ToChildBlockJoinScorer : Scorer
{
private readonly Scorer _parentScorer;
private readonly FixedBitSet _parentBits;
private readonly bool _doScores;
private readonly IBits _acceptDocs;
private float _parentScore;
private int _parentFreq = 1;
private int _childDoc = -1;
private int _parentDoc;
public ToChildBlockJoinScorer(Weight weight, Scorer parentScorer, FixedBitSet parentBits, bool doScores, IBits acceptDocs)
: base(weight)
{
_doScores = doScores;
_parentBits = parentBits;
_parentScorer = parentScorer;
_acceptDocs = acceptDocs;
}
public override ICollection<ChildScorer> GetChildren()
{
return new JCG.List<ChildScorer> { new ChildScorer(_parentScorer, "BLOCK_JOIN") };
}
public override int NextDoc()
{
//System.out.println("Q.nextDoc() parentDoc=" + parentDoc + " childDoc=" + childDoc);
// Loop until we hit a childDoc that's accepted
while (true)
{
if (_childDoc + 1 == _parentDoc)
{
// OK, we are done iterating through all children
// matching this one parent doc, so we now nextDoc()
// the parent. Use a while loop because we may have
// to skip over some number of parents w/ no
// children:
while (true)
{
_parentDoc = _parentScorer.NextDoc();
ValidateParentDoc();
if (_parentDoc == 0)
{
// Degenerate but allowed: first parent doc has no children
// TODO: would be nice to pull initial parent
// into ctor so we can skip this if... but it's
// tricky because scorer must return -1 for
// .doc() on init...
_parentDoc = _parentScorer.NextDoc();
ValidateParentDoc();
}
if (_parentDoc == NO_MORE_DOCS)
{
_childDoc = NO_MORE_DOCS;
//System.out.println(" END");
return _childDoc;
}
// Go to first child for this next parentDoc:
_childDoc = 1 + _parentBits.PrevSetBit(_parentDoc - 1);
if (_childDoc == _parentDoc)
{
// This parent has no children; continue
// parent loop so we move to next parent
continue;
}
if (_acceptDocs != null && !_acceptDocs.Get(_childDoc))
{
goto nextChildDocContinue;
}
if (_childDoc < _parentDoc)
{
if (_doScores)
{
_parentScore = _parentScorer.GetScore();
_parentFreq = _parentScorer.Freq;
}
//System.out.println(" " + childDoc);
return _childDoc;
}
else
{
// Degenerate but allowed: parent has no children
}
}
}
if (Debugging.AssertsEnabled) Debugging.Assert(_childDoc < _parentDoc, "childDoc={0} parentDoc={1}", _childDoc, _parentDoc);
_childDoc++;
if (_acceptDocs != null && !_acceptDocs.Get(_childDoc))
{
continue;
}
//System.out.println(" " + childDoc);
return _childDoc;
nextChildDocContinue:;
}
}
/// <summary>
/// Detect mis-use, where provided parent query in fact sometimes returns child documents.
/// </summary>
private void ValidateParentDoc()
{
if (_parentDoc != NO_MORE_DOCS && !_parentBits.Get(_parentDoc))
{
throw IllegalStateException.Create(INVALID_QUERY_MESSAGE + _parentDoc);
}
}
public override int DocID => _childDoc;
public override float GetScore()
{
return _parentScore;
}
public override int Freq => _parentFreq;
public override int Advance(int childTarget)
{
if (Debugging.AssertsEnabled) Debugging.Assert(childTarget >= _parentBits.Length || !_parentBits.Get(childTarget));
//System.out.println("Q.advance childTarget=" + childTarget);
if (childTarget == NO_MORE_DOCS)
{
//System.out.println(" END");
return _childDoc = _parentDoc = NO_MORE_DOCS;
}
if (Debugging.AssertsEnabled) Debugging.Assert(_childDoc == -1 || childTarget != _parentDoc, "childTarget={0}", childTarget);
if (_childDoc == -1 || childTarget > _parentDoc)
{
// Advance to new parent:
_parentDoc = _parentScorer.Advance(childTarget);
ValidateParentDoc();
//System.out.println(" advance to parentDoc=" + parentDoc);
if (Debugging.AssertsEnabled) Debugging.Assert(_parentDoc > childTarget);
if (_parentDoc == NO_MORE_DOCS)
{
//System.out.println(" END");
return _childDoc = NO_MORE_DOCS;
}
if (_doScores)
{
_parentScore = _parentScorer.GetScore();
_parentFreq = _parentScorer.Freq;
}
int firstChild = _parentBits.PrevSetBit(_parentDoc - 1);
//System.out.println(" firstChild=" + firstChild);
childTarget = Math.Max(childTarget, firstChild);
}
if (Debugging.AssertsEnabled) Debugging.Assert(childTarget < _parentDoc);
// Advance within children of current parent:
_childDoc = childTarget;
//System.out.println(" " + childDoc);
if (_acceptDocs != null && !_acceptDocs.Get(_childDoc))
{
NextDoc();
}
return _childDoc;
}
public override long GetCost()
{
return _parentScorer.GetCost();
}
}
public override void ExtractTerms(ISet<Term> terms)
{
_parentQuery.ExtractTerms(terms);
}
public override Query Rewrite(IndexReader reader)
{
Query parentRewrite = _parentQuery.Rewrite(reader);
if (parentRewrite != _parentQuery)
{
Query rewritten = new ToChildBlockJoinQuery(_parentQuery, parentRewrite, _parentsFilter, _doScores);
rewritten.Boost = Boost;
return rewritten;
}
return this;
}
public override string ToString(string field)
{
return "ToChildBlockJoinQuery (" + _parentQuery + ")";
}
public override bool Equals(object obj)
{
if (obj is null) return false;
if (obj is ToChildBlockJoinQuery other)
{
return _origParentQuery.Equals(other._origParentQuery) &&
_parentsFilter.Equals(other._parentsFilter) &&
_doScores == other._doScores &&
base.Equals(other);
}
else
{
return false;
}
}
public override int GetHashCode()
{
unchecked
{
int hashCode = base.GetHashCode();
hashCode = (hashCode*397) ^ (_origParentQuery != null ? _origParentQuery.GetHashCode() : 0);
hashCode = (hashCode*397) ^ _doScores.GetHashCode();
hashCode = (hashCode*397) ^ (_parentsFilter != null ? _parentsFilter.GetHashCode() : 0);
return hashCode;
}
}
public override object Clone()
{
return new ToChildBlockJoinQuery((Query) _origParentQuery.Clone(), _parentsFilter, _doScores);
}
}
}