1 package org.apache.lucene.index;
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.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.analysis.MockTokenizer;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.search.DefaultSimilarity;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32 import org.apache.lucene.util._TestUtil;
35 * Tests the maxTermFrequency statistic in FieldInvertState
37 public class TestMaxTermFrequency extends LuceneTestCase {
40 /* expected maxTermFrequency values for our documents */
41 ArrayList<Integer> expected = new ArrayList<Integer>();
44 public void setUp() throws Exception {
47 IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT,
48 new MockAnalyzer(random, MockTokenizer.SIMPLE, true)).setMergePolicy(newLogMergePolicy());
49 config.setSimilarity(new TestSimilarity());
50 RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
51 Document doc = new Document();
52 Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);
54 for (int i = 0; i < 100; i++) {
55 foo.setValue(addValue());
56 writer.addDocument(doc);
58 reader = writer.getReader();
63 public void tearDown() throws Exception {
69 public void test() throws Exception {
70 byte fooNorms[] = reader.norms("foo");
71 for (int i = 0; i < reader.maxDoc(); i++)
72 assertEquals(expected.get(i).intValue(), fooNorms[i] & 0xff);
76 * Makes a bunch of single-char tokens (the max freq will at most be 255).
77 * shuffles them around, and returns the whole list with Arrays.toString().
78 * This works fine because we use lettertokenizer.
79 * puts the max-frequency term into expected, to be checked against the norm.
81 private String addValue() {
82 List<String> terms = new ArrayList<String>();
83 int maxCeiling = _TestUtil.nextInt(random, 0, 255);
85 for (char ch = 'a'; ch <= 'z'; ch++) {
86 int num = _TestUtil.nextInt(random, 0, maxCeiling);
87 for (int i = 0; i < num; i++)
88 terms.add(Character.toString(ch));
89 max = Math.max(max, num);
92 Collections.shuffle(terms, random);
93 return Arrays.toString(terms.toArray(new String[terms.size()]));
97 * Simple similarity that encodes maxTermFrequency directly as a byte
99 class TestSimilarity extends DefaultSimilarity {
102 public byte encodeNormValue(float f) {
107 public float decodeNormValue(byte b) {
112 public float computeNorm(String field, FieldInvertState state) {
113 return (float) state.getMaxTermFrequency();