KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > search > CmsSearchSimilarity


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsSearchSimilarity.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.6 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.search;
33
34 import org.opencms.search.documents.I_CmsDocumentFactory;
35
36 import org.apache.lucene.search.DefaultSimilarity;
37
38 /**
39  * Reduces the importance of the <code>{@link #lengthNorm(String, int)}</code> factor
40  * for the <code>{@link I_CmsDocumentFactory#DOC_CONTENT}</code> field, while
41  * keeping the Lucene default for all other fields.<p>
42  *
43  * This implementation was added since apparently the default length norm is heavily biased
44  * for small documents. In the default, even if a term is found in 2 documents the same number of
45  * times, the smaller document (containing less terms) will have a score easily 3x as high as
46  * the longer document. Using this implementation the importance of the term number is reduced.<p>
47  *
48  * Inspired by Chuck Williams WikipediaSimilarity.<p>
49  *
50  * @author Alexander Kandzior
51  *
52  * @version $Revision: 1.6 $
53  *
54  * @since 6.0.0
55  */

56 public class CmsSearchSimilarity extends DefaultSimilarity {
57
58     /** Logarithm base 10 used as factor in the score calculations. */
59     private static final double LOG10 = Math.log(10.0);
60
61     /** Serial version UID required for safe serialization. */
62     private static final long serialVersionUID = 3598754228215079733L;
63
64     /**
65      * Creates a new instance of the OpenCms search similarity.<p>
66      */

67     public CmsSearchSimilarity() {
68
69         super();
70     }
71
72     /**
73      * Special implementation for "length norm" to reduce the significance of this factor
74      * for the <code>{@link I_CmsDocumentFactory#DOC_CONTENT}</code> field, while
75      * keeping the Lucene default for all other fields.<p>
76      *
77      * @see org.apache.lucene.search.Similarity#lengthNorm(java.lang.String, int)
78      */

79     public float lengthNorm(String JavaDoc fieldName, int numTerms) {
80
81         if (fieldName.equals(I_CmsDocumentFactory.DOC_CONTENT)) {
82             // special length norm for content
83
return (float)(3.0 / (Math.log(1000 + numTerms) / LOG10));
84         }
85         // all other fields use the default Lucene implementation
86
return (float)(1.0 / Math.sqrt(numTerms));
87     }
88 }
Popular Tags