1
package org.apache.lucene.search;
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.util.LuceneTestCase;
21
import java.io.IOException;
22
import java.util.Collection;
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;
34
/** Similarity unit test.
37
* @version $Revision: 1167511 $
39
public class TestSimilarity extends LuceneTestCase {
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() {
51
public float getIdf() {
55
public String explain() {
56
return "Inexplicable";
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()));
68
Document d1 = new Document();
69
d1.add(newField("field", "a c", Field.Store.YES, Field.Index.ANALYZED));
71
Document d2 = new Document();
72
d2.add(newField("field", "a b c", Field.Store.YES, Field.Index.ANALYZED));
74
writer.addDocument(d1);
75
writer.addDocument(d2);
76
IndexReader reader = writer.getReader();
79
IndexSearcher searcher = newSearcher(reader);
80
searcher.setSimilarity(new SimpleSimilarity());
82
Term a = new Term("field", "a");
83
Term b = new Term("field", "b");
84
Term c = new Term("field", "c");
86
searcher.search(new TermQuery(b), new Collector() {
87
private Scorer scorer;
89
public void setScorer(Scorer scorer) throws IOException {
93
public final void collect(int doc) throws IOException {
94
assertEquals(1.0f, scorer.score(), 0);
97
public void setNextReader(IndexReader reader, int docBase) {}
99
public boolean acceptsDocsOutOfOrder() {
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;
112
public void setScorer(Scorer scorer) throws IOException {
113
this.scorer = scorer;
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);
121
public void setNextReader(IndexReader reader, int docBase) {
125
public boolean acceptsDocsOutOfOrder() {
130
PhraseQuery pq = new PhraseQuery();
133
//System.out.println(pq.toString("field"));
136
private Scorer scorer;
138
public void setScorer(Scorer scorer) throws IOException {
139
this.scorer = scorer;
142
public final void collect(int doc) throws IOException {
143
//System.out.println("Doc=" + doc + " score=" + score);
144
assertEquals(1.0f, scorer.score(), 0);
147
public void setNextReader(IndexReader reader, int docBase) {}
149
public boolean acceptsDocsOutOfOrder() {
155
//System.out.println(pq.toString("field"));
156
searcher.search(pq, new Collector() {
157
private Scorer scorer;
159
public void setScorer(Scorer scorer) throws IOException {
160
this.scorer = scorer;
163
public final void collect(int doc) throws IOException {
164
//System.out.println("Doc=" + doc + " score=" + score);
165
assertEquals(2.0f, scorer.score(), 0);
168
public void setNextReader(IndexReader reader, int docBase) {}
170
public boolean acceptsDocsOutOfOrder() {