1
package org.apache.lucene.search.function;
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
11
* http://www.apache.org/licenses/LICENSE-2.0
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.
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;
34
* Setup for function tests
37
public abstract class FunctionTestSetup extends LuceneTestCase {
40
* Actual score computation order is slightly different than assumptios
41
* this allows for a small amount of variation
43
protected static float TEST_SCORE_TOLERANCE_DELTA = 0.001f;
45
protected static final int N_DOCS = 17; // select a primary number > 2
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";
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. ",
69
protected static Directory dir;
70
protected static Analyzer anlzr;
73
public static void afterClassFunctionTestSetup() throws Exception {
79
protected static void createIndex(boolean doMultiSegment) throws Exception {
81
System.out.println("TEST: setUp");
83
// prepare a small index with just a few documents.
85
anlzr = new MockAnalyzer(random);
86
IndexWriterConfig iwc = newIndexWriterConfig( TEST_VERSION_CURRENT, anlzr).setMergePolicy(newLogMergePolicy());
87
RandomIndexWriter iw = new RandomIndexWriter(random, dir, iwc);
89
iw.w.setMaxBufferedDocs(_TestUtil.nextInt(random, 2, 7));
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];
97
while (remaining > 0) {
99
throw new Exception("to set this test correctly N_DOCS=" + N_DOCS + " must be primary and greater than 2!");
103
i = (i + 4) % N_DOCS;
106
if (!doMultiSegment) {
108
System.out.println("TEST: setUp full merge");
114
System.out.println("TEST: setUp done close");
118
private static void addDoc(RandomIndexWriter iw, int i) throws Exception {
119
Document d = new Document();
121
int scoreAndID = i + 1;
123
f = newField(ID_FIELD, id2String(scoreAndID), Field.Store.YES, Field.Index.NOT_ANALYZED); // for debug purposes
124
f.setOmitNorms(true);
127
f = newField(TEXT_FIELD, "text of doc" + scoreAndID + textLine(i), Field.Store.NO, Field.Index.ANALYZED); // for regular search
128
f.setOmitNorms(true);
131
f = newField(INT_FIELD, "" + scoreAndID, Field.Store.NO, Field.Index.NOT_ANALYZED); // for function scoring
132
f.setOmitNorms(true);
135
f = newField(FLOAT_FIELD, scoreAndID + ".000", Field.Store.NO, Field.Index.NOT_ANALYZED); // for function scoring
136
f.setOmitNorms(true);
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);
151
// some text line for regular search
152
private static String textLine(int docNum) {
153
return DOC_TEXT_LINES[docNum % DOC_TEXT_LINES.length];
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));
161
// debug messages (change DBG to true for anything to print)
162
protected static void log(Object o) {
164
System.out.println(o.toString());