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

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestSetNorm.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;
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 java.io.IOException;
21
 
 
22
 
import org.apache.lucene.util.LuceneTestCase;
23
 
import org.apache.lucene.analysis.MockAnalyzer;
24
 
import org.apache.lucene.document.*;
25
 
import org.apache.lucene.index.IndexReader;
26
 
import org.apache.lucene.index.IndexWriter;
27
 
import org.apache.lucene.index.Term;
28
 
import org.apache.lucene.store.Directory;
29
 
 
30
 
/** Document boost unit test.
31
 
 *
32
 
 *
33
 
 * @version $Revision: 1202680 $
34
 
 */
35
 
public class TestSetNorm extends LuceneTestCase {
36
 
 
37
 
  public void testSetNorm() throws Exception {
38
 
    Directory store = newDirectory();
39
 
    IndexWriter writer = new IndexWriter(store, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
40
 
 
41
 
    // add the same document four times
42
 
    Fieldable f1 = newField("field", "word", Field.Store.YES, Field.Index.ANALYZED);
43
 
    Document d1 = new Document();
44
 
    d1.add(f1);
45
 
    writer.addDocument(d1);
46
 
    writer.addDocument(d1);
47
 
    writer.addDocument(d1);
48
 
    writer.addDocument(d1);
49
 
    writer.close();
50
 
 
51
 
    // reset the boost of each instance of this document
52
 
    IndexReader reader = IndexReader.open(store, false);
53
 
    reader.setNorm(0, "field", 1.0f);
54
 
    reader.setNorm(1, "field", 2.0f);
55
 
    reader.setNorm(2, "field", 4.0f);
56
 
    reader.setNorm(3, "field", 16.0f);
57
 
    reader.close();
58
 
 
59
 
    // check that searches are ordered by this boost
60
 
    final float[] scores = new float[4];
61
 
 
62
 
    IndexReader ir = IndexReader.open(store);
63
 
    IndexSearcher is = new IndexSearcher(ir);
64
 
    is.search
65
 
      (new TermQuery(new Term("field", "word")),
66
 
       new Collector() {
67
 
         private int base = 0;
68
 
         private Scorer scorer;
69
 
         @Override
70
 
         public void setScorer(Scorer scorer) throws IOException {
71
 
          this.scorer = scorer;
72
 
         }
73
 
         @Override
74
 
         public final void collect(int doc) throws IOException {
75
 
           scores[doc + base] = scorer.score();
76
 
         }
77
 
         @Override
78
 
         public void setNextReader(IndexReader reader, int docBase) {
79
 
           base = docBase;
80
 
         }
81
 
         @Override
82
 
         public boolean acceptsDocsOutOfOrder() {
83
 
           return true;
84
 
         }
85
 
       });
86
 
    is.close();
87
 
    ir.close();
88
 
    float lastScore = 0.0f;
89
 
 
90
 
    for (int i = 0; i < 4; i++) {
91
 
      assertTrue(scores[i] > lastScore);
92
 
      lastScore = scores[i];
93
 
    }
94
 
    store.close();
95
 
  }
96
 
}