Index: lucene/contrib/misc/src/test/org/apache/lucene/misc/TestShortFieldSimilarity.java =================================================================== --- lucene/contrib/misc/src/test/org/apache/lucene/misc/TestShortFieldSimilarity.java (revision 0) +++ lucene/contrib/misc/src/test/org/apache/lucene/misc/TestShortFieldSimilarity.java (revision 0) @@ -0,0 +1,38 @@ +package org.apache.lucene.misc; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.index.FieldInvertState; +import org.apache.lucene.search.Similarity; +import org.apache.lucene.util.LuceneTestCase; + +public class TestShortFieldSimilarity extends LuceneTestCase { + /** test that we get unique length norm bytes */ + public void test() throws Exception { + Similarity similarity = new ShortFieldSimilarity(); + FieldInvertState state = new FieldInvertState(); + state.setBoost(1.0f); + byte lastValue = 0; + for (int i = 1; i < 10; i++) { + state.setLength(i); + byte value = similarity.encodeNormValue(similarity.computeNorm("field", state)); + assertTrue(value != lastValue); + lastValue = value; + } + } +} Property changes on: lucene\contrib\misc\src\test\org\apache\lucene\misc\TestShortFieldSimilarity.java ___________________________________________________________________ Added: svn:eol-style + native Index: lucene/contrib/misc/src/java/org/apache/lucene/misc/ShortFieldSimilarity.java =================================================================== --- lucene/contrib/misc/src/java/org/apache/lucene/misc/ShortFieldSimilarity.java (revision 0) +++ lucene/contrib/misc/src/java/org/apache/lucene/misc/ShortFieldSimilarity.java (revision 0) @@ -0,0 +1,50 @@ +package org.apache.lucene.misc; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.DefaultSimilarity; +import org.apache.lucene.util.SmallFloat; + +/** + * Similarity with more precise length normalization for shorter fields + * such as metadata. + * @see SmallFloat#floatToByte52(float) + * @see SmallFloat#byte52ToFloat(byte) + * + * @lucene.experimental + */ +public class ShortFieldSimilarity extends DefaultSimilarity { + + /** Cache of decoded bytes. */ + private static final float[] NORM_TABLE = new float[256]; + + static { + for (int i = 0; i < 256; i++) + NORM_TABLE[i] = SmallFloat.byte52ToFloat((byte)i); + } + + @Override + public float decodeNormValue(byte b) { + return NORM_TABLE[b & 0xFF]; // & 0xFF maps negative bytes to positive above 127 + } + + @Override + public byte encodeNormValue(float f) { + return SmallFloat.floatToByte52(f); + } +} Property changes on: lucene\contrib\misc\src\java\org\apache\lucene\misc\ShortFieldSimilarity.java ___________________________________________________________________ Added: svn:eol-style + native