001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021:
022: import java.util.Collection;
023:
024: import org.apache.lucene.index.Term;
025: import org.apache.lucene.index.IndexWriter;
026: import org.apache.lucene.search.IndexSearcher;
027: import org.apache.lucene.store.RAMDirectory;
028: import org.apache.lucene.analysis.SimpleAnalyzer;
029: import org.apache.lucene.document.Document;
030: import org.apache.lucene.document.Field;
031:
032: /** Similarity unit test.
033: *
034: *
035: * @version $Revision: 583534 $
036: */
037: public class TestSimilarity extends LuceneTestCase {
038: public TestSimilarity(String name) {
039: super (name);
040: }
041:
042: public static class SimpleSimilarity extends Similarity {
043: public float lengthNorm(String field, int numTerms) {
044: return 1.0f;
045: }
046:
047: public float queryNorm(float sumOfSquaredWeights) {
048: return 1.0f;
049: }
050:
051: public float tf(float freq) {
052: return freq;
053: }
054:
055: public float sloppyFreq(int distance) {
056: return 2.0f;
057: }
058:
059: public float idf(Collection terms, Searcher searcher) {
060: return 1.0f;
061: }
062:
063: public float idf(int docFreq, int numDocs) {
064: return 1.0f;
065: }
066:
067: public float coord(int overlap, int maxOverlap) {
068: return 1.0f;
069: }
070: }
071:
072: public void testSimilarity() throws Exception {
073: RAMDirectory store = new RAMDirectory();
074: IndexWriter writer = new IndexWriter(store,
075: new SimpleAnalyzer(), true);
076: writer.setSimilarity(new SimpleSimilarity());
077:
078: Document d1 = new Document();
079: d1.add(new Field("field", "a c", Field.Store.YES,
080: Field.Index.TOKENIZED));
081:
082: Document d2 = new Document();
083: d2.add(new Field("field", "a b c", Field.Store.YES,
084: Field.Index.TOKENIZED));
085:
086: writer.addDocument(d1);
087: writer.addDocument(d2);
088: writer.optimize();
089: writer.close();
090:
091: Searcher searcher = new IndexSearcher(store);
092: searcher.setSimilarity(new SimpleSimilarity());
093:
094: Term a = new Term("field", "a");
095: Term b = new Term("field", "b");
096: Term c = new Term("field", "c");
097:
098: searcher.search(new TermQuery(b), new HitCollector() {
099: public final void collect(int doc, float score) {
100: assertTrue(score == 1.0f);
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 HitCollector() {
109: public final void collect(int doc, float score) {
110: //System.out.println("Doc=" + doc + " score=" + score);
111: assertTrue(score == (float) doc + 1);
112: }
113: });
114:
115: PhraseQuery pq = new PhraseQuery();
116: pq.add(a);
117: pq.add(c);
118: //System.out.println(pq.toString("field"));
119: searcher.search(pq, new HitCollector() {
120: public final void collect(int doc, float score) {
121: //System.out.println("Doc=" + doc + " score=" + score);
122: assertTrue(score == 1.0f);
123: }
124: });
125:
126: pq.setSlop(2);
127: //System.out.println(pq.toString("field"));
128: searcher.search(pq, new HitCollector() {
129: public final void collect(int doc, float score) {
130: //System.out.println("Doc=" + doc + " score=" + score);
131: assertTrue(score == 2.0f);
132: }
133: });
134: }
135: }
|