mirror of https://github.com/apache/lucene.git
update example so no deprecated code is used
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@703881 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7ecd3260ad
commit
c027596aa2
|
@ -24,6 +24,7 @@
|
||||||
Here's a simple example how to use Lucene for indexing and searching (using JUnit
|
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>
|
to check if the results are what we expect):</p>
|
||||||
|
|
||||||
|
<!-- code comes from org.apache.lucene.TestDemo: -->
|
||||||
<!-- ======================================================== -->
|
<!-- ======================================================== -->
|
||||||
<!-- = Java Sourcecode to HTML automatically converted code = -->
|
<!-- = Java Sourcecode to HTML automatically converted code = -->
|
||||||
<!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
|
<!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
|
||||||
|
@ -40,14 +41,13 @@ to check if the results are what we expect):</p>
|
||||||
<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="#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:</font><br />
|
<font color="#ffffff"> </font><font color="#3f7f5f">// To store an index on disk, use this instead:</font><br />
|
||||||
<font color="#ffffff"> </font><font color="#3f7f5f">//Directory directory = FSDirectory.getDirectory("/tmp/testindex");</font><br />
|
<font color="#ffffff"> </font><font color="#3f7f5f">//Directory directory = FSDirectory.getDirectory("/tmp/testindex");</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">IndexWriter iwriter = </font><font color="#7f0055"><b>new </b></font><font color="#000000">IndexWriter</font><font color="#000000">(</font><font color="#000000">directory, analyzer, true,</font><br />
|
||||||
<font color="#ffffff"> </font><font color="#000000">iwriter.setMaxFieldLength</font><font color="#000000">(</font><font color="#990000">25000</font><font color="#000000">)</font><font color="#000000">;</font><br />
|
<font color="#ffffff"> </font><font color="#7f0055"><b>new </b></font><font color="#000000">IndexWriter.MaxFieldLength</font><font color="#000000">(</font><font color="#990000">25000</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">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">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">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">Field.Index.ANALYZED</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.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.optimize</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><font color="#000000">iwriter.close</font><font color="#000000">()</font><font color="#000000">;</font><br />
|
||||||
<font color="#ffffff"> </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="#3f7f5f">// Now search the index:</font><br />
|
||||||
|
@ -55,11 +55,11 @@ to check if the results are what we expect):</p>
|
||||||
<font color="#ffffff"> </font><font color="#3f7f5f">// Parse a simple query that searches for "text":</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">QueryParser parser = </font><font color="#7f0055"><b>new </b></font><font color="#000000">QueryParser</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">QueryParser parser = </font><font color="#7f0055"><b>new </b></font><font color="#000000">QueryParser</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">Query query = parser.parse</font><font color="#000000">(</font><font color="#2a00ff">"text"</font><font color="#000000">)</font><font color="#000000">;</font><br />
|
<font color="#ffffff"> </font><font color="#000000">Query query = parser.parse</font><font color="#000000">(</font><font color="#2a00ff">"text"</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">ScoreDoc</font><font color="#000000">[] </font><font color="#000000">hits = isearcher.search</font><font color="#000000">(</font><font color="#000000">query, null, </font><font color="#990000">1000</font><font color="#000000">)</font><font color="#000000">.scoreDocs;</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="#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="#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="#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; 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">Document hitDoc = isearcher.doc</font><font color="#000000">(</font><font color="#000000">hits</font><font color="#000000">[</font><font color="#000000">i</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">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">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">}</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">isearcher.close</font><font color="#000000">()</font><font color="#000000">;</font><br />
|
||||||
|
|
Loading…
Reference in New Issue