1
package org.apache.lucene.misc;
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 junit.framework.TestCase;
22
import org.apache.lucene.index.Term;
23
import org.apache.lucene.index.IndexWriter;
24
import org.apache.lucene.index.IndexReader;
25
import org.apache.lucene.search.IndexSearcher;
26
import org.apache.lucene.search.Similarity;
27
import org.apache.lucene.search.DefaultSimilarity;
28
import org.apache.lucene.search.TermQuery;
29
import org.apache.lucene.search.HitCollector;
30
import org.apache.lucene.store.RAMDirectory;
31
import org.apache.lucene.store.Directory;
32
import org.apache.lucene.analysis.SimpleAnalyzer;
33
import org.apache.lucene.document.Document;
34
import org.apache.lucene.document.Field;
37
* Tests changing the norms after changing the simularity
41
public class TestLengthNormModifier extends TestCase {
42
public TestLengthNormModifier(String name) {
46
public static byte DEFAULT_NORM = Similarity.encodeNorm(1.0f);
48
public static int NUM_DOCS = 5;
50
public Directory store = new RAMDirectory();
52
/** inverts the normal notion of lengthNorm */
53
public static Similarity s = new DefaultSimilarity() {
54
public float lengthNorm(String fieldName, int numTokens) {
55
return (float)numTokens;
59
public void setUp() throws Exception {
60
IndexWriter writer = new
61
IndexWriter(store, new SimpleAnalyzer(), true);
63
IndexSearcher searcher;
65
for (int i = 0; i < NUM_DOCS; i++) {
66
Document d = new Document();
67
d.add(new Field("field", "word",
68
Field.Store.YES, Field.Index.TOKENIZED));
69
d.add(new Field("nonorm", "word",
70
Field.Store.YES, Field.Index.NO_NORMS));
72
for (int j = 1; j <= i; j++) {
73
d.add(new Field("field", "crap",
74
Field.Store.YES, Field.Index.TOKENIZED));
75
d.add(new Field("nonorm", "more words",
76
Field.Store.YES, Field.Index.NO_NORMS));
78
writer.addDocument(d);
83
public void testMissingField() {
84
LengthNormModifier lnm = new LengthNormModifier(store, s);
86
lnm.reSetNorms("nobodyherebutuschickens");
87
} catch (Exception e) {
88
assertNull("caught something", e);
92
public void testFieldWithNoNorm() throws Exception {
94
IndexReader r = IndexReader.open(store);
95
byte[] norms = r.norms("nonorm");
97
// sanity check, norms should all be 1
98
assertTrue("Whoops we have norms?", !r.hasNorms("nonorm"));
99
for (int i = 0; i< norms.length; i++) {
100
assertEquals(""+i, DEFAULT_NORM, norms[i]);
105
LengthNormModifier lnm = new LengthNormModifier(store, s);
107
lnm.reSetNorms("nonorm");
108
} catch (Exception e) {
109
assertNull("caught something", e);
112
// nothing should have changed
113
r = IndexReader.open(store);
115
norms = r.norms("nonorm");
116
assertTrue("Whoops we have norms?", !r.hasNorms("nonorm"));
117
for (int i = 0; i< norms.length; i++) {
118
assertEquals(""+i, DEFAULT_NORM, norms[i]);
126
public void testGoodCases() throws Exception {
128
IndexSearcher searcher;
129
final float[] scores = new float[NUM_DOCS];
130
float lastScore = 0.0f;
132
// default similarity should put docs with shorter length first
133
searcher = new IndexSearcher(store);
135
(new TermQuery(new Term("field", "word")),
137
public final void collect(int doc, float score) {
143
lastScore = Float.MAX_VALUE;
144
for (int i = 0; i < NUM_DOCS; i++) {
145
String msg = "i=" + i + ", "+scores[i]+" <= "+lastScore;
146
assertTrue(msg, scores[i] <= lastScore);
147
//System.out.println(msg);
148
lastScore = scores[i];
151
// override the norms to be inverted
152
Similarity s = new DefaultSimilarity() {
153
public float lengthNorm(String fieldName, int numTokens) {
154
return (float)numTokens;
157
LengthNormModifier lnm = new LengthNormModifier(store, s);
158
lnm.reSetNorms("field");
160
// new norm (with default similarity) should put longer docs first
161
searcher = new IndexSearcher(store);
163
(new TermQuery(new Term("field", "word")),
165
public final void collect(int doc, float score) {
172
for (int i = 0; i < NUM_DOCS; i++) {
173
String msg = "i=" + i + ", "+scores[i]+" >= "+lastScore;
174
assertTrue(msg, scores[i] >= lastScore);
175
//System.out.println(msg);
176
lastScore = scores[i];