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 [EMAIL PROTECTED] = --> +<!-- = 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"> </font><font color="#000000">Analyzer analyzer = </font><font color="#7f0055"><b>new </b></font><font color="#000000">StandardAnalyzer</font><font color="#000000">()</font><font color="#000000">;</font><br/> +<font color="#ffffff"></font><br/> +<font color="#ffffff"> </font><font color="#3f7f5f">// Store the index in memory:</font><br/> +<font color="#ffffff"> </font><font color="#000000">Directory directory = </font><font color="#7f0055"><b>new </b></font><font color="#000000">RAMDirectory</font><font color="#000000">()</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#3f7f5f">// To store an index on disk, use this instead (note that the </font><br/> +<font color="#ffffff"> </font><font color="#3f7f5f">// parameter true will overwrite the index in that directory</font><br/> +<font color="#ffffff"> </font><font color="#3f7f5f">// if one exists):</font><br/> +<font color="#ffffff"> </font><font color="#3f7f5f">//Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);</font><br/> +<font color="#ffffff"> </font><font color="#000000">IndexWriter iwriter = </font><font color="#7f0055"><b>new </b></font><font color="#000000">IndexWriter</font><font color="#000000">(</font><font color="#000000">directory, analyzer, </font><font color="#7f0055"><b>true</b></font><font color="#000000">)</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#000000">Document doc = </font><font color="#7f0055"><b>new </b></font><font color="#000000">Document</font><font color="#000000">()</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#000000">String text = </font><font color="#2a00ff">"This is the text to be indexed."</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><b>new </b></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">"fieldname"</font><font color="#000000">, text, Field.Store.YES,</font><br/> +<font color="#ffffff"> </font><font color="#000000">Field.Index.TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </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"> </font><font color="#000000">iwriter.close</font><font color="#000000">()</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><br/> +<font color="#ffffff"> </font><font color="#3f7f5f">// Now search the index:</font><br/> +<font color="#ffffff"> </font><font color="#000000">IndexSearcher isearcher = </font><font color="#7f0055"><b>new </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"> </font><font color="#3f7f5f">// Parse a simple query that searches for "text":</font><br/> +<font color="#ffffff"> </font><font color="#000000">Query query = QueryParser.parse</font><font color="#000000">(</font><font color="#2a00ff">"text"</font><font color="#000000">, </font><font color="#2a00ff">"fieldname"</font><font color="#000000">, analyzer</font><font color="#000000">)</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#000000">Hits hits = isearcher.search</font><font color="#000000">(</font><font color="#000000">query</font><font color="#000000">)</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#000000">assertEquals</font><font color="#000000">(</font><font color="#990000">1</font><font color="#000000">, hits.length</font><font color="#000000">())</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#3f7f5f">// Iterate through the results:</font><br/> +<font color="#ffffff"> </font><font color="#7f0055"><b>for </b></font><font color="#000000">(</font><font color="#7f0055"><b>int </b></font><font color="#000000">i = </font><font color="#990000">0</font><font color="#000000">; i < hits.length</font><font color="#000000">()</font><font color="#000000">; i++</font><font color="#000000">) {</font><br/> +<font color="#ffffff"> </font><font color="#000000">Document hitDoc = hits.doc</font><font color="#000000">(</font><font color="#000000">i</font><font color="#000000">)</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#000000">assertEquals</font><font color="#000000">(</font><font color="#2a00ff">"This is the text to be indexed."</font><font color="#000000">, hitDoc.get</font><font color="#000000">(</font><font color="#2a00ff">"fieldname"</font><font color="#000000">))</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </font><font color="#000000">}</font><br/> +<font color="#ffffff"> </font><font color="#000000">isearcher.close</font><font color="#000000">()</font><font color="#000000">;</font><br/> +<font color="#ffffff"> </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>. -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. 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. 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>. +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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]