lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dna...@apache.org
Subject cvs commit: jakarta-lucene/src/java overview.html
Date Sat, 11 Dec 2004 11:15:51 GMT
dnaber      2004/12/11 03:15:51

  Modified:    src/java overview.html
  Added:       src/test/org/apache/lucene TestDemo.java
  Log:
  adding a simple indexing and searching example to the API documentation
  
  Revision  Changes    Path
  1.1                  jakarta-lucene/src/test/org/apache/lucene/TestDemo.java
  
  Index: TestDemo.java
  ===================================================================
  package org.apache.lucene;
  
  /**
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.io.IOException;
  
  import org.apache.lucene.analysis.Analyzer;
  import org.apache.lucene.analysis.standard.StandardAnalyzer;
  import org.apache.lucene.document.Document;
  import org.apache.lucene.document.Field;
  import org.apache.lucene.index.IndexWriter;
  import org.apache.lucene.queryParser.ParseException;
  import org.apache.lucene.queryParser.QueryParser;
  import org.apache.lucene.search.Hits;
  import org.apache.lucene.search.IndexSearcher;
  import org.apache.lucene.search.Query;
  import org.apache.lucene.store.Directory;
  import org.apache.lucene.store.RAMDirectory;
  
  import junit.framework.TestCase;
  
  /**
   * A very simple demo used in the API documentation (src/java/overview.html).
   * 
   * @author Daniel Naber
   */
  public class TestDemo extends TestCase {
  
    public void testDemo() throws IOException, ParseException {
  
      Analyzer analyzer = new StandardAnalyzer();
  
      // Store the index in memory:
      Directory directory = new RAMDirectory();
      // To store an index on disk, use this instead (note that the 
      // parameter true will overwrite the index in that directory
      // if one exists):
      //Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);
      IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
      Document doc = new Document();
      String text = "This is the text to be indexed.";
      doc.add(new Field("fieldname", text, Field.Store.YES,
          Field.Index.TOKENIZED));
      iwriter.addDocument(doc);
      iwriter.close();
      
      // Now search the index:
      IndexSearcher isearcher = new IndexSearcher(directory);
      // Parse a simple query that searches for "text":
      Query query = QueryParser.parse("text", "fieldname", analyzer);
      Hits hits = isearcher.search(query);
      assertEquals(1, hits.length());
      // Iterate through the results:
      for (int i = 0; i < hits.length(); i++) {
        Document hitDoc = hits.doc(i);
        assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
      }
      isearcher.close();
      directory.close();
      
    }
  
  }
  
  
  
  1.15      +73 -22    jakarta-lucene/src/java/overview.html
  
  Index: overview.html
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/overview.html,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- overview.html	10 Dec 2004 22:18:35 -0000	1.14
  +++ overview.html	11 Dec 2004 11:15:51 -0000	1.15
  @@ -4,30 +4,61 @@
   </head>
   <body>
   
  -Jakarta Lucene is a high-performance, full-featured text search engine library.
  -The API is divided into several packages:
  +<p>Jakarta Lucene is a high-performance, full-featured text search engine library.
  +Here's a simple example how to use Lucene for indexing and searching (using JUnit
  +to check if the results are what we expect):</p>
  +
  +<!-- ======================================================== -->
  +<!-- = Java Sourcecode to HTML automatically converted code = -->
  +<!-- =   Java2Html Converter V4.1 2004 by Markus Gebhard  markus@jave.de   = -->
  +<!-- =     Further information: http://www.java2html.de     = -->
  +<div align="left" class="java">
  +<table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
  +   <tr>
  +  <!-- start source code -->
  +   <td nowrap="nowrap" valign="top" align="left">
  +    <code>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">Analyzer&nbsp;analyzer&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font
