01: package org.apache.lucene;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import org.apache.lucene.util.LuceneTestCase;
21: import org.apache.lucene.analysis.Analyzer;
22: import org.apache.lucene.analysis.standard.StandardAnalyzer;
23: import org.apache.lucene.document.Document;
24: import org.apache.lucene.document.Field;
25: import org.apache.lucene.index.IndexWriter;
26: import org.apache.lucene.queryParser.ParseException;
27: import org.apache.lucene.queryParser.QueryParser;
28: import org.apache.lucene.search.Hits;
29: import org.apache.lucene.search.IndexSearcher;
30: import org.apache.lucene.search.Query;
31: import org.apache.lucene.store.Directory;
32: import org.apache.lucene.store.RAMDirectory;
33:
34: import java.io.IOException;
35:
36: /**
37: * A very simple demo used in the API documentation (src/java/overview.html).
38: *
39: * @author Daniel Naber
40: */
41: public class TestDemo extends LuceneTestCase {
42:
43: public void testDemo() throws IOException, ParseException {
44:
45: Analyzer analyzer = new StandardAnalyzer();
46:
47: // Store the index in memory:
48: Directory directory = new RAMDirectory();
49: // To store an index on disk, use this instead (note that the
50: // parameter true will overwrite the index in that directory
51: // if one exists):
52: //Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);
53: IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
54: iwriter.setMaxFieldLength(25000);
55: Document doc = new Document();
56: String text = "This is the text to be indexed.";
57: doc.add(new Field("fieldname", text, Field.Store.YES,
58: Field.Index.TOKENIZED));
59: iwriter.addDocument(doc);
60: iwriter.close();
61:
62: // Now search the index:
63: IndexSearcher isearcher = new IndexSearcher(directory);
64: // Parse a simple query that searches for "text":
65: QueryParser parser = new QueryParser("fieldname", analyzer);
66: Query query = parser.parse("text");
67: Hits hits = isearcher.search(query);
68: assertEquals(1, hits.length());
69: // Iterate through the results:
70: for (int i = 0; i < hits.length(); i++) {
71: Document hitDoc = hits.doc(i);
72: assertEquals("This is the text to be indexed.", hitDoc
73: .get("fieldname"));
74: }
75: isearcher.close();
76: directory.close();
77:
78: }
79:
80: }
|