001: package org.apache.lucene;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.GregorianCalendar;
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023:
024: import org.apache.lucene.util.LuceneTestCase;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: import org.apache.lucene.store.*;
029: import org.apache.lucene.document.*;
030: import org.apache.lucene.analysis.*;
031: import org.apache.lucene.index.*;
032: import org.apache.lucene.search.*;
033: import org.apache.lucene.queryParser.*;
034:
035: /** JUnit adaptation of an older test case SearchTest.
036: *
037: * @version $Id: TestSearch.java 583534 2007-10-10 16:46:35Z mikemccand $
038: */
039: public class TestSearch extends LuceneTestCase {
040:
041: /** Main for running test case by itself. */
042: public static void main(String args[]) {
043: TestRunner.run(new TestSuite(TestSearch.class));
044: }
045:
046: /** This test performs a number of searches. It also compares output
047: * of searches using multi-file index segments with single-file
048: * index segments.
049: *
050: * TODO: someone should check that the results of the searches are
051: * still correct by adding assert statements. Right now, the test
052: * passes if the results are the same between multi-file and
053: * single-file formats, even if the results are wrong.
054: */
055: public void testSearch() throws Exception {
056: StringWriter sw = new StringWriter();
057: PrintWriter pw = new PrintWriter(sw, true);
058: doTestSearch(pw, false);
059: pw.close();
060: sw.close();
061: String multiFileOutput = sw.getBuffer().toString();
062: //System.out.println(multiFileOutput);
063:
064: sw = new StringWriter();
065: pw = new PrintWriter(sw, true);
066: doTestSearch(pw, true);
067: pw.close();
068: sw.close();
069: String singleFileOutput = sw.getBuffer().toString();
070:
071: assertEquals(multiFileOutput, singleFileOutput);
072: }
073:
074: private void doTestSearch(PrintWriter out, boolean useCompoundFile)
075: throws Exception {
076: Directory directory = new RAMDirectory();
077: Analyzer analyzer = new SimpleAnalyzer();
078: IndexWriter writer = new IndexWriter(directory, analyzer, true);
079:
080: writer.setUseCompoundFile(useCompoundFile);
081:
082: String[] docs = { "a b c d e", "a b c d e a b c d e",
083: "a b c d e f g h i j", "a c e", "e c a", "a c e a c e",
084: "a c e a b c" };
085: for (int j = 0; j < docs.length; j++) {
086: Document d = new Document();
087: d.add(new Field("contents", docs[j], Field.Store.YES,
088: Field.Index.TOKENIZED));
089: writer.addDocument(d);
090: }
091: writer.close();
092:
093: Searcher searcher = new IndexSearcher(directory);
094:
095: String[] queries = { "a b", "\"a b\"", "\"a b c\"", "a c",
096: "\"a c\"", "\"a c e\"", };
097: Hits hits = null;
098:
099: QueryParser parser = new QueryParser("contents", analyzer);
100: parser.setPhraseSlop(4);
101: for (int j = 0; j < queries.length; j++) {
102: Query query = parser.parse(queries[j]);
103: out.println("Query: " + query.toString("contents"));
104:
105: //DateFilter filter =
106: // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
107: //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
108: //System.out.println(filter);
109:
110: hits = searcher.search(query);
111:
112: out.println(hits.length() + " total results");
113: for (int i = 0; i < hits.length() && i < 10; i++) {
114: Document d = hits.doc(i);
115: out.println(i + " " + hits.score(i)
116: // + " " + DateField.stringToDate(d.get("modified"))
117: + " " + d.get("contents"));
118: }
119: }
120: searcher.close();
121: }
122:
123: static long Time(int year, int month, int day) {
124: GregorianCalendar calendar = new GregorianCalendar();
125: calendar.set(year, month, day);
126: return calendar.getTime().getTime();
127: }
128: }
|