org.apache.avalon.framework.context.ContextException org.apache.avalon.framework.component.ComponentException org.apache.cocoon.ProcessingException org.apache.cocoon.components.language.markup.xsp.XSPUtil org.apache.cocoon.components.search.* org.apache.lucene.analysis.Analyzer org.apache.lucene.store.* org.apache.lucene.document.* org.apache.lucene.search.Hits java.util.* File workDir = null; /** Contextualize this class */ public void contextualize(Context context) throws ContextException { super.contextualize( context ); workDir = (File) context.get(Constants.CONTEXT_WORK_DIR); } LuceneCocoonSearcher lcs; Directory directory; Analyzer analyzer = LuceneCocoonHelper.getAnalyzer( "org.apache.lucene.analysis.standard.StandardAnalyzer" ); Hits search( String query_string ) throws ProcessingException { Hits hits = null; try { lcs = (LuceneCocoonSearcher)this.manager.lookup( LuceneCocoonSearcher.ROLE ); lcs.setAnalyzer( analyzer ); if (directory == null) { directory = LuceneCocoonHelper.getDirectory( new File( workDir, "index" ), false ); } lcs.setDirectory( directory ); hits = lcs.search( query_string, LuceneXMLIndexer.BODY_FIELD ); start = 0; end = Math.min(hits.length(), start + hitsPerPage); } catch (IOException ioe) { // ignore ?? throw new ProcessingException( "IOException in search", ioe ); } catch (ProcessingException pe) { // ignore ?? Throwable t = pe.getCause(); if (t instanceof org.apache.lucene.queryParser.ParseException) { // ignore it or write info about reason } else { throw new ProcessingException( "ProcessingException in search", pe ); } } catch (ComponentException ce) { // ignore ?? throw new ProcessingException( "ComponentException in search", ce ); } finally { if (lcs != null) { this.manager.release( lcs ); } lcs = null; } return hits; } int hitsPerPage = 10; int start = 0; int end = 0; Hits hits; LuceneCocoonPager luceneCocoonPager; String queryString = ; boolean findIt = "Find!".equals( ); Integer startIndex = null; Integer pageLength = null; try { if ( != null) { startIndex = new Integer( ); } else if ( != null) { startIndex = new Integer( ); } else { startIndex = new Integer( 0 ); } pageLength = new Integer( ); } catch (NumberFormatException nfe) { // ignore it } Cocoon XML Search Interface Lucene Logo Index Statistics | Welcome
queryString
Help by example (see also the Lucene FAQ)
  • free AND "text search" Search for documents which contain the word "free" and the phrase "text search"
  • +text search Search for documents which must contain the word "text" and optionally contain the word "search".
  • giants -football Search for "giants" but omit documents containing "football"
  • body:john Search for documents containing "john" in the body field. The field "body" is used by default. Thus query "body:john" is equivalent to query "john".
  • s1@title:cocoon Search for documents containing "cocoon" in the using field s1@title, ie searching in title attribute of s1 element of xml document.
SearchResult: if (queryString != null && queryString.length() != 0) { // do the search, search results are available in hits hits = search( queryString ); luceneCocoonPager = new LuceneCocoonPager( hits ); if (startIndex != null && pageLength != null) { luceneCocoonPager.setStartIndex( startIndex.intValue() ); luceneCocoonPager.setCountOfHitsPerPage( pageLength.intValue() ); } Total Hits: hits.length() } if (luceneCocoonPager!= null && luceneCocoonPager.hasNext()) { int counter = luceneCocoonPager.getStartIndex(); List l = (List)luceneCocoonPager.next(); Iterator i = l.iterator(); for (; i.hasNext(); counter++) { LuceneCocoonPager.HitWrapper hw = (LuceneCocoonPager.HitWrapper)i.next(); Document doc = hw.getDocument(); float score = hw.getScore(); String url = doc.get( LuceneXMLIndexer.URL_FIELD ); } }
ScoreCountURL
String.valueOf((int)(score * 100.0f))% String.valueOf(counter + 1) url url
if (luceneCocoonPager!= null && luceneCocoonPager != null && (luceneCocoonPager.hasNext() || luceneCocoonPager.hasPrevious())) {
String.valueOf(queryString) String.valueOf(luceneCocoonPager.getCountOfHitsPerPage()) if (luceneCocoonPager.hasPrevious()) { String.valueOf(luceneCocoonPager.previousIndex()) }   if (luceneCocoonPager.hasNext()) { String.valueOf(luceneCocoonPager.nextIndex()) }
}