org.apache.avalon.framework.context.ContextException org.apache.cocoon.components.search.* org.apache.lucene.document.Document org.apache.lucene.index.* org.apache.lucene.document.* org.apache.lucene.store.* java.io.* java.util.* java.net.* File workDir = null; /** Contextualize this class */ public void contextualize(Context context) throws ContextException { super.contextualize( context ); workDir = (File) context.get(Constants.CONTEXT_WORK_DIR); } Directory directory; void init() throws ProcessingException { try { directory = LuceneCocoonHelper.getDirectory( new File( workDir, "index" ), false ); } catch (Exception e) { throw new ProcessingException( "Exception in init()!", e ); } } int numDocs() throws ProcessingException { try { IndexReader reader = IndexReader.open( directory ); int num_docs; num_docs = reader.numDocs(); return num_docs; } catch (Exception e) { throw new ProcessingException( "Exception in numDocs()!", e ); } } Map allDocuments() throws ProcessingException { try { IndexReader reader = IndexReader.open( directory ); HashMap fieldsStatistic = new HashMap(); for (int i = 0; i < reader.maxDoc(); i++ ) { if (!reader.isDeleted(i)) { Document document = reader.document(i); Enumeration fields = document.fields(); while (fields.hasMoreElements()) { Field f = (Field)fields.nextElement(); String name = f.name(); String value = f.stringValue(); if (value == null) value = "--void--"; String fieldStatistic = name + "/" + value; if (fieldsStatistic.get( fieldStatistic ) == null) { fieldsStatistic.put( fieldStatistic, new Integer(1) ); } else { Integer sum = (Integer)fieldsStatistic.get( fieldStatistic ); int sum_plus = sum.intValue() + 1; fieldsStatistic.put( fieldStatistic, new Integer( sum_plus ) ); } } } } return fieldsStatistic; //map.keySet(); } catch (Exception e) { throw new ProcessingException( "Exception allDocuments()!", e ); } } Map allTerms() throws ProcessingException { try { IndexReader reader = IndexReader.open( directory ); TermEnum te = reader.terms(); HashMap all_fields = new HashMap(); while (te.next()) { Term term = te.term(); int docfreq = te.docFreq(); String field = term.field(); if (field != null) { if (all_fields.containsKey( field )) { Integer sum = (Integer)all_fields.get( field ); int sum_plus = sum.intValue() + docfreq; all_fields.put( field, new Integer( sum_plus ) ); } else { all_fields.put( field, new Integer( docfreq ) ); } } } te.close(); return all_fields; } catch (Exception e) { throw new ProcessingException( "Exception allDocuments()!", e ); } } Map sort( Map map ) { TreeMap treeMap = new TreeMap( map ); return treeMap; } init(); Index Statistics Statistics: Total Count Of Documents String.valueOf(numDocs()) Map all_docs = sort(allDocuments()); Iterator it1 = all_docs.keySet().iterator(); while (it1.hasNext()) { String k = (String)it1.next(); Integer v = (Integer)all_docs.get( k ); }
Count Of TermsFieldname/Fieldvalue
v.toString() k
All Terms Map all_terms = sort(allTerms()); Iterator it2 = all_terms.keySet().iterator(); while (it2.hasNext()) { String k = (String)it2.next(); Integer v = (Integer)all_terms.get( k ); }
Count Of TermsTerm
v.toString() k