forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestOmitNorms.cs
More file actions
339 lines (289 loc) · 12.8 KB
/
Copy pathTestOmitNorms.cs
File metadata and controls
339 lines (289 loc) · 12.8 KB
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
using Lucene.Net.Documents;
using Lucene.Net.Index.Extensions;
using NUnit.Framework;
using RandomizedTesting.Generators;
using System;
using Assert = Lucene.Net.TestFramework.Assert;
namespace Lucene.Net.Index
{
/*
* 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 Analyzer = Lucene.Net.Analysis.Analyzer;
using Directory = Lucene.Net.Store.Directory;
using Document = Documents.Document;
using Field = Field;
using FieldType = FieldType;
using LuceneTestCase = Lucene.Net.Util.LuceneTestCase;
using MockAnalyzer = Lucene.Net.Analysis.MockAnalyzer;
using TestUtil = Lucene.Net.Util.TestUtil;
using TextField = TextField;
[TestFixture]
public class TestOmitNorms : LuceneTestCase
{
// Tests whether the DocumentWriter correctly enable the
// omitNorms bit in the FieldInfo
[Test]
public virtual void TestOmitNorms_Mem()
{
Directory ram = NewDirectory();
Analyzer analyzer = new MockAnalyzer(Random);
IndexWriter writer = new IndexWriter(ram, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
Document d = new Document();
// this field will have norms
Field f1 = NewTextField("f1", "this field has norms", Field.Store.NO);
d.Add(f1);
// this field will NOT have norms
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.OmitNorms = true;
Field f2 = NewField("f2", "this field has NO norms in all docs", customType);
d.Add(f2);
writer.AddDocument(d);
writer.ForceMerge(1);
// now we add another document which has term freq for field f2 and not for f1 and verify if the SegmentMerger
// keep things constant
d = new Document();
// Reverse
d.Add(NewField("f1", "this field has norms", customType));
d.Add(NewTextField("f2", "this field has NO norms in all docs", Field.Store.NO));
writer.AddDocument(d);
// force merge
writer.ForceMerge(1);
// flush
writer.Dispose();
SegmentReader reader = GetOnlySegmentReader(DirectoryReader.Open(ram));
FieldInfos fi = reader.FieldInfos;
Assert.IsTrue(fi.FieldInfo("f1").OmitsNorms, "OmitNorms field bit should be set.");
Assert.IsTrue(fi.FieldInfo("f2").OmitsNorms, "OmitNorms field bit should be set.");
reader.Dispose();
ram.Dispose();
}
// Tests whether merging of docs that have different
// omitNorms for the same field works
[Test]
public virtual void TestMixedMerge()
{
Directory ram = NewDirectory();
Analyzer analyzer = new MockAnalyzer(Random);
IndexWriter writer = new IndexWriter(ram, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer)
.SetMaxBufferedDocs(3)
.SetMergePolicy(NewLogMergePolicy(2)));
Document d = new Document();
// this field will have norms
Field f1 = NewTextField("f1", "this field has norms", Field.Store.NO);
d.Add(f1);
// this field will NOT have norms
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.OmitNorms = true;
Field f2 = NewField("f2", "this field has NO norms in all docs", customType);
d.Add(f2);
for (int i = 0; i < 30; i++)
{
writer.AddDocument(d);
}
// now we add another document which has norms for field f2 and not for f1 and verify if the SegmentMerger
// keep things constant
d = new Document();
// Reverese
d.Add(NewField("f1", "this field has norms", customType));
d.Add(NewTextField("f2", "this field has NO norms in all docs", Field.Store.NO));
for (int i = 0; i < 30; i++)
{
writer.AddDocument(d);
}
// force merge
writer.ForceMerge(1);
// flush
writer.Dispose();
SegmentReader reader = GetOnlySegmentReader(DirectoryReader.Open(ram));
FieldInfos fi = reader.FieldInfos;
Assert.IsTrue(fi.FieldInfo("f1").OmitsNorms, "OmitNorms field bit should be set.");
Assert.IsTrue(fi.FieldInfo("f2").OmitsNorms, "OmitNorms field bit should be set.");
reader.Dispose();
ram.Dispose();
}
// Make sure first adding docs that do not omitNorms for
// field X, then adding docs that do omitNorms for that same
// field,
[Test]
public virtual void TestMixedRAM()
{
Directory ram = NewDirectory();
Analyzer analyzer = new MockAnalyzer(Random);
IndexWriter writer = new IndexWriter(ram, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer)
.SetMaxBufferedDocs(10)
.SetMergePolicy(NewLogMergePolicy(2)));
Document d = new Document();
// this field will have norms
Field f1 = NewTextField("f1", "this field has norms", Field.Store.NO);
d.Add(f1);
// this field will NOT have norms
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.OmitNorms = true;
Field f2 = NewField("f2", "this field has NO norms in all docs", customType);
d.Add(f2);
for (int i = 0; i < 5; i++)
{
writer.AddDocument(d);
}
for (int i = 0; i < 20; i++)
{
writer.AddDocument(d);
}
// force merge
writer.ForceMerge(1);
// flush
writer.Dispose();
SegmentReader reader = GetOnlySegmentReader(DirectoryReader.Open(ram));
FieldInfos fi = reader.FieldInfos;
Assert.IsTrue(!fi.FieldInfo("f1").OmitsNorms, "OmitNorms field bit should not be set.");
Assert.IsTrue(fi.FieldInfo("f2").OmitsNorms, "OmitNorms field bit should be set.");
reader.Dispose();
ram.Dispose();
}
private void AssertNoNrm(Directory dir)
{
string[] files = dir.ListAll();
for (int i = 0; i < files.Length; i++)
{
// TODO: this relies upon filenames
Assert.IsFalse(files[i].EndsWith(".nrm", StringComparison.Ordinal) || files[i].EndsWith(".len", StringComparison.Ordinal));
}
}
// Verifies no *.nrm exists when all fields omit norms:
[Test]
public virtual void TestNoNrmFile()
{
Directory ram = NewDirectory();
Analyzer analyzer = new MockAnalyzer(Random);
IndexWriter writer = new IndexWriter(ram, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer)
.SetMaxBufferedDocs(3)
.SetMergePolicy(NewLogMergePolicy()));
LogMergePolicy lmp = (LogMergePolicy)writer.Config.MergePolicy;
lmp.MergeFactor = 2;
lmp.NoCFSRatio = 0.0;
Document d = new Document();
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.OmitNorms = true;
Field f1 = NewField("f1", "this field has no norms", customType);
d.Add(f1);
for (int i = 0; i < 30; i++)
{
writer.AddDocument(d);
}
writer.Commit();
AssertNoNrm(ram);
// force merge
writer.ForceMerge(1);
// flush
writer.Dispose();
AssertNoNrm(ram);
ram.Dispose();
}
/// <summary>
/// Tests various combinations of omitNorms=true/false, the field not existing at all,
/// ensuring that only omitNorms is 'viral'.
/// Internally checks that MultiNorms.norms() is consistent (returns the same bytes)
/// as the fully merged equivalent.
/// </summary>
[Test]
public virtual void TestOmitNormsCombos()
{
// indexed with norms
FieldType customType = new FieldType(TextField.TYPE_STORED);
Field norms = new Field("foo", "a", customType);
// indexed without norms
FieldType customType1 = new FieldType(TextField.TYPE_STORED);
customType1.OmitNorms = true;
Field noNorms = new Field("foo", "a", customType1);
// not indexed, but stored
FieldType customType2 = new FieldType();
customType2.IsStored = true;
Field noIndex = new Field("foo", "a", customType2);
// not indexed but stored, omitNorms is set
FieldType customType3 = new FieldType();
customType3.IsStored = true;
customType3.OmitNorms = true;
Field noNormsNoIndex = new Field("foo", "a", customType3);
// not indexed nor stored (doesn't exist at all, we index a different field instead)
Field emptyNorms = new Field("bar", "a", customType);
Assert.IsNotNull(GetNorms("foo", norms, norms));
Assert.IsNull(GetNorms("foo", norms, noNorms));
Assert.IsNotNull(GetNorms("foo", norms, noIndex));
Assert.IsNotNull(GetNorms("foo", norms, noNormsNoIndex));
Assert.IsNotNull(GetNorms("foo", norms, emptyNorms));
Assert.IsNull(GetNorms("foo", noNorms, noNorms));
Assert.IsNull(GetNorms("foo", noNorms, noIndex));
Assert.IsNull(GetNorms("foo", noNorms, noNormsNoIndex));
Assert.IsNull(GetNorms("foo", noNorms, emptyNorms));
Assert.IsNull(GetNorms("foo", noIndex, noIndex));
Assert.IsNull(GetNorms("foo", noIndex, noNormsNoIndex));
Assert.IsNull(GetNorms("foo", noIndex, emptyNorms));
Assert.IsNull(GetNorms("foo", noNormsNoIndex, noNormsNoIndex));
Assert.IsNull(GetNorms("foo", noNormsNoIndex, emptyNorms));
Assert.IsNull(GetNorms("foo", emptyNorms, emptyNorms));
}
/// <summary>
/// Indexes at least 1 document with f1, and at least 1 document with f2.
/// returns the norms for "field".
/// </summary>
internal virtual NumericDocValues GetNorms(string field, Field f1, Field f2)
{
Directory dir = NewDirectory();
IndexWriterConfig iwc = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetMergePolicy(NewLogMergePolicy());
RandomIndexWriter riw = new RandomIndexWriter(Random, dir, iwc);
// add f1
Document d = new Document();
d.Add(f1);
riw.AddDocument(d);
// add f2
d = new Document();
d.Add(f2);
riw.AddDocument(d);
// add a mix of f1's and f2's
int numExtraDocs = TestUtil.NextInt32(Random, 1, 1000);
for (int i = 0; i < numExtraDocs; i++)
{
d = new Document();
d.Add(Random.NextBoolean() ? f1 : f2);
riw.AddDocument(d);
}
IndexReader ir1 = riw.GetReader();
// todo: generalize
NumericDocValues norms1 = MultiDocValues.GetNormValues(ir1, field);
// fully merge and validate MultiNorms against single segment.
riw.ForceMerge(1);
DirectoryReader ir2 = riw.GetReader();
NumericDocValues norms2 = GetOnlySegmentReader(ir2).GetNormValues(field);
if (norms1 is null)
{
Assert.IsNull(norms2);
}
else
{
for (int docID = 0; docID < ir1.MaxDoc; docID++)
{
Assert.AreEqual(norms1.Get(docID), norms2.Get(docID));
}
}
ir1.Dispose();
ir2.Dispose();
riw.Dispose();
dir.Dispose();
return norms1;
}
}
}