1 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 ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 import java.util.TreeMap ; 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 63 public class CmsSearchCategoryCollector extends HitCollector { 64 65 69 private class CmsCategroyCount { 70 71 72 int m_count; 73 74 77 CmsCategroyCount() { 78 79 m_count = 1; 80 } 81 82 87 int getCount() { 88 89 return m_count; 90 } 91 92 95 void inc() { 96 97 m_count++; 98 } 99 100 105 Integer toInteger() { 106 107 return new Integer (m_count); 108 } 109 } 110 111 112 public static final String UNKNOWN_CATEGORY = "unknown"; 113 114 115 private static final Log LOG = CmsLog.getLog(CmsSearchCategoryCollector.class); 116 117 118 private Map m_categories; 119 120 121 private IndexSearcher m_searcher; 122 123 128 public CmsSearchCategoryCollector(IndexSearcher searcher) { 129 130 super(); 131 m_searcher = searcher; 132 m_categories = new HashMap (); 133 } 134 135 142 public static final String formatCategoryMap(Map categories) { 143 144 StringBuffer result = new StringBuffer (256); 145 result.append("Total categories: "); 146 result.append(categories.size()); 147 result.append('\n'); 148 Iterator i = categories.keySet().iterator(); 149 while (i.hasNext()) { 150 String category = (String )i.next(); 151 Integer count = (Integer )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 162 public void collect(int id, float score) { 163 164 String category = null; 165 try { 166 Document doc = m_searcher.doc(id); 167 category = doc.get(I_CmsDocumentFactory.DOC_CATEGORY); 168 } catch (IOException e) { 169 if (LOG.isDebugEnabled()) { 171 LOG.debug(Messages.get().getBundle().key(Messages.LOG_READ_CATEGORY_FAILED_1, new Integer (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 193 public Map getCategoryCountResult() { 194 195 Map result = new TreeMap (); 196 Iterator i = m_categories.keySet().iterator(); 197 while (i.hasNext()) { 198 String category = (String )i.next(); 199 CmsCategroyCount count = (CmsCategroyCount)m_categories.get(category); 200 result.put(category, count.toInteger()); 201 } 202 return result; 203 } 204 205 208 public String toString() { 209 210 return formatCategoryMap(getCategoryCountResult()); 211 } 212 } | Popular Tags |