color="#000000">StandardAnalyzer</font><font color="#000000">()</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff"></font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//&nbsp;Store&nbsp;the&nbsp;index&nbsp;in&nbsp;memory:</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">Directory&nbsp;directory&nbsp;=&nbsp;</font><font
color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">RAMDirectory</font><font
color="#000000">()</font><font color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//&nbsp;To&nbsp;store&nbsp;an&nbsp;index&nbsp;on&nbsp;disk,&nbsp;use&nbsp;this&nbsp;instead&nbsp;(note&nbsp;that&nbsp;the&nbsp;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//&nbsp;parameter&nbsp;true&nbsp;will&nbsp;overwrite&nbsp;the&nbsp;index&nbsp;in&nbsp;that&nbsp;directory</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//&nbsp;if&nbsp;one&nbsp;exists):</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//Directory&nbsp;directory&nbsp;=&nbsp;FSDirectory.getDirectory(&#34;/tmp/testindex&#34;,&nbsp;true);</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">IndexWriter&nbsp;iwriter&nbsp;=&nbsp;</font><font
color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">IndexWriter</font><font
color="#000000">(</font><font color="#000000">directory,&nbsp;analyzer,&nbsp;</font><font
color="#7f0055"><b>true</b></font><font color="#000000">)</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">Document&nbsp;doc&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font
color="#000000">Document</font><font color="#000000">()</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">String&nbsp;text&nbsp;=&nbsp;</font><font color="#2a00ff">&#34;This&nbsp;is&nbsp;the&nbsp;text&nbsp;to&nbsp;be&nbsp;indexed.&#34;</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">doc.add</font><font color="#000000">(</font><font
color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">Field</font><font
color="#000000">(</font><font color="#2a00ff">&#34;fieldname&#34;</font><font
color="#000000">,&nbsp;text,&nbsp;Field.Store.YES,</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">Field.Index.TOKENIZED</font><font color="#000000">))</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">iwriter.addDocument</font><font color="#000000">(</font><font
color="#000000">doc</font><font color="#000000">)</font><font color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">iwriter.close</font><font color="#000000">()</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//&nbsp;Now&nbsp;search&nbsp;the&nbsp;index:</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">IndexSearcher&nbsp;isearcher&nbsp;=&nbsp;</font><font
color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">IndexSearcher</font><font
color="#000000">(</font><font color="#000000">directory</font><font
color="#000000">)</font><font color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//&nbsp;Parse&nbsp;a&nbsp;simple&nbsp;query&nbsp;that&nbsp;searches&nbsp;for&nbsp;&#34;text&#34;:</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">Query&nbsp;query&nbsp;=&nbsp;QueryParser.parse</font><font
color="#000000">(</font><font color="#2a00ff">&#34;text&#34;</font><font
color="#000000">,&nbsp;</font><font color="#2a00ff">&#34;fieldname&#34;</font><font
color="#000000">,&nbsp;analyzer</font><font color="#000000">)</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">Hits&nbsp;hits&nbsp;=&nbsp;isearcher.search</font><font
color="#000000">(</font><font color="#000000">query</font><font color="#000000">)</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">assertEquals</font><font color="#000000">(</font><font
color="#990000">1</font><font color="#000000">,&nbsp;hits.length</font><font
color="#000000">())</font><font color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#3f7f5f">//&nbsp;Iterate&nbsp;through&nbsp;the&nbsp;results:</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#7f0055"><b>for&nbsp;</b></font><font color="#000000">(</font><font
color="#7f0055"><b>int&nbsp;</b></font><font color="#000000">i&nbsp;=&nbsp;</font><font
color="#990000">0</font><font color="#000000">;&nbsp;i&nbsp;&lt;&nbsp;hits.length</font><font
color="#000000">()</font><font color="#000000">;&nbsp;i++</font><font
color="#000000">)&nbsp;{</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">Document&nbsp;hitDoc&nbsp;=&nbsp;hits.doc</font><font
color="#000000">(</font><font color="#000000">i</font><font color="#000000">)</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">assertEquals</font><font color="#000000">(</font><font
color="#2a00ff">&#34;This&nbsp;is&nbsp;the&nbsp;text&nbsp;to&nbsp;be&nbsp;indexed.&#34;</font><font
color="#000000">,&nbsp;hitDoc.get</font><font color="#000000">(</font><font
color="#2a00ff">&#34;fieldname&#34;</font><font color="#000000">))</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">}</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">isearcher.close</font><font color="#000000">()</font><font
color="#000000">;</font><br/>
  +<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font
color="#000000">directory.close</font><font color="#000000">()</font><font
color="#000000">;</font></code>
  +    
  +   </td>
  +  <!-- end source code -->
  +   </tr>
  +</table>
  +</div>
  +<!-- =       END of automatically generated HTML code       = -->
  +<!-- ======================================================== -->
   
  -<ul>
  -<li>
  -<b><a href="org/apache/lucene/util/package-summary.html">org.apache.lucene.util</a></b>
  -contains a few handy data structures, e.g., <a href="org/apache/lucene/util/BitVector.html">BitVector</a>
  -and <a href="org/apache/lucene/util/PriorityQueue.html">PriorityQueue</a>.</li>
  -
  -<li>
  -<b><a href="org/apache/lucene/store/package-summary.html">org.apache.lucene.store</a></b>
  -defines an abstract class for storing persistent data, the <a href="org/apache/lucene/store/Directory.html">Directory</a>,
  -a collection of named files written by an <a href="org/apache/lucene/store/OutputStream.html">OutputStream</a>
  -and read by an <a href="org/apache/lucene/store/IndexInput.html">IndexInput</a>.&nbsp;
  -Two implementations are provided, <a href="org/apache/lucene/store/FSDirectory.html">FSDirectory</a>,
  -which uses a file system directory to store files, and <a href="org/apache/lucene/store/RAMDirectory.html">RAMDirectory</a>
  -which implements files as memory-resident data structures.</li>
  -
  -<li>
  -<b><a href="org/apache/lucene/document/package-summary.html">org.apache.lucene.document</a></b>
  -provides a simple <a href="org/apache/lucene/document/Document.html">Document</a>
  -class.&nbsp; A document is simply a set of named <a href="org/apache/lucene/document/Field.html">Field</a>'s,
  -whose values may be strings or instances of <a href="http://java.sun.com/products/jdk/1.2/docs/api/java/io/Reader.html">java.io.Reader</a>.</li>
  +<p>The Lucene API is divided into several packages:</p>
   
  +<ul>
   <li>
   <b><a href="org/apache/lucene/analysis/package-summary.html">org.apache.lucene.analysis</a></b>
   defines an abstract <a href="org/apache/lucene/analysis/Analyzer.html">Analyzer</a>
  @@ -40,6 +71,12 @@
   and the grammar-based <a href="org/apache/lucene/analysis/standard/StandardAnalyzer.html">StandardAnalyzer</a>.</li>
   
   <li>
  +<b><a href="org/apache/lucene/document/package-summary.html">org.apache.lucene.document</a></b>
  +provides a simple <a href="org/apache/lucene/document/Document.html">Document</a>
  +class.&nbsp; A document is simply a set of named <a href="org/apache/lucene/document/Field.html">Field</a>'s,
  +whose values may be strings or instances of <a href="http://java.sun.com/products/jdk/1.2/docs/api/java/io/Reader.html">java.io.Reader</a>.</li>
  +
  +<li>
   <b><a href="org/apache/lucene/index/package-summary.html">org.apache.lucene.index</a></b>
   provides two primary classes: <a href="org/apache/lucene/index/IndexWriter.html">IndexWriter</a>,
   which creates and adds documents to indices; and <a href="org/apache/lucene/index/IndexReader.html">IndexReader</a>,
  @@ -59,6 +96,20 @@
   <b><a href="org/apache/lucene/queryParser/package-summary.html">org.apache.lucene.queryParser</a></b>
   uses <a href="http://javacc.dev.java.net">JavaCC</a> to implement a
   <a href="org/apache/lucene/queryParser/QueryParser.html">QueryParser</a>.</li>
  +
  +<li>
  +<b><a href="org/apache/lucene/store/package-summary.html">org.apache.lucene.store</a></b>
  +defines an abstract class for storing persistent data, the <a href="org/apache/lucene/store/Directory.html">Directory</a>,
  +a collection of named files written by an <a href="org/apache/lucene/store/IndexOutput.html">IndexOutput</a>
  +and read by an <a href="org/apache/lucene/store/IndexInput.html">IndexInput</a>.&nbsp;
  +Two implementations are provided, <a href="org/apache/lucene/store/FSDirectory.html">FSDirectory</a>,
  +which uses a file system directory to store files, and <a href="org/apache/lucene/store/RAMDirectory.html">RAMDirectory</a>
  +which implements files as memory-resident data structures.</li>
  +
  +<li>
  +<b><a href="org/apache/lucene/util/package-summary.html">org.apache.lucene.util</a></b>
  +contains a few handy data structures, e.g., <a href="org/apache/lucene/util/BitVector.html">BitVector</a>
  +and <a href="org/apache/lucene/util/PriorityQueue.html">PriorityQueue</a>.</li>
   </ul>
   To use Lucene, an application should:
   <ol>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message