add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / TestSearch.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.util.GregorianCalendar;
21 import java.util.Random;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24
25 import org.apache.lucene.util.LuceneTestCase;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28
29 import org.apache.lucene.store.*;
30 import org.apache.lucene.document.*;
31 import org.apache.lucene.analysis.*;
32 import org.apache.lucene.index.*;
33 import org.apache.lucene.search.*;
34 import org.apache.lucene.queryParser.*;
35
36 /** JUnit adaptation of an older test case SearchTest. */
37 public class TestSearch extends LuceneTestCase {
38
39     /** Main for running test case by itself. */
40     public static void main(String args[]) {
41         TestRunner.run (new TestSuite(TestSearch.class));
42     }
43
44     /** This test performs a number of searches. It also compares output
45      *  of searches using multi-file index segments with single-file
46      *  index segments.
47      *
48      *  TODO: someone should check that the results of the searches are
49      *        still correct by adding assert statements. Right now, the test
50      *        passes if the results are the same between multi-file and
51      *        single-file formats, even if the results are wrong.
52      */
53     public void testSearch() throws Exception {
54       StringWriter sw = new StringWriter();
55       PrintWriter pw = new PrintWriter(sw, true);
56       doTestSearch(random, pw, false);
57       pw.close();
58       sw.close();
59       String multiFileOutput = sw.getBuffer().toString();
60       //System.out.println(multiFileOutput);
61
62       sw = new StringWriter();
63       pw = new PrintWriter(sw, true);
64       doTestSearch(random, pw, true);
65       pw.close();
66       sw.close();
67       String singleFileOutput = sw.getBuffer().toString();
68
69       assertEquals(multiFileOutput, singleFileOutput);
70     }
71
72
73     private void doTestSearch(Random random, PrintWriter out, boolean useCompoundFile)
74     throws Exception {
75       Directory directory = newDirectory();
76       Analyzer analyzer = new MockAnalyzer(random);
77       IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
78       MergePolicy mp = conf.getMergePolicy();
79       if (mp instanceof LogMergePolicy) {
80         ((LogMergePolicy) mp).setUseCompoundFile(useCompoundFile);
81       }
82       
83       IndexWriter writer = new IndexWriter(directory, conf);
84
85       String[] docs = {
86         "a b c d e",
87         "a b c d e a b c d e",
88         "a b c d e f g h i j",
89         "a c e",
90         "e c a",
91         "a c e a c e",
92         "a c e a b c"
93       };
94       for (int j = 0; j < docs.length; j++) {
95         Document d = new Document();
96         d.add(newField("contents", docs[j], Field.Store.YES, Field.Index.ANALYZED));
97         d.add(newField("id", ""+j, Field.Index.NOT_ANALYZED_NO_NORMS));
98         writer.addDocument(d);
99       }
100       writer.close();
101
102       Searcher searcher = new IndexSearcher(directory, true);
103
104       String[] queries = {
105         "a b",
106         "\"a b\"",
107         "\"a b c\"",
108         "a c",
109         "\"a c\"",
110         "\"a c e\"",
111       };
112       ScoreDoc[] hits = null;
113
114       Sort sort = new Sort(new SortField[] {
115           SortField.FIELD_SCORE,
116           new SortField("id", SortField.INT)});
117
118       QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "contents", analyzer);
119       parser.setPhraseSlop(4);
120       for (int j = 0; j < queries.length; j++) {
121         Query query = parser.parse(queries[j]);
122         out.println("Query: " + query.toString("contents"));
123
124         hits = searcher.search(query, null, 1000, sort).scoreDocs;
125
126         out.println(hits.length + " total results");
127         for (int i = 0 ; i < hits.length && i < 10; i++) {
128           Document d = searcher.doc(hits[i].doc);
129           out.println(i + " " + hits[i].score
130 //                         + " " + DateField.stringToDate(d.get("modified"))
131                              + " " + d.get("contents"));
132         }
133       }
134       searcher.close();
135       directory.close();
136   }
137
138   static long Time(int year, int month, int day) {
139     GregorianCalendar calendar = new GregorianCalendar();
140     calendar.clear();
141     calendar.set(year, month, day);
142     return calendar.getTime().getTime();
143   }
144 }