add --shared
[pylucene.git] / lucene-java-3.4.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.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;
12
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;
20
21 /**
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
28  *
29  *     http://www.apache.org/licenses/LICENSE-2.0
30  *
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.
36  */
37
38 public abstract class BaseTestTopK extends FacetTestBase {
39
40   protected static final String ALPHA = "alpha";
41   protected static final String BETA  = "beta";
42
43   /** partition sizes on which the tests are run */
44   protected static int[] partitionSizes = new int[] { 2, 3, 100, Integer.MAX_VALUE };
45
46   /** Categories are generated from range [0,maxCategory) */
47   protected static int maxCategory = 5000;
48   private static final int categoriesPow2 = maxCategory * maxCategory;
49
50   private int currDoc;
51   private int nextInt;
52
53   @Override
54   protected void populateIndex(RandomIndexWriter iw, TaxonomyWriter taxo,
55       FacetIndexingParams iParams) throws IOException,
56       DocumentBuilderException, CorruptIndexException {
57     currDoc = -1;
58     super.populateIndex(iw, taxo, iParams);
59   }
60   
61   /** prepare the next random int */
62   private void nextInt(int doc) {
63     if (currDoc == doc ) {
64       return;
65     }
66     currDoc = doc;
67     nextInt = random.nextInt(categoriesPow2);
68     nextInt = (int)Math.sqrt(nextInt);
69   }
70   
71   @Override
72   protected String getContent(int doc) {
73     nextInt(doc);
74     if (random.nextDouble() > 0.1) {
75       return ALPHA + ' ' + BETA;
76     }
77     return ALPHA;
78   }
79   
80   @Override
81   protected List<CategoryPath> getCategories(int doc) {
82     nextInt(doc);
83     CategoryPath cp = new CategoryPath(
84         "a", 
85         Integer.toString(nextInt / 1000), 
86         Integer.toString(nextInt / 100), 
87         Integer.toString(nextInt / 10));
88     if (VERBOSE) {
89       System.out.println("Adding CP: " + cp.toString());
90     }
91     return Arrays.asList(new CategoryPath[] { cp });
92   }
93
94   protected FacetSearchParams searchParamsWithRequests(int numResults) {
95     return searchParamsWithRequests(numResults, Integer.MAX_VALUE);
96   }
97   
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));
104     return res;
105   }
106
107   @Override
108   protected int numDocsToIndex() {
109     return 20000;
110   }
111
112   @Override
113   protected IndexWriterConfig getIndexWriterConfig(Analyzer analyzer) {
114     return super.getIndexWriterConfig(analyzer).setMaxBufferedDocs(_TestUtil.nextInt(random, 500, 10000));
115   }
116 }