forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConstantScoreAutoRewrite.cs
274 lines (246 loc) · 10.2 KB
/
ConstantScoreAutoRewrite.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
using System;
using System.Diagnostics;
namespace Lucene.Net.Search
{
/*
* 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.
*/
using ArrayUtil = Lucene.Net.Util.ArrayUtil;
using ByteBlockPool = Lucene.Net.Util.ByteBlockPool;
using BytesRef = Lucene.Net.Util.BytesRef;
using BytesRefHash = Lucene.Net.Util.BytesRefHash;
using IndexReader = Lucene.Net.Index.IndexReader;
using RamUsageEstimator = Lucene.Net.Util.RamUsageEstimator;
using Term = Lucene.Net.Index.Term;
using TermContext = Lucene.Net.Index.TermContext;
using TermsEnum = Lucene.Net.Index.TermsEnum;
using TermState = Lucene.Net.Index.TermState;
/// <summary>
/// A rewrite method that tries to pick the best
/// constant-score rewrite method based on term and
/// document counts from the query. If both the number of
/// terms and documents is small enough, then
/// <see cref="MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE"/> is used.
/// Otherwise, <see cref="MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE"/> is
/// used.
/// </summary>
// LUCENENET specific: made this class public. In Lucene there was a derived class
// with the same name that was nested within MultiTermQuery, but in .NET it is
// more intuitive if our classes are not nested.
public class ConstantScoreAutoRewrite : TermCollectingRewrite<BooleanQuery>
{
/// <summary>
/// Defaults derived from rough tests with a 20.0 million
/// doc Wikipedia index. With more than 350 terms in the
/// query, the filter method is fastest:
/// </summary>
public static int DEFAULT_TERM_COUNT_CUTOFF = 350;
/// <summary>
/// If the query will hit more than 1 in 1000 of the docs
/// in the index (0.1%), the filter method is fastest:
/// </summary>
public static double DEFAULT_DOC_COUNT_PERCENT = 0.1;
private int termCountCutoff = DEFAULT_TERM_COUNT_CUTOFF;
private double docCountPercent = DEFAULT_DOC_COUNT_PERCENT;
/// <summary>
/// If the number of terms in this query is equal to or
/// larger than this setting then
/// <see cref="MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE"/> is used.
/// </summary>
public virtual int TermCountCutoff
{
set
{
termCountCutoff = value;
}
get
{
return termCountCutoff;
}
}
/// <summary>
/// If the number of documents to be visited in the
/// postings exceeds this specified percentage of the
/// <see cref="Index.IndexReader.MaxDoc"/> for the index, then
/// <see cref="MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE"/> is used.
/// Value may be 0.0 to 100.0.
/// </summary>
public virtual double DocCountPercent
{
set
{
docCountPercent = value;
}
get
{
return docCountPercent;
}
}
protected override BooleanQuery GetTopLevelQuery()
{
return new BooleanQuery(true);
}
protected override void AddClause(BooleanQuery topLevel, Term term, int docFreq, float boost, TermContext states) //ignored
{
topLevel.Add(new TermQuery(term, states), Occur.SHOULD);
}
public override Query Rewrite(IndexReader reader, MultiTermQuery query)
{
// Get the enum and start visiting terms. If we
// exhaust the enum before hitting either of the
// cutoffs, we use ConstantBooleanQueryRewrite; else,
// ConstantFilterRewrite:
int docCountCutoff = (int)((docCountPercent / 100.0) * reader.MaxDoc);
int termCountLimit = Math.Min(BooleanQuery.MaxClauseCount, termCountCutoff);
CutOffTermCollector col = new CutOffTermCollector(docCountCutoff, termCountLimit);
CollectTerms(reader, query, col);
int size = col.pendingTerms.Count;
if (col.hasCutOff)
{
return MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE.Rewrite(reader, query);
}
else
{
BooleanQuery bq = GetTopLevelQuery();
if (size > 0)
{
BytesRefHash pendingTerms = col.pendingTerms;
int[] sort = pendingTerms.Sort(col.termsEnum.Comparer);
for (int i = 0; i < size; i++)
{
int pos = sort[i];
// docFreq is not used for constant score here, we pass 1
// to explicitely set a fake value, so it's not calculated
AddClause(bq, new Term(query.m_field, pendingTerms.Get(pos, new BytesRef())), 1, 1.0f, col.array.termState[pos]);
}
}
// Strip scores
Query result = new ConstantScoreQuery(bq);
result.Boost = query.Boost;
return result;
}
}
internal sealed class CutOffTermCollector : TermCollector
{
private void InitializeInstanceFields()
{
pendingTerms = new BytesRefHash(new ByteBlockPool(new ByteBlockPool.DirectAllocator()), 16, array);
}
internal CutOffTermCollector(int docCountCutoff, int termCountLimit)
{
InitializeInstanceFields();
this.docCountCutoff = docCountCutoff;
this.termCountLimit = termCountLimit;
}
public override void SetNextEnum(TermsEnum termsEnum)
{
this.termsEnum = termsEnum;
}
public override bool Collect(BytesRef bytes)
{
int pos = pendingTerms.Add(bytes);
docVisitCount += termsEnum.DocFreq;
if (pendingTerms.Count >= termCountLimit || docVisitCount >= docCountCutoff)
{
hasCutOff = true;
return false;
}
TermState termState = termsEnum.GetTermState();
Debug.Assert(termState != null);
if (pos < 0)
{
pos = (-pos) - 1;
array.termState[pos].Register(termState, m_readerContext.Ord, termsEnum.DocFreq, termsEnum.TotalTermFreq);
}
else
{
array.termState[pos] = new TermContext(m_topReaderContext, termState, m_readerContext.Ord, termsEnum.DocFreq, termsEnum.TotalTermFreq);
}
return true;
}
internal int docVisitCount = 0;
internal bool hasCutOff = false;
internal TermsEnum termsEnum;
internal readonly int docCountCutoff, termCountLimit;
internal readonly TermStateByteStart array = new TermStateByteStart(16);
internal BytesRefHash pendingTerms;
}
public override int GetHashCode()
{
const int prime = 1279;
return (int)(prime * termCountCutoff + J2N.BitConversion.DoubleToInt64Bits(docCountPercent));
}
public override bool Equals(object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.GetType() != obj.GetType())
{
return false;
}
ConstantScoreAutoRewrite other = (ConstantScoreAutoRewrite)obj;
if (other.termCountCutoff != termCountCutoff)
{
return false;
}
if (J2N.BitConversion.DoubleToInt64Bits(other.docCountPercent) != J2N.BitConversion.DoubleToInt64Bits(docCountPercent))
{
return false;
}
return true;
}
/// <summary>
/// Special implementation of <see cref="BytesRefHash.BytesStartArray"/> that keeps parallel arrays for <see cref="TermContext"/> </summary>
internal sealed class TermStateByteStart : BytesRefHash.DirectBytesStartArray
{
internal TermContext[] termState;
public TermStateByteStart(int initSize)
: base(initSize)
{
}
public override int[] Init()
{
int[] ord = base.Init();
termState = new TermContext[ArrayUtil.Oversize(ord.Length, RamUsageEstimator.NUM_BYTES_OBJECT_REF)];
Debug.Assert(termState.Length >= ord.Length);
return ord;
}
public override int[] Grow()
{
int[] ord = base.Grow();
if (termState.Length < ord.Length)
{
TermContext[] tmpTermState = new TermContext[ArrayUtil.Oversize(ord.Length, RamUsageEstimator.NUM_BYTES_OBJECT_REF)];
Array.Copy(termState, 0, tmpTermState, 0, termState.Length);
termState = tmpTermState;
}
Debug.Assert(termState.Length >= ord.Length);
return ord;
}
public override int[] Clear()
{
termState = null;
return base.Clear();
}
}
}
}