1 package org.apache.lucene;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.RandomIndexWriter;
28 import org.apache.lucene.queryParser.ParseException;
29 import org.apache.lucene.queryParser.QueryParser;
30 import org.apache.lucene.search.TopDocs;
31 import org.apache.lucene.search.IndexSearcher;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.store.Directory;
34 import org.apache.lucene.util.LuceneTestCase;
37 * A very simple demo used in the API documentation (src/java/overview.html).
39 * Please try to keep src/java/overview.html up-to-date when making changes
42 public class TestDemo extends LuceneTestCase {
44 public void testDemo() throws IOException, ParseException {
45 Analyzer analyzer = new MockAnalyzer(random);
47 // Store the index in memory:
48 Directory directory = newDirectory();
49 // To store an index on disk, use this instead:
50 //Directory directory = FSDirectory.open("/tmp/testindex");
51 RandomIndexWriter iwriter = new RandomIndexWriter(random, directory, analyzer);
52 iwriter.w.setInfoStream(VERBOSE ? System.out : null);
53 Document doc = new Document();
54 String text = "This is the text to be indexed.";
55 doc.add(newField("fieldname", text, Field.Store.YES,
56 Field.Index.ANALYZED));
57 iwriter.addDocument(doc);
60 // Now search the index:
61 IndexReader ireader = IndexReader.open(directory); // read-only=true
62 IndexSearcher isearcher = new IndexSearcher(ireader);
63 // Parse a simple query that searches for "text":
64 QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "fieldname", analyzer);
65 Query query = parser.parse("text");
66 TopDocs hits = isearcher.search(query, null, 1);
67 assertEquals(1, hits.totalHits);
68 // Iterate through the results:
69 for (int i = 0; i < hits.scoreDocs.length; i++) {
70 Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
71 assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
74 // Test simple phrase query
75 query = parser.parse("\"to be\"");
76 assertEquals(1, isearcher.search(query, null, 1).totalHits);