add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / function / FunctionTestSetup.java
1 package org.apache.lucene.search.function;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.analysis.Analyzer;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.document.Fieldable;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.IndexWriterConfig;
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.util.LuceneTestCase;
29 import org.apache.lucene.util._TestUtil;
30 import org.junit.AfterClass;
31 import org.junit.Ignore;
32
33 /**
34  * Setup for function tests
35  */
36 @Ignore
37 public abstract class FunctionTestSetup extends LuceneTestCase {
38
39   /**
40    * Actual score computation order is slightly different than assumptios
41    * this allows for a small amount of variation
42    */
43   protected static float TEST_SCORE_TOLERANCE_DELTA = 0.001f;
44
45   protected static final int N_DOCS = 17; // select a primary number > 2
46
47   protected static final String ID_FIELD = "id";
48   protected static final String TEXT_FIELD = "text";
49   protected static final String INT_FIELD = "iii";
50   protected static final String FLOAT_FIELD = "fff";
51
52   private static final String DOC_TEXT_LINES[] = {
53           "Well, this is just some plain text we use for creating the ",
54           "test documents. It used to be a text from an online collection ",
55           "devoted to first aid, but if there was there an (online) lawyers ",
56           "first aid collection with legal advices, \"it\" might have quite ",
57           "probably advised one not to include \"it\"'s text or the text of ",
58           "any other online collection in one's code, unless one has money ",
59           "that one don't need and one is happy to donate for lawyers ",
60           "charity. Anyhow at some point, rechecking the usage of this text, ",
61           "it became uncertain that this text is free to use, because ",
62           "the web site in the disclaimer of he eBook containing that text ",
63           "was not responding anymore, and at the same time, in projGut, ",
64           "searching for first aid no longer found that eBook as well. ",
65           "So here we are, with a perhaps much less interesting ",
66           "text for the test, but oh much much safer. ",
67   };
68   
69   protected static Directory dir;
70   protected static Analyzer anlzr;
71   
72   @AfterClass
73   public static void afterClassFunctionTestSetup() throws Exception {
74     dir.close();
75     dir = null;
76     anlzr = null;
77   }
78
79   protected static void createIndex(boolean doMultiSegment) throws Exception {
80     if (VERBOSE) {
81       System.out.println("TEST: setUp");
82     }
83     // prepare a small index with just a few documents.  
84     dir = newDirectory();
85     anlzr = new MockAnalyzer(random);
86     IndexWriterConfig iwc = newIndexWriterConfig( TEST_VERSION_CURRENT, anlzr).setMergePolicy(newLogMergePolicy());
87     RandomIndexWriter iw = new RandomIndexWriter(random, dir, iwc);
88     if (doMultiSegment) {
89       iw.w.setMaxBufferedDocs(_TestUtil.nextInt(random, 2, 7));
90     }
91
92     iw.w.setInfoStream(VERBOSE ? System.out : null);
93     // add docs not exactly in natural ID order, to verify we do check the order of docs by scores
94     int remaining = N_DOCS;
95     boolean done[] = new boolean[N_DOCS];
96     int i = 0;
97     while (remaining > 0) {
98       if (done[i]) {
99         throw new Exception("to set this test correctly N_DOCS=" + N_DOCS + " must be primary and greater than 2!");
100       }
101       addDoc(iw, i);
102       done[i] = true;
103       i = (i + 4) % N_DOCS;
104       remaining --;
105     }
106     if (!doMultiSegment) {
107       if (VERBOSE) {
108         System.out.println("TEST: setUp optimize");
109       }
110       iw.optimize();
111     }
112     iw.close();
113     if (VERBOSE) {
114       System.out.println("TEST: setUp done close");
115     }
116   }
117
118   private static void addDoc(RandomIndexWriter iw, int i) throws Exception {
119     Document d = new Document();
120     Fieldable f;
121     int scoreAndID = i + 1;
122
123     f = newField(ID_FIELD, id2String(scoreAndID), Field.Store.YES, Field.Index.NOT_ANALYZED); // for debug purposes
124     f.setOmitNorms(true);
125     d.add(f);
126
127     f = newField(TEXT_FIELD, "text of doc" + scoreAndID + textLine(i), Field.Store.NO, Field.Index.ANALYZED); // for regular search
128     f.setOmitNorms(true);
129     d.add(f);
130
131     f = newField(INT_FIELD, "" + scoreAndID, Field.Store.NO, Field.Index.NOT_ANALYZED); // for function scoring
132     f.setOmitNorms(true);
133     d.add(f);
134
135     f = newField(FLOAT_FIELD, scoreAndID + ".000", Field.Store.NO, Field.Index.NOT_ANALYZED); // for function scoring
136     f.setOmitNorms(true);
137     d.add(f);
138
139     iw.addDocument(d);
140     log("added: " + d);
141   }
142
143   // 17 --> ID00017
144   protected static String id2String(int scoreAndID) {
145     String s = "000000000" + scoreAndID;
146     int n = ("" + N_DOCS).length() + 3;
147     int k = s.length() - n;
148     return "ID" + s.substring(k);
149   }
150
151   // some text line for regular search
152   private static String textLine(int docNum) {
153     return DOC_TEXT_LINES[docNum % DOC_TEXT_LINES.length];
154   }
155
156   // extract expected doc score from its ID Field: "ID7" --> 7.0
157   protected static float expectedFieldScore(String docIDFieldVal) {
158     return Float.parseFloat(docIDFieldVal.substring(2));
159   }
160
161   // debug messages (change DBG to true for anything to print) 
162   protected static void log(Object o) {
163     if (VERBOSE) {
164       System.out.println(o.toString());
165     }
166   }
167 }