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 java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
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;
33 public class TestTermScorer extends LuceneTestCase {
34 protected Directory directory;
35 private static final String FIELD = "field";
37 protected String[] values = new String[] {"all", "dogs dogs", "like",
38 "playing", "fetch", "all"};
39 protected IndexSearcher indexSearcher;
40 protected IndexReader indexReader;
43 public void setUp() throws Exception {
45 directory = newDirectory();
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();
51 .add(newField(FIELD, values[i], Field.Store.YES,
52 Field.Index.ANALYZED));
53 writer.addDocument(doc);
56 indexReader = writer.getReader();
58 indexSearcher = newSearcher(indexReader);
62 public void tearDown() throws Exception {
63 indexSearcher.close();
69 public void test() throws IOException {
71 Term allTerm = new Term(FIELD, "all");
72 TermQuery termQuery = new TermQuery(allTerm);
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
80 final List<TestHit> docs = new ArrayList<TestHit>();
81 // must call next first
83 ts.score(new Collector() {
85 private Scorer scorer;
88 public void setScorer(Scorer scorer) throws IOException {
93 public void collect(int doc) throws IOException {
94 float score = scorer.score();
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);
103 public void setNextReader(IndexReader reader, int docBase) {
108 public boolean acceptsDocsOutOfOrder() {
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);
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
125 * score = 1 * 2.8667 * 1 * 1 * 0.590 = 1.69
127 assertTrue(doc0.score + " does not equal: " + 1.6931472f,
128 doc0.score == 1.6931472f);
131 public void testNext() throws Exception {
133 Term allTerm = new Term(FIELD, "all");
134 TermQuery termQuery = new TermQuery(allTerm);
136 Weight weight = indexSearcher.createNormalizedWeight(termQuery);
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);
151 public void testAdvance() throws Exception {
153 Term allTerm = new Term(FIELD, "all");
154 TermQuery termQuery = new TermQuery(allTerm);
156 Weight weight = indexSearcher.createNormalizedWeight(termQuery);
158 IndexReader sub = indexSearcher.getIndexReader().getSequentialSubReaders() == null ?
159 indexSearcher.getIndexReader() : indexSearcher.getIndexReader().getSequentialSubReaders()[0];
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);
167 private class TestHit {
171 public TestHit(int doc, float score) {
177 public String toString() {
178 return "TestHit{" + "doc=" + doc + ", score=" + score + "}";