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.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.Random;
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;
35 public class TestSearchForDuplicates extends LuceneTestCase {
37 /** Main for running test case by itself. */
38 public static void main(String args[]) {
39 TestRunner.run (new TestSuite(TestSearchForDuplicates.class));
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";
51 /** This test compares search results when using and not using compound
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.
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);
66 String multiFileOutput = sw.getBuffer().toString();
67 //System.out.println(multiFileOutput);
69 sw = new StringWriter();
70 pw = new PrintWriter(sw, true);
71 doTest(random, pw, true, MAX_DOCS);
74 String singleFileOutput = sw.getBuffer().toString();
76 assertEquals(multiFileOutput, singleFileOutput);
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);
88 IndexWriter writer = new IndexWriter(directory, conf);
90 System.out.println("TEST: now build index");
91 writer.setInfoStream(System.out);
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);
102 // try a search without OR
103 Searcher searcher = new IndexSearcher(directory, true);
105 QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, PRIORITY_FIELD, analyzer);
107 Query query = parser.parse(HIGH_PRIORITY);
108 out.println("Query: " + query.toString(PRIORITY_FIELD));
110 final Sort sort = new Sort(new SortField[] {
111 SortField.FIELD_SCORE,
112 new SortField(ID_FIELD, SortField.INT)});
114 ScoreDoc[] hits = searcher.search(query, null, MAX_DOCS, sort).scoreDocs;
115 printHits(out, hits, searcher);
116 checkHits(hits, MAX_DOCS, searcher);
120 // try a new search with OR
121 searcher = new IndexSearcher(directory, true);
124 parser = new QueryParser(TEST_VERSION_CURRENT, PRIORITY_FIELD, analyzer);
126 query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
127 out.println("Query: " + query.toString(PRIORITY_FIELD));
129 hits = searcher.search(query, null, MAX_DOCS, sort).scoreDocs;
130 printHits(out, hits, searcher);
131 checkHits(hits, MAX_DOCS, searcher);
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));
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));