You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by st...@apache.org on 2001/12/11 23:54:25 UTC

cvs commit: xml-cocoon2/webapp/search index.xsp search.xsp sitemap.xmap statistics.xsp welcome.xsp

stefano     01/12/11 14:54:25

  Added:       webapp/search index.xsp search.xsp sitemap.xmap
                        statistics.xsp welcome.xsp
  Log:
  adding the search pages
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/webapp/search/index.xsp
  
  Index: index.xsp
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <xsp:page
            language="java"
            xmlns:xsp="http://apache.org/xsp"
            xmlns:xsp-request="http://apache.org/xsp/request/2.0"
            xmlns:log="http://apache.org/xsp/log/2.0"
            xmlns:capture="http://apache.org/cocoon/capture/1.0"
  >
  
    <xsp:structure>
     <xsp:include>org.apache.avalon.framework.context.ContextException</xsp:include>
     <xsp:include>org.apache.avalon.framework.component.ComponentException</xsp:include>
     <xsp:include>org.apache.cocoon.ProcessingException</xsp:include>
     <xsp:include>java.net.URL</xsp:include>
     <xsp:include>java.net.MalformedURLException</xsp:include>
     <xsp:include>org.apache.cocoon.components.language.markup.xsp.XSPUtil</xsp:include>
     <xsp:include>org.apache.avalon.framework.context.ContextException</xsp:include>
     <xsp:include>org.apache.cocoon.components.lucene.*</xsp:include>
     <xsp:include>org.apache.lucene.store.Directory</xsp:include>
     <xsp:include>org.apache.lucene.analysis.Analyzer</xsp:include>
    </xsp:structure>
  
    <xsp:logic>
    File workDir = null;
    
    /** Contextualize this class */
    public void contextualize(Context context) throws ContextException {
      super.contextualize( context );
      workDir = (File) context.get(Constants.CONTEXT_WORK_DIR);
    }
    
    LuceneCocoonIndexer lcii;
    Analyzer analyzer = LuceneCocoonHelper.getAnalyzer( "org.apache.lucene.analysis.standard.StandardAnalyzer" );
    
    void createIndex() throws ProcessingException {
      try {
        boolean create = true;
        lcii = (LuceneCocoonIndexer)this.manager.lookup( LuceneCocoonIndexer.ROLE );
        Directory directory = LuceneCocoonHelper.getDirectory( new File( workDir, "index" ), create );
        lcii.setAnalyzer( analyzer );
        URL base_url = new URL( "http://localhost/cocoon/documents/index.html" );
        lcii.index( directory, create, base_url );
        this.manager.release( lcii );
      } catch (MalformedURLException mue) {
        throw new ProcessingException( "MalformedURLException in createIndex()!", mue );
      } catch (IOException ioe) {
        // ignore ??
        throw new ProcessingException( "IOException in createIndex()!", ioe );
  //    } catch (ProcessingException pe) {
        // ignore ??
  //      throw new ProcessingException( "ProcessingException in createIndex()!", pe );
      } catch (ComponentException ce) {
        // ignore ??
        throw new ProcessingException( "ComponentException in createIndex()!", ce );
      } finally {
        if (lcii != null) {
          this.manager.release( lcii );
        }
        lcii = null;
      }
    }
  
    </xsp:logic>
  
    <page>
      <xsp:logic>
        createIndex();
      </xsp:logic>
      <content>
        <para>
          Hello Cocoon-indexer
        </para>
      </content>
    </page>
  
  </xsp:page>
  
  
  
  
  1.1                  xml-cocoon2/webapp/search/search.xsp
  
  Index: search.xsp
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <xsp:page
            language="java"
            xmlns:xsp="http://apache.org/xsp"
            xmlns:xsp-request="http://apache.org/xsp/request/2.0"
            xmlns:log="http://apache.org/xsp/log/2.0"
            xmlns:capture="http://apache.org/cocoon/capture/1.0"
  >
    <xsp:structure>
      <xsp:include>org.apache.avalon.framework.context.ContextException</xsp:include>
      <xsp:include>org.apache.avalon.framework.component.ComponentException</xsp:include>
      
      <xsp:include>org.apache.cocoon.ProcessingException</xsp:include>
      <xsp:include>org.apache.cocoon.components.language.markup.xsp.XSPUtil</xsp:include>
      
      <xsp:include>org.apache.cocoon.components.lucene.*</xsp:include>
      
      <xsp:include>org.apache.lucene.analysis.Analyzer</xsp:include>
      <xsp:include>org.apache.lucene.store.*</xsp:include>
      <xsp:include>org.apache.lucene.document.*</xsp:include>
      <xsp:include>org.apache.lucene.search.Hits</xsp:include>
      
      <xsp:include>java.util.*</xsp:include>
    </xsp:structure>
  
    <xsp:logic>
      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 ??
          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;
    </xsp:logic>
  
    <page>
      <xsp:logic>
        String queryString = <xsp-request:get-parameter name="queryString" default=""/>;
        boolean findIt = "Find!".equals( <xsp-request:get-parameter name="find"/> );
        Integer startIndex = null;
        Integer pageLength = null;
        try {
          startIndex = new Integer( <xsp-request:get-parameter name="startIndex" default="0"/> );
          pageLength = new Integer( <xsp-request:get-parameter name="pageLength" default="10"/> );
        } catch (NumberFormatException nfe) {
          // ignore it
        }
      </xsp:logic>
      
      <title>Index Search</title>
      <content>
        <para>
          <form action="search">
            <input type="text" name="queryString" size="60">
              <xsp:attribute name="value"><xsp:expr>queryString</xsp:expr></xsp:attribute>
            </input>
            <input type="submit" name="find" value="Find!"/>
          </form>
        </para>
        <para>
          Search Help
          <table cellspacing="2" cellpadding="2">
            <tr bgcolor="#dddedd" valign="top">
            <td><font size="-2">
              <ul>
                <li>free AND "text search"
                  Search for documents containing "free" and the 
                  phrase "text search"
                </li>
                <li>+text search
                  Search for documents containing "text" and 
                  preferentially containing "search".
                </li>
              </ul>
            </font></td>
            
            <td><font size="-2">
              <ul>
                <li>giants -football
                  Search for "giants" but omit documents containing "football"
                </li>
                <li>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".
                </li>
                <li>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.
                </li>
              </ul>
            </font></td>
            </tr>
          </table>
        </para>
        <para>
          SearchResult:
          <xsp:logic>
            if (queryString != null &amp;&amp; queryString.length() != 0) {
              
              // do the search, search results are available in hits
              hits = search( queryString );
              luceneCocoonPager = new LuceneCocoonPager( hits );
              if (startIndex != null &amp;&amp; pageLength != null) {
                luceneCocoonPager.setStartIndex( startIndex.intValue() );
                luceneCocoonPager.setCountOfHitsPerPage( pageLength.intValue() );
              }
              <xsp:content>
                Total Hits: <xsp:expr>hits.length()</xsp:expr>
              </xsp:content>
            }
          </xsp:logic>
        </para>
        <para>
          <a target="_blank" href="statistic">Index Statistic</a>
        </para>
        <para>
          <table width="90%" cellpadding="4" border="1">
            <tr>
              <td>Score</td><td>Count</td><td>URL</td>
            </tr>
            <xsp:logic>
              if (luceneCocoonPager!= null &amp;&amp; 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 );
                  
                  <xsp:content>
                    <tr>
                      <td> <xsp:expr>String.valueOf((int)(score * 100.0f))</xsp:expr>% </td> 
                      <td> <xsp:expr>String.valueOf(counter)</xsp:expr> </td>
                      <td>
                        <a target="_blank">
                          <xsp:attribute name="href"><xsp:expr>url</xsp:expr></xsp:attribute>
                          <xsp:expr>url</xsp:expr>
                        </a>
                      </td>
                    </tr>
                  </xsp:content>
                }
              }
            </xsp:logic>
          </table>
        </para>
        <para>
          <xsp:logic>
            if (luceneCocoonPager!= null &amp;&amp; luceneCocoonPager != null &amp;&amp; luceneCocoonPager.hasPrevious()) {
              <xsp:content>previous <xsp:expr>String.valueOf(luceneCocoonPager.previousIndex())</xsp:expr></xsp:content>
              <form action="search">
                <input type="hidden" name="queryString">
                  <xsp:attribute name="value"><xsp:expr>String.valueOf(queryString)</xsp:expr></xsp:attribute>
                </input>
                <input type="hidden" name="startIndex">
                  <xsp:attribute name="value"><xsp:expr>String.valueOf(luceneCocoonPager.previousIndex())</xsp:expr></xsp:attribute>
                </input>
                <input type="hidden" name="pageLength">
                  <xsp:attribute name="value"><xsp:expr>String.valueOf(luceneCocoonPager.getCountOfHitsPerPage())</xsp:expr></xsp:attribute>
                </input>
                <input type="submit" name="previous" value="previous"/>
              </form>
            }
          </xsp:logic>
          <xsp:logic>
            if (luceneCocoonPager != null &amp;&amp; luceneCocoonPager.hasNext()) {
              <xsp:content>next <xsp:expr>String.valueOf(luceneCocoonPager.nextIndex())</xsp:expr></xsp:content>
              <form action="search">
                <input type="hidden" name="queryString">
                  <xsp:attribute name="value"><xsp:expr>String.valueOf(queryString)</xsp:expr></xsp:attribute>
                </input>
                <input type="hidden" name="startIndex">
                  <xsp:attribute name="value"><xsp:expr>String.valueOf(luceneCocoonPager.nextIndex())</xsp:expr></xsp:attribute>
                </input>
                <input type="hidden" name="pageLength">
                  <xsp:attribute name="value"><xsp:expr>String.valueOf(luceneCocoonPager.getCountOfHitsPerPage())</xsp:expr></xsp:attribute>
                </input>
                <input type="submit" name="next" value="next"/>
              </form>
            }
          </xsp:logic>
        </para>
      </content>
    </page>
  
  </xsp:page>
  
  
  
  
  1.1                  xml-cocoon2/webapp/search/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  <?xml version="1.0"?>
  
  <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
  
  <!-- =========================== Components ================================
  
   <map:components>
  
    <map:generators default="file"/>
  
    <map:transformers default="xslt"/>
  
    <map:readers default="resource"/>
  
    <map:serializers default="html"/>
  
    <map:selectors default="browser"/>
  
    <map:matchers default="wildcard"/>
  
   </map:components>
  
   -->
   
  <!-- =========================== Pipelines ================================= -->
  
   <map:pipelines>
    <map:pipeline>
  
     <map:match pattern="">
      <map:redirect-to uri="welcome"/>
     </map:match>
  
     <map:match pattern="*">
      <map:generate type="serverpages" src="{1}.xsp"/>
      <map:transform src="context://stylesheets/dynamic-page2html.xsl">
      <map:serialize/>
     </map:match>
  
     <map:handle-errors>
      <map:transform src="cocoon://stylesheets/system/error2html.xsl"/>
      <map:serialize status-code="500"/>
     </map:handle-errors>
    </map:pipeline>
   </map:pipelines>
  
  </map:sitemap>
  
  
  1.1                  xml-cocoon2/webapp/search/statistics.xsp
  
  Index: statistics.xsp
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <xsp:page
            language="java"
            xmlns:xsp="http://apache.org/xsp"
            xmlns:xsp-request="http://apache.org/xsp/request/2.0"
            xmlns:log="http://apache.org/xsp/log/2.0"
            xmlns:capture="http://apache.org/cocoon/capture/1.0"
  >
    <xsp:structure>
      <xsp:include>org.apache.avalon.framework.context.ContextException</xsp:include>
      <xsp:include>org.apache.cocoon.components.lucene.*</xsp:include>
      
      <xsp:include>org.apache.lucene.document.Document</xsp:include>
      <xsp:include>org.apache.lucene.index.*</xsp:include>
      <xsp:include>org.apache.lucene.document.*</xsp:include>
      <xsp:include>org.apache.lucene.store.*</xsp:include>
      
      <xsp:include>java.io.*</xsp:include>
      <xsp:include>java.util.*</xsp:include>
      <xsp:include>java.net.*</xsp:include>
    </xsp:structure>
    
    <xsp:logic>
      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 &lt; 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;
      }
    </xsp:logic>
    
    <page>
      <xsp:logic>
        init();
      </xsp:logic>
      <title>Index Statistics</title>
      <content>
        <para>
          Statistics:
        </para>
        <para>
          Total Count Of Documents
          <xsp:expr>String.valueOf(numDocs())</xsp:expr>
        </para>
        <para>
          <table>
            <tr>
              <td>Count Of Terms</td><td>Fieldname/Fieldvalue</td>
            </tr>
            <xsp:logic>
              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 );
                <xsp:content>
                  <tr>
                    <td> <xsp:expr>v.toString()</xsp:expr> </td>
                    <td> <xsp:expr>k</xsp:expr> </td>
                  </tr>
                </xsp:content>
              }
            </xsp:logic>
          </table>
        </para>
        <para>
          All Terms
        </para>
        <para>
          <table>
            <tr>
              <td>Count Of Terms</td><td>Term</td>
            </tr>
            <xsp:logic>
              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 );
                <xsp:content>
                  <tr>
                    <td> <xsp:expr>v.toString()</xsp:expr> </td>
                    <td> <xsp:expr>k</xsp:expr> </td> 
                  </tr>
                </xsp:content>
              }
            </xsp:logic>
          </table>
        </para>
      </content>
    </page>
  
  </xsp:page>
  
  
  
  
  1.1                  xml-cocoon2/webapp/search/welcome.xsp
  
  Index: welcome.xsp
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <xsp:page
            language="java"
            xmlns:xsp="http://apache.org/xsp"
            xmlns:xsp-request="http://apache.org/xsp/request/2.0"
            xmlns:log="http://apache.org/xsp/log/2.0"
            xmlns:capture="http://apache.org/cocoon/capture/1.0"
  >
    <page>
      <title>Welcome</title>
      <content>
  
        <h1>Lucene Search Demo</h1>
  
        <center>
          <form name="search" action="search" method="get">
            <table>
              <tr><td></td>
                <td>
                  <input name="queryString" size="44"/>&#160;<input type="submit" value="Search"/>
                </td>
              </tr>
              <tr>
                <td>Index</td>
                <td>
                  <select name="index">
                    <option value="index">index</option>
                    <option value="index-1">index-1</option>
                    <option value="index-2">index-2</option>
                    <option value="index-3">index-3</option>
                  </select>
                </td>
              </tr>
              <tr>
                <td>Analyzer Class</td>
                <td>
                  <select name="analyzerClassName">
                    <option value="org.apache.lucene.analysis.StopAnalyzer">StopAnalyzer</option>
                    <option value="org.apache.lucene.analysis.standard.StandardAnalyzer">StandardAnalyzer</option>
                    <option value="org.apache.lucene.analysis.de.GermanAnalyzer">GermanAnalyzer</option>
                    <option value="org.apache.lucene.analysis.SimpleAnalyzer">SimpleAnalyzer</option>
                  </select>
                </td>
              </tr>
            </table>
          </form>
        </center>
      </content>
    </page>
  </xsp:page>
  
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org