~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/backwards/src/test/org/apache/lucene/search/function/TestOrdValues.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.index.CorruptIndexException;
21
 
import org.apache.lucene.index.IndexReader;
22
 
import org.apache.lucene.search.*;
23
 
import org.junit.BeforeClass;
24
 
import org.junit.Test;
25
 
 
26
 
/**
27
 
 * Test search based on OrdFieldSource and ReverseOrdFieldSource.
28
 
 * <p/>
29
 
 * Tests here create an index with a few documents, each having
30
 
 * an indexed "id" field.
31
 
 * The ord values of this field are later used for scoring.
32
 
 * <p/>
33
 
 * The order tests use Hits to verify that docs are ordered as expected.
34
 
 * <p/>
35
 
 * The exact score tests use TopDocs top to verify the exact score.
36
 
 */
37
 
public class TestOrdValues extends FunctionTestSetup {
38
 
 
39
 
  @BeforeClass
40
 
  public static void beforeClass() throws Exception {
41
 
    createIndex(false);
42
 
  }
43
 
 
44
 
  /**
45
 
   * Test OrdFieldSource
46
 
   */
47
 
  @Test
48
 
  public void testOrdFieldRank() throws CorruptIndexException, Exception {
49
 
    doTestRank(ID_FIELD, true);
50
 
  }
51
 
 
52
 
  /**
53
 
   * Test ReverseOrdFieldSource
54
 
   */
55
 
  @Test
56
 
  public void testReverseOrdFieldRank() throws CorruptIndexException, Exception {
57
 
    doTestRank(ID_FIELD, false);
58
 
  }
59
 
 
60
 
  // Test that queries based on reverse/ordFieldScore scores correctly
61
 
  private void doTestRank(String field, boolean inOrder) throws CorruptIndexException, Exception {
62
 
    IndexSearcher s = new IndexSearcher(dir, true);
63
 
    ValueSource vs;
64
 
    if (inOrder) {
65
 
      vs = new OrdFieldSource(field);
66
 
    } else {
67
 
      vs = new ReverseOrdFieldSource(field);
68
 
    }
69
 
 
70
 
    Query q = new ValueSourceQuery(vs);
71
 
    log("test: " + q);
72
 
    QueryUtils.check(random, q, s);
73
 
    ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
74
 
    assertEquals("All docs should be matched!", N_DOCS, h.length);
75
 
    String prevID = inOrder
76
 
            ? "IE"   // greater than all ids of docs in this test ("ID0001", etc.)
77
 
            : "IC";  // smaller than all ids of docs in this test ("ID0001", etc.)
78
 
 
79
 
    for (int i = 0; i < h.length; i++) {
80
 
      String resID = s.doc(h[i].doc).get(ID_FIELD);
81
 
      log(i + ".   score=" + h[i].score + "  -  " + resID);
82
 
      log(s.explain(q, h[i].doc));
83
 
      if (inOrder) {
84
 
        assertTrue("res id " + resID + " should be < prev res id " + prevID, resID.compareTo(prevID) < 0);
85
 
      } else {
86
 
        assertTrue("res id " + resID + " should be > prev res id " + prevID, resID.compareTo(prevID) > 0);
87
 
      }
88
 
      prevID = resID;
89
 
    }
90
 
    s.close();
91
 
  }
92
 
 
93
 
  /**
94
 
   * Test exact score for OrdFieldSource
95
 
   */
96
 
  @Test
97
 
  public void testOrdFieldExactScore() throws CorruptIndexException, Exception {
98
 
    doTestExactScore(ID_FIELD, true);
99
 
  }
100
 
 
101
 
  /**
102
 
   * Test exact score for ReverseOrdFieldSource
103
 
   */
104
 
  @Test
105
 
  public void testReverseOrdFieldExactScore() throws CorruptIndexException, Exception {
106
 
    doTestExactScore(ID_FIELD, false);
107
 
  }
108
 
 
109
 
 
110
 
  // Test that queries based on reverse/ordFieldScore returns docs with expected score.
111
 
