1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
4 import java.util.Arrays;
7 import org.apache.lucene.DocumentBuilder.DocumentBuilderException;
8 import org.apache.lucene.index.CorruptIndexException;
9 import org.apache.lucene.index.IndexWriterConfig;
10 import org.apache.lucene.index.RandomIndexWriter;
11 import org.apache.lucene.util._TestUtil;
13 import org.apache.lucene.analysis.Analyzer;
14 import org.apache.lucene.facet.FacetTestBase;
15 import org.apache.lucene.facet.index.params.FacetIndexingParams;
16 import org.apache.lucene.facet.search.params.CountFacetRequest;
17 import org.apache.lucene.facet.search.params.FacetSearchParams;
18 import org.apache.lucene.facet.taxonomy.CategoryPath;
19 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
22 * Licensed to the Apache Software Foundation (ASF) under one or more
23 * contributor license agreements. See the NOTICE file distributed with
24 * this work for additional information regarding copyright ownership.
25 * The ASF licenses this file to You under the Apache License, Version 2.0
26 * (the "License"); you may not use this file except in compliance with
27 * the License. You may obtain a copy of the License at
29 * http://www.apache.org/licenses/LICENSE-2.0
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
38 public abstract class BaseTestTopK extends FacetTestBase {
40 protected static final String ALPHA = "alpha";
41 protected static final String BETA = "beta";
43 /** partition sizes on which the tests are run */
44 protected static int[] partitionSizes = new int[] { 2, 3, 100, Integer.MAX_VALUE };
46 /** Categories are generated from range [0,maxCategory) */
47 protected static int maxCategory = 5000;
48 private static final int categoriesPow2 = maxCategory * maxCategory;
54 protected void populateIndex(RandomIndexWriter iw, TaxonomyWriter taxo,
55 FacetIndexingParams iParams) throws IOException,
56 DocumentBuilderException, CorruptIndexException {
58 super.populateIndex(iw, taxo, iParams);
61 /** prepare the next random int */
62 private void nextInt(int doc) {
63 if (currDoc == doc ) {
67 nextInt = random.nextInt(categoriesPow2);
68 nextInt = (int)Math.sqrt(nextInt);
72 protected String getContent(int doc) {
74 if (random.nextDouble() > 0.1) {
75 return ALPHA + ' ' + BETA;
81 protected List<CategoryPath> getCategories(int doc) {
83 CategoryPath cp = new CategoryPath(
85 Integer.toString(nextInt / 1000),
86 Integer.toString(nextInt / 100),
87 Integer.toString(nextInt / 10));
89 System.out.println("Adding CP: " + cp.toString());
91 return Arrays.asList(new CategoryPath[] { cp });
94 protected FacetSearchParams searchParamsWithRequests(int numResults) {
95 return searchParamsWithRequests(numResults, Integer.MAX_VALUE);
98 protected FacetSearchParams searchParamsWithRequests(int numResults, int partitionSize) {
99 FacetSearchParams res = getFacetedSearchParams(partitionSize);
100 res.addFacetRequest(new CountFacetRequest(new CategoryPath("a"), numResults));
101 res.addFacetRequest(new CountFacetRequest(new CategoryPath("a", "1"), numResults));
102 res.addFacetRequest(new CountFacetRequest(new CategoryPath("a", "1", "10"), numResults));
103 res.addFacetRequest(new CountFacetRequest(new CategoryPath("a", "2", "26", "267"), numResults));
108 protected int numDocsToIndex() {
113 protected IndexWriterConfig getIndexWriterConfig(Analyzer analyzer) {
114 return super.getIndexWriterConfig(analyzer).setMaxBufferedDocs(_TestUtil.nextInt(random, 500, 10000));