forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctValuesCollectorTest.cs
More file actions
624 lines (564 loc) · 28.2 KB
/
Copy pathDistinctValuesCollectorTest.cs
File metadata and controls
624 lines (564 loc) · 28.2 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Index.Extensions;
using Lucene.Net.Queries.Function.ValueSources;
using Lucene.Net.Search.Grouping.Function;
using Lucene.Net.Search.Grouping.Terms;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Util.Mutable;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Search.Grouping
{
/*
* 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.
*/
public class DistinctValuesCollectorTest : AbstractGroupingTestCase
{
private readonly static NullComparer nullComparer = new NullComparer();
private readonly string groupField = "author";
//private readonly string dvGroupField = "author_dv"; // LUCENENET NOTE: Not used in Lucene
private readonly string countField = "publisher";
//private readonly string dvCountField = "publisher_dv"; // LUCENENET NOTE: Not used in Lucene
[Test]
public virtual void TestSimple()
{
Random random = Random;
DocValuesType[] dvTypes = new DocValuesType[]{
DocValuesType.NUMERIC,
DocValuesType.BINARY,
DocValuesType.SORTED,
};
Directory dir = NewDirectory();
RandomIndexWriter w = new RandomIndexWriter(
random,
dir,
NewIndexWriterConfig(TEST_VERSION_CURRENT,
new MockAnalyzer(random)).SetMergePolicy(NewLogMergePolicy()));
bool canUseDV = !"Lucene3x".Equals(w.IndexWriter.Config.Codec.Name, StringComparison.Ordinal);
DocValuesType dvType = canUseDV ? dvTypes[random.nextInt(dvTypes.Length)] : DocValuesType.NONE;
Document doc = new Document();
AddField(doc, groupField, "1", dvType);
AddField(doc, countField, "1", dvType);
doc.Add(new TextField("content", "random text", Field.Store.NO));
doc.Add(new StringField("id", "1", Field.Store.NO));
w.AddDocument(doc);
// 1
doc = new Document();
AddField(doc, groupField, "1", dvType);
AddField(doc, countField, "1", dvType);
doc.Add(new TextField("content", "some more random text blob", Field.Store.NO));
doc.Add(new StringField("id", "2", Field.Store.NO));
w.AddDocument(doc);
// 2
doc = new Document();
AddField(doc, groupField, "1", dvType);
AddField(doc, countField, "2", dvType);
doc.Add(new TextField("content", "some more random textual data", Field.Store.NO));
doc.Add(new StringField("id", "3", Field.Store.NO));
w.AddDocument(doc);
w.Commit(); // To ensure a second segment
// 3
doc = new Document();
AddField(doc, groupField, "2", dvType);
doc.Add(new TextField("content", "some random text", Field.Store.NO));
doc.Add(new StringField("id", "4", Field.Store.NO));
w.AddDocument(doc);
// 4
doc = new Document();
AddField(doc, groupField, "3", dvType);
AddField(doc, countField, "1", dvType);
doc.Add(new TextField("content", "some more random text", Field.Store.NO));
doc.Add(new StringField("id", "5", Field.Store.NO));
w.AddDocument(doc);
// 5
doc = new Document();
AddField(doc, groupField, "3", dvType);
AddField(doc, countField, "1", dvType);
doc.Add(new TextField("content", "random blob", Field.Store.NO));
doc.Add(new StringField("id", "6", Field.Store.NO));
w.AddDocument(doc);
// 6 -- no author field
doc = new Document();
doc.Add(new TextField("content", "random word stuck in alot of other text", Field.Store.YES));
AddField(doc, countField, "1", dvType);
doc.Add(new StringField("id", "6", Field.Store.NO));
w.AddDocument(doc);
IndexSearcher indexSearcher = NewSearcher(w.GetReader());
w.Dispose();
var cmp = Comparer<AbstractDistinctValuesCollector.IGroupCount<IComparable>>.Create((groupCount1, groupCount2) =>
{
if (groupCount1.GroupValue is null)
{
if (groupCount2.GroupValue is null)
{
return 0;
}
return -1;
}
else if (groupCount2.GroupValue is null)
{
return 1;
}
else
{
return groupCount1.GroupValue.CompareTo(groupCount2.GroupValue);
}
});
// === Search for content:random
IAbstractFirstPassGroupingCollector<IComparable> firstCollector = CreateRandomFirstPassCollector(dvType, new Sort(), groupField, 10);
indexSearcher.Search(new TermQuery(new Term("content", "random")), firstCollector);
IAbstractDistinctValuesCollector<AbstractDistinctValuesCollector.IGroupCount<IComparable>> distinctValuesCollector
= CreateDistinctCountCollector(firstCollector, groupField, countField, dvType);
indexSearcher.Search(new TermQuery(new Term("content", "random")), distinctValuesCollector);
//var gcs = distinctValuesCollector.Groups as JCG.List<IGroupCount<IComparable>>;
// LUCENENET TODO: Try to work out how to do this without an O(n) operation
var gcs = new JCG.List<AbstractDistinctValuesCollector.IGroupCount<IComparable>>(distinctValuesCollector.Groups);
gcs.Sort(cmp);
assertEquals(4, gcs.Count);
CompareNull(gcs[0].GroupValue);
JCG.List<IComparable> countValues = new JCG.List<IComparable>(gcs[0].UniqueValues);
assertEquals(1, countValues.size());
Compare("1", countValues[0]);
Compare("1", gcs[1].GroupValue);
countValues = new JCG.List<IComparable>(gcs[1].UniqueValues);
countValues.Sort(nullComparer);
assertEquals(2, countValues.size());
Compare("1", countValues[0]);
Compare("2", countValues[1]);
Compare("2", gcs[2].GroupValue);
countValues = new JCG.List<IComparable>(gcs[2].UniqueValues);
assertEquals(1, countValues.size());
CompareNull(countValues[0]);
Compare("3", gcs[3].GroupValue);
countValues = new JCG.List<IComparable>(gcs[3].UniqueValues);
assertEquals(1, countValues.size());
Compare("1", countValues[0]);
// === Search for content:some
firstCollector = CreateRandomFirstPassCollector(dvType, new Sort(), groupField, 10);
indexSearcher.Search(new TermQuery(new Term("content", "some")), firstCollector);
distinctValuesCollector = CreateDistinctCountCollector(firstCollector, groupField, countField, dvType);
indexSearcher.Search(new TermQuery(new Term("content", "some")), distinctValuesCollector);
// LUCENENET TODO: Try to work out how to do this without an O(n) operation
//gcs = distinctValuesCollector.Groups as JCG.List<IGroupCount<IComparable>>;
gcs = new JCG.List<AbstractDistinctValuesCollector.IGroupCount<IComparable>>(distinctValuesCollector.Groups);
gcs.Sort(cmp);
assertEquals(3, gcs.Count);
Compare("1", gcs[0].GroupValue);
countValues = new JCG.List<IComparable>(gcs[0].UniqueValues);
assertEquals(2, countValues.size());
countValues.Sort(nullComparer);
Compare("1", countValues[0]);
Compare("2", countValues[1]);
Compare("2", gcs[1].GroupValue);
countValues = new JCG.List<IComparable>(gcs[1].UniqueValues);
assertEquals(1, countValues.size());
CompareNull(countValues[0]);
Compare("3", gcs[2].GroupValue);
countValues = new JCG.List<IComparable>(gcs[2].UniqueValues);
assertEquals(1, countValues.size());
Compare("1", countValues[0]);
// === Search for content:blob
firstCollector = CreateRandomFirstPassCollector(dvType, new Sort(), groupField, 10);
indexSearcher.Search(new TermQuery(new Term("content", "blob")), firstCollector);
distinctValuesCollector = CreateDistinctCountCollector(firstCollector, groupField, countField, dvType);
indexSearcher.Search(new TermQuery(new Term("content", "blob")), distinctValuesCollector);
// LUCENENET TODO: Try to work out how to do this without an O(n) operation
//gcs = distinctValuesCollector.Groups as JCG.List<IGroupCount<IComparable>>;
gcs = new JCG.List<AbstractDistinctValuesCollector.IGroupCount<IComparable>>(distinctValuesCollector.Groups);
gcs.Sort(cmp);
assertEquals(2, gcs.Count);
Compare("1", gcs[0].GroupValue);
countValues = new JCG.List<IComparable>(gcs[0].UniqueValues);
// B/c the only one document matched with blob inside the author 1 group
assertEquals(1, countValues.Count);
Compare("1", countValues[0]);
Compare("3", gcs[1].GroupValue);
countValues = new JCG.List<IComparable>(gcs[1].UniqueValues);
assertEquals(1, countValues.Count);
Compare("1", countValues[0]);
indexSearcher.IndexReader.Dispose();
dir.Dispose();
}
[Test]
public virtual void TestRandom()
{
Random random = Random;
int numberOfRuns = TestUtil.NextInt32(random, 3, 6);
for (int indexIter = 0; indexIter < numberOfRuns; indexIter++)
{
IndexContext context = CreateIndexContext();
for (int searchIter = 0; searchIter < 100; searchIter++)
{
IndexSearcher searcher = NewSearcher(context.indexReader);
bool useDv = context.dvType != DocValuesType.NONE && random.nextBoolean();
DocValuesType dvType = useDv ? context.dvType : DocValuesType.NONE;
string term = context.contentStrings[random.nextInt(context.contentStrings.Length)];
Sort groupSort = new Sort(new SortField("id", SortFieldType.STRING));
int topN = 1 + random.nextInt(10);
IList<AbstractDistinctValuesCollector.IGroupCount<IComparable>> expectedResult = CreateExpectedResult(context, term, groupSort, topN);
IAbstractFirstPassGroupingCollector<IComparable> firstCollector = CreateRandomFirstPassCollector(dvType, groupSort, groupField, topN);
searcher.Search(new TermQuery(new Term("content", term)), firstCollector);
IAbstractDistinctValuesCollector<AbstractDistinctValuesCollector.IGroupCount<IComparable>> distinctValuesCollector
= CreateDistinctCountCollector(firstCollector, groupField, countField, dvType);
searcher.Search(new TermQuery(new Term("content", term)), distinctValuesCollector);
// LUCENENET TODO: Try to work out how to do this without an O(n) operation
JCG.List<AbstractDistinctValuesCollector.IGroupCount<IComparable>> actualResult = new JCG.List<AbstractDistinctValuesCollector.IGroupCount<IComparable>>(distinctValuesCollector.Groups);
if (Verbose)
{
Console.WriteLine("Index iter=" + indexIter);
Console.WriteLine("Search iter=" + searchIter);
Console.WriteLine("1st pass collector class name=" + firstCollector.GetType().Name);
Console.WriteLine("2nd pass collector class name=" + distinctValuesCollector.GetType().Name);
Console.WriteLine("Search term=" + term);
Console.WriteLine("DVType=" + dvType);
Console.WriteLine("1st pass groups=" + firstCollector.GetTopGroups(0, false).toString());
Console.WriteLine("Expected:");
PrintGroups(expectedResult);
Console.WriteLine("Actual:");
PrintGroups(actualResult);
Console.Out.Flush();
}
assertEquals(expectedResult.Count, actualResult.Count);
for (int i = 0; i < expectedResult.size(); i++)
{
AbstractDistinctValuesCollector.IGroupCount<IComparable> expected = expectedResult[i];
AbstractDistinctValuesCollector.IGroupCount<IComparable> actual = actualResult[i];
AssertValues(expected.GroupValue, actual.GroupValue);
assertEquals(expected.UniqueValues.Count(), actual.UniqueValues.Count());
JCG.List<IComparable> expectedUniqueValues = new JCG.List<IComparable>(expected.UniqueValues);
expectedUniqueValues.Sort(nullComparer);
JCG.List<IComparable> actualUniqueValues = new JCG.List<IComparable>(actual.UniqueValues);
actualUniqueValues.Sort(nullComparer);
for (int j = 0; j < expectedUniqueValues.size(); j++)
{
AssertValues(expectedUniqueValues[j], actualUniqueValues[j]);
}
}
}
context.indexReader.Dispose();
context.directory.Dispose();
}
}
private void PrintGroups(IList<AbstractDistinctValuesCollector.IGroupCount<IComparable>> results)
{
for (int i = 0; i < results.size(); i++)
{
var group = results[i];
object gv = group.GroupValue;
if (gv is BytesRef bytesRef)
{
Console.WriteLine(i + ": groupValue=" + bytesRef.Utf8ToString());
}
else
{
Console.WriteLine(i + ": groupValue=" + gv);
}
foreach (object o in group.UniqueValues)
{
if (o is BytesRef bytesRef2)
{
Console.WriteLine(" " + bytesRef2.Utf8ToString());
}
else
{
Console.WriteLine(" " + o);
}
}
}
}
private void AssertValues(object expected, object actual)
{
if (expected is null)
{
CompareNull(actual);
}
else
{
Compare(((BytesRef)expected).Utf8ToString(), actual);
}
}
private void Compare(string expected, object groupValue)
{
if (typeof(BytesRef).IsAssignableFrom(groupValue.GetType()))
{
assertEquals(expected, ((BytesRef)groupValue).Utf8ToString());
}
else if (typeof(double).IsAssignableFrom(groupValue.GetType()))
{
assertEquals(double.Parse(expected, CultureInfo.InvariantCulture), groupValue);
}
else if (typeof(long).IsAssignableFrom(groupValue.GetType()))
{
assertEquals(long.Parse(expected, CultureInfo.InvariantCulture), groupValue);
}
else if (typeof(MutableValue).IsAssignableFrom(groupValue.GetType()))
{
MutableValueStr mutableValue = new MutableValueStr();
mutableValue.Value = new BytesRef(expected);
assertEquals(mutableValue, groupValue);
}
else
{
fail();
}
}
private void CompareNull(object groupValue)
{
if (groupValue is null)
{
return; // term based impl...
}
// DV based impls..
if (typeof(BytesRef).IsAssignableFrom(groupValue.GetType()))
{
assertEquals("", ((BytesRef)groupValue).Utf8ToString());
}
else if (typeof(double).IsAssignableFrom(groupValue.GetType()))
{
assertEquals(0.0d, groupValue);
}
else if (typeof(long).IsAssignableFrom(groupValue.GetType()))
{
assertEquals(0L, groupValue);
// Function based impl
}
else if (typeof(MutableValue).IsAssignableFrom(groupValue.GetType()))
{
assertFalse(((MutableValue)groupValue).Exists);
}
else
{
fail();
}
}
private void AddField(Document doc, string field, string value, DocValuesType type)
{
doc.Add(new StringField(field, value, Field.Store.YES));
if (type == DocValuesType.NONE)
{
return;
}
string dvField = field + "_dv";
Field valuesField = null;
switch (type)
{
case DocValuesType.NUMERIC:
valuesField = new NumericDocValuesField(dvField, int.Parse(value, CultureInfo.InvariantCulture));
break;
case DocValuesType.BINARY:
valuesField = new BinaryDocValuesField(dvField, new BytesRef(value));
break;
case DocValuesType.SORTED:
valuesField = new SortedDocValuesField(dvField, new BytesRef(value));
break;
}
doc.Add(valuesField);
}
private IAbstractDistinctValuesCollector<AbstractDistinctValuesCollector.IGroupCount<T>> CreateDistinctCountCollector<T>(IAbstractFirstPassGroupingCollector<T> firstPassGroupingCollector,
string groupField,
string countField,
DocValuesType dvType)
{
Random random = Random;
IEnumerable<ISearchGroup<T>> searchGroups = firstPassGroupingCollector.GetTopGroups(0, false);
if (typeof(FunctionFirstPassGroupingCollector<MutableValue>).IsAssignableFrom(firstPassGroupingCollector.GetType())) // LUCENENET Specific type for generic must be specified.
{
return (IAbstractDistinctValuesCollector<AbstractDistinctValuesCollector.IGroupCount<T>>)new FunctionDistinctValuesCollector(new Hashtable(), new BytesRefFieldSource(groupField), new BytesRefFieldSource(countField), searchGroups as IEnumerable<ISearchGroup<MutableValue>>);
}
else
{
return (IAbstractDistinctValuesCollector<AbstractDistinctValuesCollector.IGroupCount<T>>)new TermDistinctValuesCollector(groupField, countField, searchGroups as IEnumerable<ISearchGroup<BytesRef>>);
}
}
private IAbstractFirstPassGroupingCollector<IComparable> CreateRandomFirstPassCollector(DocValuesType dvType, Sort groupSort, string groupField, int topNGroups)
{
Random random = Random;
if (dvType != DocValuesType.NONE)
{
if (random.nextBoolean())
{
return new FunctionFirstPassGroupingCollector<MutableValue>(new BytesRefFieldSource(groupField), new Hashtable(), groupSort, topNGroups) // LUCENENET Specific type for generic must be specified.
as IAbstractFirstPassGroupingCollector<IComparable>;
}
else
{
return new TermFirstPassGroupingCollector(groupField, groupSort, topNGroups)
as IAbstractFirstPassGroupingCollector<IComparable>;
}
}
else
{
if (random.nextBoolean())
{
return new FunctionFirstPassGroupingCollector<MutableValue>(new BytesRefFieldSource(groupField), new Hashtable(), groupSort, topNGroups) // LUCENENET Specific type for generic must be specified.
as IAbstractFirstPassGroupingCollector<IComparable>;
}
else
{
return new TermFirstPassGroupingCollector(groupField, groupSort, topNGroups)
as IAbstractFirstPassGroupingCollector<IComparable>;
}
}
}
internal class GroupCount : AbstractDistinctValuesCollector.GroupCount<BytesRef>
{
internal GroupCount(BytesRef groupValue, IEnumerable<BytesRef> uniqueValues)
: base(groupValue)
{
((ISet<BytesRef>)this.UniqueValues).UnionWith(uniqueValues);
}
}
private IList<AbstractDistinctValuesCollector.IGroupCount<IComparable>> CreateExpectedResult(IndexContext context, string term, Sort groupSort, int topN)
{
JCG.List<AbstractDistinctValuesCollector.IGroupCount<IComparable>> result = new JCG.List<AbstractDistinctValuesCollector.IGroupCount<IComparable>>();
IDictionary<string, ISet<string>> groupCounts = context.searchTermToGroupCounts[term];
int i = 0;
foreach (string group in groupCounts.Keys)
{
if (topN <= i++)
{
break;
}
ISet<BytesRef> uniqueValues = new JCG.HashSet<BytesRef>();
foreach (string val in groupCounts[group])
{
uniqueValues.Add(val != null ? new BytesRef(val) : null);
}
var gc = new GroupCount(group != null ? new BytesRef(group) : (BytesRef)null, uniqueValues);
result.Add(gc);
}
return result;
}
private IndexContext CreateIndexContext()
{
Random random = Random;
DocValuesType[] dvTypes = new DocValuesType[]{
DocValuesType.BINARY,
DocValuesType.SORTED
};
Directory dir = NewDirectory();
RandomIndexWriter w = new RandomIndexWriter(
random,
dir,
NewIndexWriterConfig(TEST_VERSION_CURRENT,
new MockAnalyzer(random)).SetMergePolicy(NewLogMergePolicy())
);
bool canUseDV = !"Lucene3x".Equals(w.IndexWriter.Config.Codec.Name, StringComparison.Ordinal);
DocValuesType dvType = canUseDV ? dvTypes[random.nextInt(dvTypes.Length)] : DocValuesType.NONE;
int numDocs = 86 + random.nextInt(1087) * RandomMultiplier;
string[] groupValues = new string[numDocs / 5];
string[] countValues = new string[numDocs / 10];
for (int i = 0; i < groupValues.Length; i++)
{
groupValues[i] = GenerateRandomNonEmptyString();
}
for (int i = 0; i < countValues.Length; i++)
{
countValues[i] = GenerateRandomNonEmptyString();
}
JCG.List<string> contentStrings = new JCG.List<string>();
IDictionary<string, IDictionary<string, ISet<string>>> searchTermToGroupCounts = new JCG.Dictionary<string, IDictionary<string, ISet<string>>>();
for (int i = 1; i <= numDocs; i++)
{
string groupValue = random.nextInt(23) == 14 ? null : groupValues[random.nextInt(groupValues.Length)];
string countValue = random.nextInt(21) == 13 ? null : countValues[random.nextInt(countValues.Length)];
string content = "random" + random.nextInt(numDocs / 20);
if (!searchTermToGroupCounts.TryGetValue(content, out var groupToCounts))
{
// Groups sort always DOCID asc...
// LUCENENET specific: OrderedDictionary<TKey, TValue> is a replacement for LinkedHashMap<K, V> in the JDK
searchTermToGroupCounts.Add(content, groupToCounts = new JCG.OrderedDictionary<string, ISet<string>>());
contentStrings.Add(content);
}
if (!groupToCounts.TryGetValue(groupValue, out var countsVals))
{
groupToCounts.Add(groupValue, countsVals = new JCG.HashSet<string>());
}
countsVals.Add(countValue);
Document doc = new Document();
doc.Add(new StringField("id", string.Format(CultureInfo.InvariantCulture, "{0:D9}", i), Field.Store.YES));
if (groupValue != null)
{
AddField(doc, groupField, groupValue, dvType);
}
if (countValue != null)
{
AddField(doc, countField, countValue, dvType);
}
doc.Add(new TextField("content", content, Field.Store.YES));
w.AddDocument(doc);
}
DirectoryReader reader = w.GetReader();
if (Verbose)
{
for (int docID = 0; docID < reader.MaxDoc; docID++)
{
Document doc = reader.Document(docID);
Console.WriteLine("docID=" + docID + " id=" + doc.Get("id") + " content=" + doc.Get("content") + " author=" + doc.Get("author") + " publisher=" + doc.Get("publisher"));
}
}
w.Dispose();
return new IndexContext(dir, reader, dvType, searchTermToGroupCounts, contentStrings.ToArray(/*new String[contentStrings.size()]*/));
}
internal class IndexContext
{
internal readonly Directory directory;
internal readonly DirectoryReader indexReader;
internal readonly DocValuesType dvType;
internal readonly IDictionary<string, IDictionary<string, ISet<string>>> searchTermToGroupCounts;
internal readonly string[] contentStrings;
internal IndexContext(Directory directory, DirectoryReader indexReader, DocValuesType dvType,
IDictionary<string, IDictionary<string, ISet<string>>> searchTermToGroupCounts, string[] contentStrings)
{
this.directory = directory;
this.indexReader = indexReader;
this.dvType = dvType;
this.searchTermToGroupCounts = searchTermToGroupCounts;
this.contentStrings = contentStrings;
}
}
internal class NullComparer : IComparer<IComparable>
{
public int Compare(IComparable a, IComparable b)
{
if (a == b)
{
return 0;
}
else if (a is null)
{
return -1;
}
else if (b is null)
{
return 1;
}
else
{
return a.CompareTo(b);
}
}
}
}
}