001: package org.apache.lucene.search.function;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.analysis.Analyzer;
021: import org.apache.lucene.analysis.standard.StandardAnalyzer;
022: import org.apache.lucene.document.Document;
023: import org.apache.lucene.document.Field;
024: import org.apache.lucene.document.Fieldable;
025: import org.apache.lucene.index.IndexWriter;
026: import org.apache.lucene.store.Directory;
027: import org.apache.lucene.store.RAMDirectory;
028:
029: import org.apache.lucene.util.LuceneTestCase;
030:
031: /**
032: * Setup for function tests
033: */
034: public abstract class FunctionTestSetup extends LuceneTestCase {
035:
036: /**
037: * Actual score computation order is slightly different than assumptios
038: * this allows for a small amount of variation
039: */
040: public static float TEST_SCORE_TOLERANCE_DELTA = 0.001f;
041:
042: protected static final boolean DBG = false; // change to true for logging to print
043:
044: protected static final int N_DOCS = 17; // select a primary number > 2
045:
046: protected static final String ID_FIELD = "id";
047: protected static final String TEXT_FIELD = "text";
048: protected static final String INT_FIELD = "iii";
049: protected static final String FLOAT_FIELD = "fff";
050:
051: private static final String DOC_TEXT_LINES[] = {
052: "Well, this is just some plain text we use for creating the ",
053: "test documents. It used to be a text from an online collection ",
054: "devoted to first aid, but if there was there an (online) lawyers ",
055: "first aid collection with legal advices, \"it\" might have quite ",
056: "probably advised one not to include \"it\"'s text or the text of ",
057: "any other online collection in one's code, unless one has money ",
058: "that one don't need and one is happy to donate for lawyers ",
059: "charity. Anyhow at some point, rechecking the usage of this text, ",
060: "it became uncertain that this text is free to use, because ",
061: "the web site in the disclaimer of he eBook containing that text ",
062: "was not responding anymore, and at the same time, in projGut, ",
063: "searching for first aid no longer found that eBook as well. ",
064: "So here we are, with a perhaps much less interesting ",
065: "text for the test, but oh much much safer. ", };
066:
067: protected Directory dir;
068: protected Analyzer anlzr;
069:
070: /* @override constructor */
071: public FunctionTestSetup(String name) {
072: super (name);
073: }
074:
075: /* @override */
076: protected void tearDown() throws Exception {
077: super .tearDown();
078: super .tearDown();
079: dir = null;
080: anlzr = null;
081: }
082:
083: /* @override */
084: protected void setUp() throws Exception {
085: super .setUp();
086: // prepare a small index with just a few documents.
087: super .setUp();
088: dir = new RAMDirectory();
089: anlzr = new StandardAnalyzer();
090: IndexWriter iw = new IndexWriter(dir, anlzr);
091: // add docs not exactly in natural ID order, to verify we do check the order of docs by scores
092: int remaining = N_DOCS;
093: boolean done[] = new boolean[N_DOCS];
094: int i = 0;
095: while (remaining > 0) {
096: if (done[i]) {
097: throw new Exception(
098: "to set this test correctly N_DOCS="
099: + N_DOCS
100: + " must be primary and greater than 2!");
101: }
102: addDoc(iw, i);
103: done[i] = true;
104: i = (i + 4) % N_DOCS;
105: remaining--;
106: }
107: iw.close();
108: }
109:
110: private void addDoc(IndexWriter iw, int i) throws Exception {
111: Document d = new Document();
112: Fieldable f;
113: int scoreAndID = i + 1;
114:
115: f = new Field(ID_FIELD, id2String(scoreAndID), Field.Store.YES,
116: Field.Index.UN_TOKENIZED); // for debug purposes
117: f.setOmitNorms(true);
118: d.add(f);
119:
120: f = new Field(TEXT_FIELD, "text of doc" + scoreAndID
121: + textLine(i), Field.Store.NO, Field.Index.TOKENIZED); // for regular search
122: f.setOmitNorms(true);
123: d.add(f);
124:
125: f = new Field(INT_FIELD, "" + scoreAndID, Field.Store.NO,
126: Field.Index.UN_TOKENIZED); // for function scoring
127: f.setOmitNorms(true);
128: d.add(f);
129:
130: f = new Field(FLOAT_FIELD, scoreAndID + ".000", Field.Store.NO,
131: Field.Index.UN_TOKENIZED); // for function scoring
132: f.setOmitNorms(true);
133: d.add(f);
134:
135: iw.addDocument(d);
136: log("added: " + d);
137: }
138:
139: // 17 --> ID00017
140: protected String id2String(int scoreAndID) {
141: String s = "000000000" + scoreAndID;
142: int n = ("" + N_DOCS).length() + 3;
143: int k = s.length() - n;
144: return "ID" + s.substring(k);
145: }
146:
147: // some text line for regular search
148: private String textLine(int docNum) {
149: return DOC_TEXT_LINES[docNum % DOC_TEXT_LINES.length];
150: }
151:
152: // extract expected doc score from its ID Field: "ID7" --> 7.0
153: protected float expectedFieldScore(String docIDFieldVal) {
154: return Float.parseFloat(docIDFieldVal.substring(2));
155: }
156:
157: // debug messages (change DBG to true for anything to print)
158: protected void log(Object o) {
159: if (DBG) {
160: System.out.println(o.toString());
161: }
162: }
163: }
|