1 package org.apache.lucene.search;
3 import org.apache.lucene.index.FieldInvertState;
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 /** Expert: Default scoring implementation. */
23 public class DefaultSimilarity extends Similarity {
26 * <code>state.getBoost()*lengthNorm(numTerms)</code>, where
27 * <code>numTerms</code> is {@link FieldInvertState#getLength()} if {@link
28 * #setDiscountOverlaps} is false, else it's {@link
29 * FieldInvertState#getLength()} - {@link
30 * FieldInvertState#getNumOverlap()}.
32 * @lucene.experimental */
34 public float computeNorm(String field, FieldInvertState state) {
37 numTerms = state.getLength() - state.getNumOverlap();
39 numTerms = state.getLength();
40 return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms)));
43 /** Implemented as <code>1/sqrt(sumOfSquaredWeights)</code>. */
45 public float queryNorm(float sumOfSquaredWeights) {
46 return (float)(1.0 / Math.sqrt(sumOfSquaredWeights));
49 /** Implemented as <code>sqrt(freq)</code>. */
51 public float tf(float freq) {
52 return (float)Math.sqrt(freq);
55 /** Implemented as <code>1 / (distance + 1)</code>. */
57 public float sloppyFreq(int distance) {
58 return 1.0f / (distance + 1);
61 /** Implemented as <code>log(numDocs/(docFreq+1)) + 1</code>. */
63 public float idf(int docFreq, int numDocs) {
64 return (float)(Math.log(numDocs/(double)(docFreq+1)) + 1.0);
67 /** Implemented as <code>overlap / maxOverlap</code>. */
69 public float coord(int overlap, int maxOverlap) {
70 return overlap / (float)maxOverlap;
74 protected boolean discountOverlaps = true;
76 /** Determines whether overlap tokens (Tokens with
77 * 0 position increment) are ignored when computing
78 * norm. By default this is true, meaning overlap
79 * tokens do not count when computing norms.
81 * @lucene.experimental
85 public void setDiscountOverlaps(boolean v) {
89 /** @see #setDiscountOverlaps */
90 public boolean getDiscountOverlaps() {
91 return discountOverlaps;