~ubuntu-branches/ubuntu/trusty/pylucene/trusty

« back to all changes in this revision

Viewing changes to lucene-java-2.3.1/contrib/miscellaneous/src/test/org/apache/lucene/misc/TestLengthNormModifier.java

  • Committer: Package Import Robot
  • Author(s): Dmitry Nezhevenko
  • Date: 2012-04-23 16:43:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120423164355-grqtepnwtecdjfk2
Tags: 3.5.0-1
* New maintainer (closes: 670179)
* New upstream release
* Switch to dpkg-source 3.0 (quilt) format
* Switch to machine-readable debian/copyright
* Bump debian/compat to 8, drop debian/pycompat
* Switch from cdbs to dh
* Add watch file
* Build for all supported versions of python2 (closes: 581198, 632240)
* Rename binary package to python-lucene (closes: 581197)
* Add -dbg package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.misc;
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 junit.framework.TestCase;
21
 
 
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;
35
 
 
36
 
/**
37
 
 * Tests changing the norms after changing the simularity
38
 
 *
39
 
 * @version $Id:$
40
 
 */
41
 
public class TestLengthNormModifier extends TestCase {
42
 
    public TestLengthNormModifier(String name) {
43
 
        super(name);
44
 
    }
45
 
 
46
 
    public static byte DEFAULT_NORM = Similarity.encodeNorm(1.0f);
47
 
    
48
 
    public static int NUM_DOCS = 5;
49
 
 
50
 
    public Directory store = new RAMDirectory();
51
 
 
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;
56
 
            }
57
 
        };
58
 
    
59
 
    public void setUp() throws Exception {
60
 
        IndexWriter writer = new
61
 
            IndexWriter(store, new SimpleAnalyzer(), true);
62
 
        
63
 
        IndexSearcher searcher;
64
 
        
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));
71
 
                
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));
77
 
            }
78
 
            writer.addDocument(d);
79
 
        }
80
 
        writer.close();
81
 
    }
82
 
    
83
 
    public void testMissingField() {
84
 
        LengthNormModifier lnm = new LengthNormModifier(store, s);
85
 
        try {
86
 
            lnm.reSetNorms("nobodyherebutuschickens");
87
 
        } catch (Exception e) {
88
 
            assertNull("caught something", e);
89
 
        }
90
 
    }
91
 
        
92
 
    public void testFieldWithNoNorm() throws Exception {
93
 
 
94
 
        IndexReader r = IndexReader.open(store);
95
 
        byte[] norms = r.norms("nonorm");
96
 
 
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]);
101
 
        }
102
 
 
103
 
        r.close();
104
 
        
105
 
        LengthNormModifier lnm = new LengthNormModifier(store, s);
106
 
        try {
107
 
            lnm.reSetNorms("nonorm");
108
 
        } catch (Exception e) {
109
 
            assertNull("caught something", e);
110
 
        }
111
 
 
112
 
        // nothing should have changed
113
 
        r = IndexReader.open(store);
114
 
        
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]);
119
 
        }
120
 
 
121
 
        r.close();
122
 
        
123
 
    }
124
 
        
125
 
    
126
 
    public void testGoodCases() throws Exception {
127
 
        
128
 
        IndexSearcher searcher;
129
 
        final float[] scores = new float[NUM_DOCS];
130
 
        float lastScore = 0.0f;
131
 
        
132
 
        // default similarity should put docs with shorter length first
133
 
        searcher = new IndexSearcher(store);
134
 
        searcher.search
135
 
            (new TermQuery(new Term("field", "word")),
136
 
             new HitCollector() {
137
 
                 public final void collect(int doc, float score) {
138
 
                     scores[doc] = score;
139
 
                 }
140
 
             });
141
 
        searcher.close();
142
 
        
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];
149
 
        }
150
 
 
151
 
        // override the norms to be inverted
152
 
        Similarity s = new DefaultSimilarity() {
153
 
                public float lengthNorm(String fieldName, int numTokens) {
154
 
                    return (float)numTokens;
155
 
                }
156
 
            };
157
 
        LengthNormModifier lnm = new LengthNormModifier(store, s);
158
 
        lnm.reSetNorms("field");
159
 
 
160
 
        // new norm (with default similarity) should put longer docs first
161
 
        searcher = new IndexSearcher(store);
162
 
        searcher.search
163
 
            (new TermQuery(new Term("field", "word")),
164
 
             new HitCollector() {
165
 
                 public final void collect(int doc, float score) {
166
 
                     scores[doc] = score;
167
 
                 }
168
 
             });
169
 
        searcher.close();
170
 
        
171
 
        lastScore = 0.0f;
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];
177
 
        }
178
 
        
179
 
    }
180
 
}