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