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.HashSet;
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.analysis.MockTokenizer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.search.DefaultSimilarity;
28 import org.apache.lucene.search.Similarity;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
31 import org.apache.lucene.util._TestUtil;
34 * Tests the uniqueTermCount statistic in FieldInvertState
36 public class TestUniqueTermCount extends LuceneTestCase {
39 /* expected uniqueTermCount values for our documents */
40 ArrayList<Integer> expected = new ArrayList<Integer>();
43 public void setUp() throws Exception {
46 IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT,
47 new MockAnalyzer(random, MockTokenizer.SIMPLE, true)).setMergePolicy(newLogMergePolicy());
48 config.setSimilarity(new TestSimilarity());
49 RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
50 Document doc = new Document();
51 Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);
53 for (int i = 0; i < 100; i++) {
54 foo.setValue(addValue());
55 writer.addDocument(doc);
57 reader = writer.getReader();
62 public void tearDown() throws Exception {
68 public void test() throws Exception {
69 byte fooNorms[] = reader.norms("foo");
70 for (int i = 0; i < reader.maxDoc(); i++)
71 assertEquals(expected.get(i).intValue(), fooNorms[i] & 0xff);
75 * Makes a bunch of single-char tokens (the max # unique terms will at most be 26).
76 * puts the # unique terms into expected, to be checked against the norm.
78 private String addValue() {
79 StringBuilder sb = new StringBuilder();
80 HashSet<String> terms = new HashSet<String>();
81 int num = _TestUtil.nextInt(random, 0, 255);
82 for (int i = 0; i < num; i++) {
84 char term = (char) _TestUtil.nextInt(random, 'a', 'z');
88 expected.add(terms.size());
93 * Simple similarity that encodes maxTermFrequency directly as a byte
95 class TestSimilarity extends DefaultSimilarity {
98 public byte encodeNormValue(float f) {
103 public float computeNorm(String field, FieldInvertState state) {
104 return (float) state.getUniqueTermCount();