-
Notifications
You must be signed in to change notification settings - Fork 970
/
TestMultipleIndexFields.java
354 lines (293 loc) · 12.2 KB
/
TestMultipleIndexFields.java
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
/*
* 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.
*/
package org.apache.lucene.facet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.analysis.MockTokenizer;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.util.IOUtils;
import org.junit.Test;
public class TestMultipleIndexFields extends FacetTestCase {
private static final FacetField[] CATEGORIES =
new FacetField[] {
new FacetField("Author", "Mark Twain"),
new FacetField("Author", "Stephen King"),
new FacetField("Author", "Kurt Vonnegut"),
new FacetField("Band", "Rock & Pop", "The Beatles"),
new FacetField("Band", "Punk", "The Ramones"),
new FacetField("Band", "Rock & Pop", "U2"),
new FacetField("Band", "Rock & Pop", "REM"),
new FacetField("Band", "Rock & Pop", "Dave Matthews Band"),
new FacetField("Composer", "Bach"),
};
private FacetsConfig getConfig() {
FacetsConfig config = new FacetsConfig();
config.setHierarchical("Band", true);
return config;
}
@Test
public void testDefault() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw =
new RandomIndexWriter(
random(),
indexDir,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
// Obtain facets results and hand-test them
assertCorrectResults(getTaxonomyFacetCounts(tr, config, sfc));
assertOrdinalsExist("$facets", ir);
iw.close();
IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}
@Test
public void testCustom() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw =
new RandomIndexWriter(
random(),
indexDir,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
config.setIndexFieldName("Author", "$author");
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
Map<String, Facets> facetsMap = new HashMap<>();
facetsMap.put("Author", getTaxonomyFacetCounts(tr, config, sfc, "$author"));
Facets facets = new MultiFacets(facetsMap, getTaxonomyFacetCounts(tr, config, sfc));
// Obtain facets results and hand-test them
assertCorrectResults(facets);
assertOrdinalsExist("$facets", ir);
assertOrdinalsExist("$author", ir);
iw.close();
IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}
@Test
public void testTwoCustomsSameField() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw =
new RandomIndexWriter(
random(),
indexDir,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
config.setIndexFieldName("Band", "$music");
config.setIndexFieldName("Composer", "$music");
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
Map<String, Facets> facetsMap = new HashMap<>();
Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music");
facetsMap.put("Band", facets2);
facetsMap.put("Composer", facets2);
Facets facets = new MultiFacets(facetsMap, getTaxonomyFacetCounts(tr, config, sfc));
// Obtain facets results and hand-test them
assertCorrectResults(facets);
assertOrdinalsExist("$facets", ir);
assertOrdinalsExist("$music", ir);
assertOrdinalsExist("$music", ir);
iw.close();
IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}
private void assertOrdinalsExist(String field, IndexReader ir) throws IOException {
for (LeafReaderContext context : ir.leaves()) {
LeafReader r = context.reader();
if (r.getSortedNumericDocValues(field) != null) {
return; // not all segments must have this DocValues
}
}
fail("no ordinals found for " + field);
}
@Test
public void testDifferentFieldsAndText() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw =
new RandomIndexWriter(
random(),
indexDir,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
config.setIndexFieldName("Band", "$bands");
config.setIndexFieldName("Composer", "$composers");
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
Map<String, Facets> facetsMap = new HashMap<>();
facetsMap.put("Band", getTaxonomyFacetCounts(tr, config, sfc, "$bands"));
facetsMap.put("Composer", getTaxonomyFacetCounts(tr, config, sfc, "$composers"));
Facets facets = new MultiFacets(facetsMap, getTaxonomyFacetCounts(tr, config, sfc));
// Obtain facets results and hand-test them
assertCorrectResults(facets);
assertOrdinalsExist("$facets", ir);
assertOrdinalsExist("$bands", ir);
assertOrdinalsExist("$composers", ir);
iw.close();
IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}
@Test
public void testSomeSameSomeDifferent() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw =
new RandomIndexWriter(
random(),
indexDir,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
config.setIndexFieldName("Band", "$music");
config.setIndexFieldName("Composer", "$music");
config.setIndexFieldName("Author", "$literature");
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
Map<String, Facets> facetsMap = new HashMap<>();
Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music");
facetsMap.put("Band", facets2);
facetsMap.put("Composer", facets2);
facetsMap.put("Author", getTaxonomyFacetCounts(tr, config, sfc, "$literature"));
Facets facets = new MultiFacets(facetsMap, getTaxonomyFacetCounts(tr, config, sfc));
// Obtain facets results and hand-test them
assertCorrectResults(facets);
assertOrdinalsExist("$music", ir);
assertOrdinalsExist("$literature", ir);
iw.close();
IOUtils.close(tr, ir, iw, tw, indexDir, taxoDir);
}
private void assertCorrectResults(Facets facets) throws IOException {
assertEquals(5, facets.getSpecificValue("Band"));
assertEquals(
"dim=Band path=[] value=5 childCount=2\n Rock & Pop (4)\n Punk (1)\n",
facets.getTopChildren(10, "Band").toString());
assertFacetResult(
facets.getAllChildren("Band"),
"Band",
new String[0],
2,
5,
new LabelAndValue[] {
new LabelAndValue("Punk", 1), new LabelAndValue("Rock & Pop", 4),
});
assertEquals(
"dim=Band path=[Rock & Pop] value=4 childCount=4\n The Beatles (1)\n U2 (1)\n REM (1)\n Dave Matthews Band (1)\n",
facets.getTopChildren(10, "Band", "Rock & Pop").toString());
assertFacetResult(
facets.getAllChildren("Band", "Rock & Pop"),
"Band",
new String[] {"Rock & Pop"},
4,
4,
new LabelAndValue[] {
new LabelAndValue("Dave Matthews Band", 1),
new LabelAndValue("REM", 1),
new LabelAndValue("The Beatles", 1),
new LabelAndValue("U2", 1),
});
assertEquals(
"dim=Author path=[] value=3 childCount=3\n Mark Twain (1)\n Stephen King (1)\n Kurt Vonnegut (1)\n",
facets.getTopChildren(10, "Author").toString());
assertFacetResult(
facets.getAllChildren("Author"),
"Author",
new String[0],
3,
3,
new LabelAndValue[] {
new LabelAndValue("Kurt Vonnegut", 1),
new LabelAndValue("Mark Twain", 1),
new LabelAndValue("Stephen King", 1),
});
}
private FacetsCollector performSearch(TaxonomyReader tr, IndexReader ir, IndexSearcher searcher)
throws IOException {
FacetsCollector fc = new FacetsCollector();
FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
return fc;
}
private void seedIndex(TaxonomyWriter tw, RandomIndexWriter iw, FacetsConfig config)
throws IOException {
for (FacetField ff : CATEGORIES) {
Document doc = new Document();
doc.add(ff);
doc.add(new TextField("content", "alpha", Field.Store.YES));
iw.addDocument(config.build(tw, doc));
}
}
}