add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / TestSearchForDuplicates.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 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.Random;
24
25 import org.apache.lucene.store.*;
26 import org.apache.lucene.document.*;
27 import org.apache.lucene.analysis.*;
28 import org.apache.lucene.index.*;
29 import org.apache.lucene.search.*;
30 import org.apache.lucene.queryParser.*;
31 import org.apache.lucene.util.LuceneTestCase;
32 import junit.framework.TestSuite;
33 import junit.textui.TestRunner;
34
35 public class TestSearchForDuplicates extends LuceneTestCase {
36
37     /** Main for running test case by itself. */
38     public static void main(String args[]) {
39         TestRunner.run (new TestSuite(TestSearchForDuplicates.class));
40     }
41
42
43
44   static final String PRIORITY_FIELD ="priority";
45   static final String ID_FIELD ="id";
46   static final String HIGH_PRIORITY ="high";
47   static final String MED_PRIORITY ="medium";
48   static final String LOW_PRIORITY ="low";
49
50
51   /** This test compares search results when using and not using compound
52    *  files.
53    *
54    *  TODO: There is rudimentary search result validation as well, but it is
55    *        simply based on asserting the output observed in the old test case,
56    *        without really knowing if the output is correct. Someone needs to
57    *        validate this output and make any changes to the checkHits method.
58    */
59   public void testRun() throws Exception {
60       StringWriter sw = new StringWriter();
61       PrintWriter pw = new PrintWriter(sw, true);
62       final int MAX_DOCS = atLeast(225);
63       doTest(random, pw, false, MAX_DOCS);
64       pw.close();
65       sw.close();
66       String multiFileOutput = sw.getBuffer().toString();
67       //System.out.println(multiFileOutput);
68
69       sw = new StringWriter();
70       pw = new PrintWriter(sw, true);
71       doTest(random, pw, true, MAX_DOCS);
72       pw.close();
73       sw.close();
74       String singleFileOutput = sw.getBuffer().toString();
75
76       assertEquals(multiFileOutput, singleFileOutput);
77   }
78
79
80   private void doTest(Random random, PrintWriter out, boolean useCompoundFiles, int MAX_DOCS) throws Exception {
81       Directory directory = newDirectory();
82       Analyzer analyzer = new MockAnalyzer(random);
83       IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
84       final MergePolicy mp = conf.getMergePolicy();
85       if (mp instanceof LogMergePolicy) {
86         ((LogMergePolicy) mp).setUseCompoundFile(useCompoundFiles);
87       }
88       IndexWriter writer = new IndexWriter(directory, conf);
89       if (VERBOSE) {
90         System.out.println("TEST: now build index");
91         writer.setInfoStream(System.out);
92       }
93
94       for (int j = 0; j < MAX_DOCS; j++) {
95         Document d = new Document();
96         d.add(newField(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES, Field.Index.ANALYZED));
97         d.add(newField(ID_FIELD, Integer.toString(j), Field.Store.YES, Field.Index.ANALYZED));
98         writer.addDocument(d);
99       }
100       writer.close();
101
102       // try a search without OR
103       Searcher searcher = new IndexSearcher(directory, true);
104
105       QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, PRIORITY_FIELD, analyzer);
106
107       Query query = parser.parse(HIGH_PRIORITY);
108       out.println("Query: " + query.toString(PRIORITY_FIELD));
109
110       final Sort sort = new Sort(new SortField[] {
111           SortField.FIELD_SCORE,
112           new SortField(ID_FIELD, SortField.INT)});
113
114       ScoreDoc[] hits = searcher.search(query, null, MAX_DOCS, sort).scoreDocs;
115       printHits(out, hits, searcher);
116       checkHits(hits, MAX_DOCS, searcher);
117
118       searcher.close();
119
120       // try a new search with OR
121       searcher = new IndexSearcher(directory, true);
122       hits = null;
123
124       parser = new QueryParser(TEST_VERSION_CURRENT, PRIORITY_FIELD, analyzer);
125
126       query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
127       out.println("Query: " + query.toString(PRIORITY_FIELD));
128
129       hits = searcher.search(query, null, MAX_DOCS, sort).scoreDocs;
130       printHits(out, hits, searcher);
131       checkHits(hits, MAX_DOCS, searcher);
132
133       searcher.close();
134       directory.close();
135   }
136
137
138   private void printHits(PrintWriter out, ScoreDoc[] hits, Searcher searcher ) throws IOException {
139     out.println(hits.length + " total results\n");
140     for (int i = 0 ; i < hits.length; i++) {
141       if ( i < 10 || (i > 94 && i < 105) ) {
142         Document d = searcher.doc(hits[i].doc);
143         out.println(i + " " + d.get(ID_FIELD));
144       }
145     }
146   }
147
148   private void checkHits(ScoreDoc[] hits, int expectedCount, Searcher searcher) throws IOException {
149     assertEquals("total results", expectedCount, hits.length);
150     for (int i = 0 ; i < hits.length; i++) {
151       if (i < 10 || (i > 94 && i < 105) ) {
152       Document d = searcher.doc(hits[i].doc);
153         assertEquals("check " + i, String.valueOf(i), d.get(ID_FIELD));
154       }
155     }
156   }
157
158 }