-
Notifications
You must be signed in to change notification settings - Fork 624
/
SearchEquivalenceTestBase.cs
213 lines (193 loc) · 7.69 KB
/
SearchEquivalenceTestBase.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
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Util.Automaton;
using RandomizedTesting.Generators;
using System;
using System.Globalization;
using System.Text;
using Assert = Lucene.Net.TestFramework.Assert;
using BitSet = Lucene.Net.Util.OpenBitSet;
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.
*/
/// <summary>
/// Simple base class for checking search equivalence.
/// Extend it, and write tests that create <see cref="RandomTerm()"/>s
/// (all terms are single characters a-z), and use
/// <see cref="AssertSameSet(Query, Query)"/> and
/// <see cref="AssertSubsetOf(Query, Query)"/>.
/// </summary>
public abstract class SearchEquivalenceTestBase : LuceneTestCase
{
protected static IndexSearcher m_s1, m_s2;
protected static Directory m_directory;
protected static IndexReader m_reader;
protected static Analyzer m_analyzer;
protected static string m_stopword; // we always pick a character as a stopword
[NUnit.Framework.OneTimeSetUp]
public override void BeforeClass()
{
base.BeforeClass();
Random random = Random;
m_directory = NewDirectory();
m_stopword = "" + GetRandomChar();
CharacterRunAutomaton stopset = new CharacterRunAutomaton(BasicAutomata.MakeString(m_stopword));
m_analyzer = new MockAnalyzer(random, MockTokenizer.WHITESPACE, false, stopset);
RandomIndexWriter iw = new RandomIndexWriter(random, m_directory, m_analyzer);
Document doc = new Document();
Field id = new StringField("id", "", Field.Store.NO);
Field field = new TextField("field", "", Field.Store.NO);
doc.Add(id);
doc.Add(field);
// index some docs
int numDocs = AtLeast(1000);
for (int i = 0; i < numDocs; i++)
{
id.SetStringValue(Convert.ToString(i, CultureInfo.InvariantCulture));
field.SetStringValue(RandomFieldContents());
iw.AddDocument(doc);
}
// delete some docs
int numDeletes = numDocs / 20;
for (int i = 0; i < numDeletes; i++)
{
Term toDelete = new Term("id", Convert.ToString(random.Next(numDocs), CultureInfo.InvariantCulture));
if (random.NextBoolean())
{
iw.DeleteDocuments(toDelete);
}
else
{
iw.DeleteDocuments(new TermQuery(toDelete));
}
}
m_reader = iw.GetReader();
m_s1 = NewSearcher(m_reader);
m_s2 = NewSearcher(m_reader);
iw.Dispose();
}
[NUnit.Framework.OneTimeTearDown]
public override void AfterClass()
{
m_reader.Dispose();
m_directory.Dispose();
m_analyzer.Dispose();
m_reader = null;
m_directory = null;
m_analyzer = null;
m_s1 = m_s2 = null;
base.AfterClass();
}
/// <summary>
/// Populate a field with random contents.
/// Terms should be single characters in lowercase (a-z)
/// tokenization can be assumed to be on whitespace.
/// </summary>
internal static string RandomFieldContents()
{
// TODO: zipf-like distribution
StringBuilder sb = new StringBuilder();
int numTerms = Random.Next(15);
for (int i = 0; i < numTerms; i++)
{
if (sb.Length > 0)
{
sb.Append(' '); // whitespace
}
sb.Append(GetRandomChar());
}
return sb.ToString();
}
/// <summary>
/// Returns random character (a-z)
/// </summary>
internal static char GetRandomChar()
{
return (char)TestUtil.NextInt32(Random, 'a', 'z');
}
/// <summary>
/// Returns a term suitable for searching.
/// Terms are single characters in lowercase (a-z).
/// </summary>
protected virtual Term RandomTerm()
{
return new Term("field", "" + GetRandomChar());
}
/// <summary>
/// Returns a random filter over the document set.
/// </summary>
protected virtual Filter RandomFilter()
{
return new QueryWrapperFilter(TermRangeQuery.NewStringRange("field", "a", "" + GetRandomChar(), true, true));
}
/// <summary>
/// Asserts that the documents returned by <paramref name="q1"/>
/// are the same as of those returned by <paramref name="q2"/>.
/// </summary>
public virtual void AssertSameSet(Query q1, Query q2)
{
AssertSubsetOf(q1, q2);
AssertSubsetOf(q2, q1);
}
/// <summary>
/// Asserts that the documents returned by <paramref name="q1"/>
/// are a subset of those returned by <paramref name="q2"/>.
/// </summary>
public virtual void AssertSubsetOf(Query q1, Query q2)
{
// test without a filter
AssertSubsetOf(q1, q2, null);
// test with a filter (this will sometimes cause advance'ing enough to test it)
AssertSubsetOf(q1, q2, RandomFilter());
}
/// <summary>
/// Asserts that the documents returned by <paramref name="q1"/>
/// are a subset of those returned by <paramref name="q2"/>.
/// <para/>
/// Both queries will be filtered by <paramref name="filter"/>.
/// </summary>
protected virtual void AssertSubsetOf(Query q1, Query q2, Filter filter)
{
// TRUNK ONLY: test both filter code paths
if (filter != null && Random.NextBoolean())
{
q1 = new FilteredQuery(q1, filter, TestUtil.RandomFilterStrategy(Random));
q2 = new FilteredQuery(q2, filter, TestUtil.RandomFilterStrategy(Random));
filter = null;
}
// not efficient, but simple!
TopDocs td1 = m_s1.Search(q1, filter, m_reader.MaxDoc);
TopDocs td2 = m_s2.Search(q2, filter, m_reader.MaxDoc);
Assert.IsTrue(td1.TotalHits <= td2.TotalHits);
// fill the superset into a bitset
var bitset = new BitSet(td2.ScoreDocs.Length);
for (int i = 0; i < td2.ScoreDocs.Length; i++)
{
bitset.Set(td2.ScoreDocs[i].Doc);
}
// check in the subset, that every bit was set by the super
for (int i = 0; i < td1.ScoreDocs.Length; i++)
{
Assert.IsTrue(bitset.Get(td1.ScoreDocs[i].Doc));
}
}
}
}