KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsSearchCategoryCollector.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.7 $
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.main.CmsLog;
35 import org.opencms.search.documents.I_CmsDocumentFactory;
36 import org.opencms.util.CmsStringUtil;
37
38 import java.io.IOException JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.TreeMap JavaDoc;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.lucene.document.Document;
46 import org.apache.lucene.search.HitCollector;
47 import org.apache.lucene.search.IndexSearcher;
48
49 /**
50  * Collects category information during a search process.<p>
51  *
52  * <b>Please note:</b> The calculation of the category count slows down the search time by an order
53  * of magnitude. Make sure that you only use this feature if it's really required!
54  * Be especially careful if your search result list can become large (> 1000 documents), since in this case
55  * overall system performance will certainly be impacted considerably when calculating the categories.<p>
56  *
57  * @author Alexander Kandzior
58  *
59  * @version $Revision: 1.7 $
60  *
61  * @since 6.0.0
62  */

63 public class CmsSearchCategoryCollector extends HitCollector {
64
65     /**
66      * Class with an increasable counter to avoid multiple look ups and
67      * object creations when dealing with the category count.<p>
68      */

69     private class CmsCategroyCount {
70
71         /** The category count. */
72         int m_count;
73
74         /**
75          * Creates a new instance with a initial count of 1.<p>
76          */

77         CmsCategroyCount() {
78
79             m_count = 1;
80         }
81
82         /**
83          * Returns the count.<p>
84          *
85          * @return the count
86          */

87         int getCount() {
88
89             return m_count;
90         }
91
92         /**
93          * Increases the count by one.<p>
94          */

95         void inc() {
96
97             m_count++;
98         }
99
100         /**
101          * Creates an Integer for this count.<p>
102          *
103          * @return an Integer for this count
104          */

105         Integer JavaDoc toInteger() {
106
107             return new Integer JavaDoc(m_count);
108         }
109     }
110
111     /** Category used in case the document belongs to no category. */
112     public static final String JavaDoc UNKNOWN_CATEGORY = "unknown";
113
114     /** The log object for this class. */
115     private static final Log LOG = CmsLog.getLog(CmsSearchCategoryCollector.class);
116
117     /** The internal map of the categories found. */
118     private Map JavaDoc m_categories;
119
120     /** The indes searcher used. */
121     private IndexSearcher m_searcher;
122
123     /**
124      * Creates a new category search collector instance.<p>
125      *
126      * @param searcher the index searcher used
127      */

128     public CmsSearchCategoryCollector(IndexSearcher searcher) {
129
130         super();
131         m_searcher = searcher;
132         m_categories = new HashMap JavaDoc();
133     }
134
135     /**
136      * Convenience method to format a map of categories in a nice 2 column list, for example
137      * for display of debugging output.<p>
138      *
139      * @param categories the map to format
140      * @return the formatted category map
141      */

142     public static final String JavaDoc formatCategoryMap(Map JavaDoc categories) {
143
144         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
145         result.append("Total categories: ");
146         result.append(categories.size());
147         result.append('\n');
148         Iterator JavaDoc i = categories.keySet().iterator();
149         while (i.hasNext()) {
150             String JavaDoc category = (String JavaDoc)i.next();
151             Integer JavaDoc count = (Integer JavaDoc)categories.get(category);
152             result.append(CmsStringUtil.padRight(category, 30));
153             result.append(count.intValue());
154             result.append('\n');
155         }
156         return result.toString();
157     }
158
159     /**
160      * @see org.apache.lucene.search.HitCollector#collect(int, float)
161      */

162     public void collect(int id, float score) {
163
164         String JavaDoc category = null;
165         try {
166             Document doc = m_searcher.doc(id);
167             category = doc.get(I_CmsDocumentFactory.DOC_CATEGORY);
168         } catch (IOException JavaDoc e) {
169             // category will be null
170
if (LOG.isDebugEnabled()) {
171                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_READ_CATEGORY_FAILED_1, new Integer JavaDoc(id)), e);
172             }
173
174         }
175         if (category == null) {
176             category = UNKNOWN_CATEGORY;
177         }
178         CmsCategroyCount count = (CmsCategroyCount)m_categories.get(category);
179         if (count != null) {
180             count.inc();
181         } else {
182             count = new CmsCategroyCount();
183             m_categories.put(category, count);
184         }
185     }
186
187     /**
188      * Returns the category count result, the returned map
189      * contains Strings (category names) mapped to an Integer (the count).<p>
190      *
191      * @return the category count result
192      */

193     public Map JavaDoc getCategoryCountResult() {
194
195         Map JavaDoc result = new TreeMap JavaDoc();
196         Iterator JavaDoc i = m_categories.keySet().iterator();
197         while (i.hasNext()) {
198             String JavaDoc category = (String JavaDoc)i.next();
199             CmsCategroyCount count = (CmsCategroyCount)m_categories.get(category);
200             result.put(category, count.toInteger());
201         }
202         return result;
203     }
204
205     /**
206      * @see java.lang.Object#toString()
207      */

208     public String JavaDoc toString() {
209
210         return formatCategoryMap(getCategoryCountResult());
211     }
212 }
Popular Tags