add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestTermScorer.java
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 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.index.IndexReader;
28 import org.apache.lucene.index.RandomIndexWriter;
29 import org.apache.lucene.index.Term;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32
33 public class TestTermScorer extends LuceneTestCase {
34   protected Directory directory;
35   private static final String FIELD = "field";
36   
37   protected String[] values = new String[] {"all", "dogs dogs", "like",
38       "playing", "fetch", "all"};
39   protected IndexSearcher indexSearcher;
40   protected IndexReader indexReader;
41   
42   @Override
43   public void setUp() throws Exception {
44     super.setUp();
45     directory = newDirectory();
46     
47     RandomIndexWriter writer = new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
48     for (int i = 0; i < values.length; i++) {
49       Document doc = new Document();
50       doc
51           .add(newField(FIELD, values[i], Field.Store.YES,
52               Field.Index.ANALYZED));
53       writer.addDocument(doc);
54     }
55     writer.optimize();
56     indexReader = writer.getReader();
57     writer.close();
58     indexSearcher = newSearcher(indexReader);
59   }
60   
61   @Override
62   public void tearDown() throws Exception {
63     indexSearcher.close();
64     indexReader.close();
65     directory.close();
66     super.tearDown();
67   }
68
69   public void test() throws IOException {
70     
71     Term allTerm = new Term(FIELD, "all");
72     TermQuery termQuery = new TermQuery(allTerm);
73     
74     Weight weight = indexSearcher.createNormalizedWeight(termQuery);
75     IndexReader sub = indexSearcher.getIndexReader().getSequentialSubReaders() == null ?
76                 indexSearcher.getIndexReader() : indexSearcher.getIndexReader().getSequentialSubReaders()[0];
77     Scorer ts = weight.scorer(sub, true, true);
78     // we have 2 documents with the term all in them, one document for all the
79     // other values
80     final List<TestHit> docs = new ArrayList<TestHit>();
81     // must call next first
82     
83     ts.score(new Collector() {
84       private int base = 0;
85       private Scorer scorer;
86       
87       @Override
88       public void setScorer(Scorer scorer) throws IOException {
89         this.scorer = scorer;
90       }
91       
92       @Override
93       public void collect(int doc) throws IOException {
94         float score = scorer.score();
95         doc = doc + base;
96         docs.add(new TestHit(doc, score));
97         assertTrue("score " + score + " is not greater than 0", score > 0);
98         assertTrue("Doc: " + doc + " does not equal 0 or doc does not equal 5",
99             doc == 0 || doc == 5);
100       }
101       
102       @Override
103       public void setNextReader(IndexReader reader, int docBase) {
104         base = docBase;
105       }
106       
107       @Override
108       public boolean acceptsDocsOutOfOrder() {
109         return true;
110       }
111     });
112     assertTrue("docs Size: " + docs.size() + " is not: " + 2, docs.size() == 2);
113     TestHit doc0 = docs.get(0);
114     TestHit doc5 = docs.get(1);
115     // The scores should be the same
116     assertTrue(doc0.score + " does not equal: " + doc5.score,
117         doc0.score == doc5.score);
118     /*
119      * Score should be (based on Default Sim.: All floats are approximate tf = 1
120      * numDocs = 6 docFreq(all) = 2 idf = ln(6/3) + 1 = 1.693147 idf ^ 2 =
121      * 2.8667 boost = 1 lengthNorm = 1 //there is 1 term in every document coord
122      * = 1 sumOfSquaredWeights = (idf * boost) ^ 2 = 1.693147 ^ 2 = 2.8667
123      * queryNorm = 1 / (sumOfSquaredWeights)^0.5 = 1 /(1.693147) = 0.590
124      * 
125      * score = 1 * 2.8667 * 1 * 1 * 0.590 = 1.69
126      */
127     assertTrue(doc0.score + " does not equal: " + 1.6931472f,
128         doc0.score == 1.6931472f);
129   }
130   
131   public void testNext() throws Exception {
132     
133     Term allTerm = new Term(FIELD, "all");
134     TermQuery termQuery = new TermQuery(allTerm);
135     
136     Weight weight = indexSearcher.createNormalizedWeight(termQuery);
137     
138     IndexReader sub = indexSearcher.getIndexReader().getSequentialSubReaders() == null ?
139         indexSearcher.getIndexReader() : indexSearcher.getIndexReader().getSequentialSubReaders()[0];
140     Scorer ts = weight.scorer(sub, true, true);
141     assertTrue("next did not return a doc",
142         ts.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
143     assertTrue("score is not correct", ts.score() == 1.6931472f);
144     assertTrue("next did not return a doc",
145         ts.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
146     assertTrue("score is not correct", ts.score() == 1.6931472f);
147     assertTrue("next returned a doc and it should not have",
148         ts.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);
149   }
150   
151   public void testAdvance() throws Exception {
152     
153     Term allTerm = new Term(FIELD, "all");
154     TermQuery termQuery = new TermQuery(allTerm);
155     
156     Weight weight = indexSearcher.createNormalizedWeight(termQuery);
157     
158     IndexReader sub = indexSearcher.getIndexReader().getSequentialSubReaders() == null ? 
159         indexSearcher.getIndexReader() : indexSearcher.getIndexReader().getSequentialSubReaders()[0];
160         
161     Scorer ts = weight.scorer(sub, true, true);
162     assertTrue("Didn't skip", ts.advance(3) != DocIdSetIterator.NO_MORE_DOCS);
163     // The next doc should be doc 5
164     assertTrue("doc should be number 5", ts.docID() == 5);
165   }
166   
167   private class TestHit {
168     public int doc;
169     public float score;
170     
171     public TestHit(int doc, float score) {
172       this.doc = doc;
173       this.score = score;
174     }
175     
176     @Override
177     public String toString() {
178       return "TestHit{" + "doc=" + doc + ", score=" + score + "}";
179     }
180   }
181   
182 }