  private void doTestExactScore(String field, boolean inOrder) throws CorruptIndexException, Exception {
112
 
    IndexSearcher s = new IndexSearcher(dir, true);
113
 
    ValueSource vs;
114
 
    if (inOrder) {
115
 
      vs = new OrdFieldSource(field);
116
 
    } else {
117
 
      vs = new ReverseOrdFieldSource(field);
118
 
    }
119
 
    Query q = new ValueSourceQuery(vs);
120
 
    TopDocs td = s.search(q, null, 1000);
121
 
    assertEquals("All docs should be matched!", N_DOCS, td.totalHits);
122
 
    ScoreDoc sd[] = td.scoreDocs;
123
 
    for (int i = 0; i < sd.length; i++) {
124
 
      float score = sd[i].score;
125
 
      String id = s.getIndexReader().document(sd[i].doc).get(ID_FIELD);
126
 
      log("-------- " + i + ". Explain doc " + id);
127
 
      log(s.explain(q, sd[i].doc));
128
 
      float expectedScore = N_DOCS - i;
129
 
      assertEquals("score of result " + i + " shuould be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA);
130
 
      String expectedId = inOrder
131
 
              ? id2String(N_DOCS - i) // in-order ==> larger  values first
132
 
              : id2String(i + 1);     // reverse  ==> smaller values first
133
 
      assertTrue("id of result " + i + " shuould be " + expectedId + " != " + score, expectedId.equals(id));
134
 
    }
135
 
    s.close();
136
 
  }
137
 
 
138
 
  /**
139
 
   * Test caching OrdFieldSource
140
 
   */
141
 
  @Test
142
 
  public void testCachingOrd() throws CorruptIndexException, Exception {
143
 
    doTestCaching(ID_FIELD, true);
144
 
  }
145
 
 
146
 
  /**
147
 
   * Test caching for ReverseOrdFieldSource
148
 
   */
149
 
  @Test
150
 
  public void testCachingReverseOrd() throws CorruptIndexException, Exception {
151
 
    doTestCaching(ID_FIELD, false);
152
 
  }
153
 
 
154
 
  // Test that values loaded for FieldScoreQuery are cached properly and consumes the proper RAM resources.
155
 
  private void doTestCaching(String field, boolean inOrder) throws CorruptIndexException, Exception {
156
 
    IndexSearcher s = new IndexSearcher(dir, true);
157
 
    Object innerArray = null;
158
 
 
159
 
    boolean warned = false; // print warning once
160
 
 
161
 
    for (int i = 0; i < 10; i++) {
162
 
      ValueSource vs;
163
 
      if (inOrder) {
164
 
        vs = new OrdFieldSource(field);
165
 
      } else {
166
 
        vs = new ReverseOrdFieldSource(field);
167
 
      }
168
 
      ValueSourceQuery q = new ValueSourceQuery(vs);
169
 
      ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
170
 
      try {
171
 
        assertEquals("All docs should be matched!", N_DOCS, h.length);
172
 
        IndexReader[] readers = s.getIndexReader().getSequentialSubReaders();
173
 
 
174
 
        for (IndexReader reader : readers) {
175
 
          if (i == 0) {
176
 
            innerArray = q.valSrc.getValues(reader).getInnerArray();
177
 
          } else {
178
 
            log(i + ".  compare: " + innerArray + " to " + q.valSrc.getValues(reader).getInnerArray());
179
 
            assertSame("field values should be cached and reused!", innerArray, q.valSrc.getValues(reader).getInnerArray());
180
 
          }
181
 
        }
182
 
      } catch (UnsupportedOperationException e) {
183
 
        if (!warned) {
184
 
          System.err.println("WARNING: " + testName() + " cannot fully test values of " + q);
185
 
          warned = true;
186
 
        }
187
 
      }
188
 
    }
189
 
 
190
 
    ValueSource vs;
191
 
    ValueSourceQuery q;
192
 
    ScoreDoc[] h;
193
 
 
194
 
    // verify that different values are loaded for a different field
195
 
    String field2 = INT_FIELD;
196
 
    assertFalse(field.equals(field2)); // otherwise this test is meaningless.
197
 
    if (inOrder) {
198
 
      vs = new OrdFieldSource(field2);
199
 
    } else {
200
 
      vs = new ReverseOrdFieldSource(field2);
201
 
    }
202
 
    q = new ValueSourceQuery(vs);
203
 
    h = s.search(q, null, 1000).scoreDocs;
204
 
    assertEquals("All docs should be matched!", N_DOCS, h.length);
205
 
    IndexReader[] readers = s.getIndexReader().getSequentialSubReaders();
206
 
 
207
 
    for (IndexReader reader : readers) {
208
 
      try {
209
 
        log("compare (should differ): " + innerArray + " to "
210
 
                + q.valSrc.getValues(reader).getInnerArray());
211
 
        assertNotSame(
212
 
                "different values shuold be loaded for a different field!",
213
 
                innerArray, q.valSrc.getValues(reader).getInnerArray());
214
 
      } catch (UnsupportedOperationException e) {
215
 
        if (!warned) {
216
 
          System.err.println("WARNING: " + testName()
217
 
                  + " cannot fully test values of " + q);
218
 
          warned = true;
219
 
        }
220
 
      }
221
 
    }
222
 
    s.close();
223
 
    // verify new values are reloaded (not reused) for a new reader
224
 
    s = new IndexSearcher(dir, true);
225
 
    if (inOrder) {
226
 
      vs = new OrdFieldSource(field);
227
 
    } else {
228
 
      vs = new ReverseOrdFieldSource(field);
229
 
    }
230
 
    q = new ValueSourceQuery(vs);
231
 
    h = s.search(q, null, 1000).scoreDocs;
232
 
    assertEquals("All docs should be matched!", N_DOCS, h.length);
233
 
    readers = s.getIndexReader().getSequentialSubReaders();
234
 
 
235
 
    for (IndexReader reader : readers) {
236
 
      try {
237
 
        log("compare (should differ): " + innerArray + " to "
238
 
                + q.valSrc.getValues(reader).getInnerArray());
239
 
        assertNotSame(
240
 
                "cached field values should not be reused if reader as changed!",
241
 
                innerArray, q.valSrc.getValues(reader).getInnerArray());
242
 
      } catch (UnsupportedOperationException e) {
243
 
        if (!warned) {
244
 
          System.err.println("WARNING: " + testName()
245
 
                  + " cannot fully test values of " + q);
246
 
          warned = true;
247
 
        }
248
 
      }
249
 
    }
250
 
    s.close();
251
 
  }
252
 
 
253
 
  private String testName() {
254
 
    return getClass().getName() + "." + getName();
255
 
  }
256
 
 
257
 
  // LUCENE-1250
258
 
  public void testEqualsNull() throws Exception {
259
 
    OrdFieldSource ofs = new OrdFieldSource("f");
260
 
    assertFalse(ofs.equals(null));
261
 
    
262
 
    ReverseOrdFieldSource rofs = new ReverseOrdFieldSource("f");
263
 
    assertFalse(rofs.equals(null));
264
 
  }
265
 
  
266
 
}