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.util.GregorianCalendar;
21 import java.util.Random;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
25 import org.apache.lucene.util.LuceneTestCase;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
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.*;
36 /** JUnit adaptation of an older test case SearchTest. */
37 public class TestSearch extends LuceneTestCase {
39 /** Main for running test case by itself. */
40 public static void main(String args[]) {
41 TestRunner.run (new TestSuite(TestSearch.class));
44 /** This test performs a number of searches. It also compares output
45 * of searches using multi-file index segments with single-file
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.
53 public void testSearch() throws Exception {
54 StringWriter sw = new StringWriter();
55 PrintWriter pw = new PrintWriter(sw, true);
56 doTestSearch(random, pw, false);
59 String multiFileOutput = sw.getBuffer().toString();
60 //System.out.println(multiFileOutput);
62 sw = new StringWriter();
63 pw = new PrintWriter(sw, true);
64 doTestSearch(random, pw, true);
67 String singleFileOutput = sw.getBuffer().toString();
69 assertEquals(multiFileOutput, singleFileOutput);
73 private void doTestSearch(Random random, PrintWriter out, boolean useCompoundFile)
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);
83 IndexWriter writer = new IndexWriter(directory, conf);
87 "a b c d e a b c d e",
88 "a b c d e f g h i j",
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);
102 Searcher searcher = new IndexSearcher(directory, true);
112 ScoreDoc[] hits = null;
114 Sort sort = new Sort(new SortField[] {
115 SortField.FIELD_SCORE,
116 new SortField("id", SortField.INT)});
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"));
124 hits = searcher.search(query, null, 1000, sort).scoreDocs;
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"));
138 static long Time(int year, int month, int day) {
139 GregorianCalendar calendar = new GregorianCalendar();
141 calendar.set(year, month, day);
142 return calendar.getTime().getTime();