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

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestSimilarity.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 org.apache.lucene.util.LuceneTestCase;
21
 
import java.io.IOException;
22
 
import java.util.Collection;
23
 
 
24
 
import org.apache.lucene.index.FieldInvertState;
25
 
import org.apache.lucene.index.IndexReader;
26
 
import org.apache.lucene.index.RandomIndexWriter;
27
 
import org.apache.lucene.index.Term;
28
 
import org.apache.lucene.store.Directory;
29
 
import org.apache.lucene.analysis.MockAnalyzer;
30
 
import org.apache.lucene.document.Document;
31
 
import org.apache.lucene.document.Field;
32
 
import org.apache.lucene.search.Explanation.IDFExplanation;
33
 
 
34
 
/** Similarity unit test.
35
 
 *
36
 
 *
37
 
 * @version $Revision: 1167511 $
38
 
 */
39
 
public class TestSimilarity extends LuceneTestCase {
40
 
  
41
 
  public static class SimpleSimilarity extends Similarity {
42
 
    @Override public float computeNorm(String field, FieldInvertState state) { return state.getBoost(); }
43
 
    @Override public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
44
 
    @Override public float tf(float freq) { return freq; }
45
 
    @Override public float sloppyFreq(int distance) { return 2.0f; }
46
 
    @Override public float idf(int docFreq, int numDocs) { return 1.0f; }
47
 
    @Override public float coord(int overlap, int maxOverlap) { return 1.0f; }
48
 
    @Override public IDFExplanation idfExplain(Collection<Term> terms, Searcher searcher) throws IOException {
49
 
      return new IDFExplanation() {
50
 
        @Override
51
 
        public float getIdf() {
52
 
          return 1.0f;
53
 
        }
54
 
        @Override
55
 
        public String explain() {
56
 
          return "Inexplicable";
57
 
        }
58
 
      };
59
 
    }
60
 
  }
61
 
 
62
 
  public void testSimilarity() throws Exception {
63
 
    Directory store = newDirectory();
64
 
    RandomIndexWriter writer = new RandomIndexWriter(random, store, 
65
 
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
66
 
        .setSimilarity(new SimpleSimilarity()));
67
 
    
68
 
    Document d1 = new Document();
69
 
    d1.add(newField("field", "a c", Field.Store.YES, Field.Index.ANALYZED));
70
 
 
71
 
    Document d2 = new Document();
72
 
    d2.add(newField("field", "a b c", Field.Store.YES, Field.Index.ANALYZED));
73
 
    
74
 
    writer.addDocument(d1);
75
 
    writer.addDocument(d2);
76
 
    IndexReader reader = writer.getReader();
77
 
    writer.close();
78
 
 
79
 
    IndexSearcher searcher = newSearcher(reader);
80
 
    searcher.setSimilarity(new SimpleSimilarity());
81
 
 
82
 
    Term a = new Term("field", "a");
83
 
    Term b = new Term("field", "b");
84
 
    Term c = new Term("field", "c");
85
 
 
86
 
    searcher.search(new TermQuery(b), new Collector() {
87
 
         private Scorer scorer;
88
 
         @Override
89
 
        public void setScorer(Scorer scorer) throws IOException {
90
 
           this.scorer = scorer; 
91
 
         }
92
 
         @Override
93
 
        public final void collect(int doc) throws IOException {
94
 
           assertEquals(1.0f, scorer.score(), 0);
95
 
         }
96
 
         @Override
97
 
        public void setNextReader(IndexReader reader, int docBase) {}
98
 
         @Override
99
 
        public boolean acceptsDocsOutOfOrder() {
100
 
           return true;
101
 
         }
102
 
       });
103
 
 
104
 
    BooleanQuery bq = new BooleanQuery();
105
 
    bq.add(new TermQuery(a), BooleanClause.Occur.SHOULD);
106
 
    bq.add(new TermQuery(b), BooleanClause.Occur.SHOULD);
107
 
    //System.out.println(bq.toString("field"));
108
 
    searcher.search(bq, new Collector() {
109
 
         private int base = 0;
110
 
         private Scorer scorer;
111
 
         @Override
112
 
        public void setScorer(Scorer scorer) throws IOException {
113
 
           this.scorer = scorer; 
114
 
         }
115
 
         @Override
116
 
        public final void collect(int doc) throws IOException {
117
 
           //System.out.println("Doc=" + doc + " score=" + score);
118
 
           assertEquals((float)doc+base+1, scorer.score(), 0);
119
 
         }
120
 
         @Override
121
 
        public void setNextReader(IndexReader reader, int docBase) {
122
 
           base = docBase;
123
 
         }
124
 
         @Override
125
 
        public boolean acceptsDocsOutOfOrder() {
126
 
           return true;
127
 
         }
128
 
       });
129
 
 
130
 
    PhraseQuery pq = new PhraseQuery();
131
 
    pq.add(a);
132
 
    pq.add(c);
133
 
    //System.out.println(pq.toString("field"));
134
 
    searcher.search(pq,
135
 
       new Collector() {
136
 
         private Scorer scorer;
137
 
         @Override
138
 
         public void setScorer(Scorer scorer) throws IOException {
139
 
          this.scorer = scorer; 
140
 
         }
141
 
         @Override
142
 
         public final void collect(int doc) throws IOException {
143
 
           //System.out.println("Doc=" + doc + " score=" + score);
144
 
           assertEquals(1.0f, scorer.score(), 0);
145
 
         }
146
 
         @Override
147
 
         public void setNextReader(IndexReader reader, int docBase) {}
148
 
         @Override
149
 
         public boolean acceptsDocsOutOfOrder() {
150
 
           return true;
151
 
         }
152
 
       });
153
 
 
154
 
    pq.setSlop(2);
155
 
    //System.out.println(pq.toString("field"));
156
 
    searcher.search(pq, new Collector() {
157
 
      private Scorer scorer;
158
 
      @Override
159
 
      public void setScorer(Scorer scorer) throws IOException {
160
 
        this.scorer = scorer; 
161
 
      }
162
 
      @Override
163
 
      public final void collect(int doc) throws IOException {
164
 
        //System.out.println("Doc=" + doc + " score=" + score);
165
 
        assertEquals(2.0f, scorer.score(), 0);
166
 
      }
167
 
      @Override
168
 
      public void setNextReader(IndexReader reader, int docBase) {}
169
 
      @Override
170
 
      public boolean acceptsDocsOutOfOrder() {
171
 
        return true;
172
 
      }
173
 
    });
174
 
 
175
 
    searcher.close();
176
 
    reader.close();
177
 
    store.close();
178
 
  }
179
 
}