pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / TestDemo.java
1 package org.apache.lucene;
2
3 /**
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
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 java.io.IOException;
21
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.RandomIndexWriter;
27 import org.apache.lucene.queryParser.ParseException;
28 import org.apache.lucene.queryParser.QueryParser;
29 import org.apache.lucene.search.TopDocs;
30 import org.apache.lucene.search.IndexSearcher;
31 import org.apache.lucene.search.Query;
32 import org.apache.lucene.store.Directory;
33 import org.apache.lucene.util.LuceneTestCase;
34
35 /**
36  * A very simple demo used in the API documentation (src/java/overview.html).
37  *
38  * Please try to keep src/java/overview.html up-to-date when making changes
39  * to this class.
40  */
41 public class TestDemo extends LuceneTestCase {
42
43   public void testDemo() throws IOException, ParseException {
44     Analyzer analyzer = new MockAnalyzer(random);
45
46     // Store the index in memory:
47     Directory directory = newDirectory();
48     // To store an index on disk, use this instead:
49     //Directory directory = FSDirectory.open("/tmp/testindex");
50     RandomIndexWriter iwriter = new RandomIndexWriter(random, directory, analyzer);
51     iwriter.w.setInfoStream(VERBOSE ? System.out : null);
52     Document doc = new Document();
53     String text = "This is the text to be indexed.";
54     doc.add(newField("fieldname", text, Field.Store.YES,
55         Field.Index.ANALYZED));
56     iwriter.addDocument(doc);
57     iwriter.close();
58     
59     // Now search the index:
60     IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
61     // Parse a simple query that searches for "text":
62     QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "fieldname", analyzer);
63     Query query = parser.parse("text");
64     TopDocs hits = isearcher.search(query, null, 1);
65     assertEquals(1, hits.totalHits);
66     // Iterate through the results:
67     for (int i = 0; i < hits.scoreDocs.length; i++) {
68       Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
69       assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
70     }
71
72     // Test simple phrase query
73     query = parser.parse("\"to be\"");
74     assertEquals(1, isearcher.search(query, null, 1).totalHits);
75
76     isearcher.close();
77     directory.close();
78   }
79 }