1 package org.apache.lucene.search;
4 * Copyright 2005 The Apache Software Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.io.IOException;
20 import java.io.Serializable;
21 import java.util.Calendar;
22 import java.util.GregorianCalendar;
24 import java.util.Random;
25 import java.util.TreeMap;
27 import org.apache.lucene.document.DateTools;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.index.IndexReader;
31 import org.apache.lucene.index.RandomIndexWriter;
32 import org.apache.lucene.index.Term;
33 import org.apache.lucene.store.Directory;
34 import org.apache.lucene.util.LuceneTestCase;
36 /** Unit test for sorting code. */
37 public class TestCustomSearcherSort extends LuceneTestCase implements Serializable {
39 private Directory index = null;
40 private IndexReader reader;
41 private Query query = null;
42 // reduced from 20000 to 2000 to speed up test...
43 private int INDEX_SIZE;
46 * Create index and query for test cases.
49 public void setUp() throws Exception {
51 INDEX_SIZE = atLeast(2000);
52 index = newDirectory();
53 RandomIndexWriter writer = new RandomIndexWriter(random, index);
54 RandomGen random = new RandomGen(this.random);
55 for (int i = 0; i < INDEX_SIZE; ++i) { // don't decrease; if to low the
56 // problem doesn't show up
57 Document doc = new Document();
58 if ((i % 5) != 0) { // some documents must not have an entry in the first
60 doc.add(newField("publicationDate_", random.getLuceneDate(),
61 Field.Store.YES, Field.Index.NOT_ANALYZED));
63 if ((i % 7) == 0) { // some documents to match the query (see below)
64 doc.add(newField("content", "test", Field.Store.YES,
65 Field.Index.ANALYZED));
67 // every document has a defined 'mandant' field
68 doc.add(newField("mandant", Integer.toString(i % 3), Field.Store.YES,
69 Field.Index.NOT_ANALYZED));
70 writer.addDocument(doc);
72 reader = writer.getReader();
74 query = new TermQuery(new Term("content", "test"));
78 public void tearDown() throws Exception {
85 * Run the test using two CustomSearcher instances.
87 public void testFieldSortCustomSearcher() throws Exception {
88 // log("Run testFieldSortCustomSearcher");
89 // define the sort criteria
90 Sort custSort = new Sort(
91 new SortField("publicationDate_", SortField.STRING),
92 SortField.FIELD_SCORE);
93 Searcher searcher = new CustomSearcher(reader, 2);
94 // search and check hits
95 matchHits(searcher, custSort);
99 * Run the test using one CustomSearcher wrapped by a MultiSearcher.
101 public void testFieldSortSingleSearcher() throws Exception {
102 // log("Run testFieldSortSingleSearcher");
103 // define the sort criteria
104 Sort custSort = new Sort(
105 new SortField("publicationDate_", SortField.STRING),
106 SortField.FIELD_SCORE);
107 Searcher searcher = new MultiSearcher(new Searcher[] {new CustomSearcher(
109 // search and check hits
110 matchHits(searcher, custSort);
114 * Run the test using two CustomSearcher instances.
116 public void testFieldSortMultiCustomSearcher() throws Exception {
117 // log("Run testFieldSortMultiCustomSearcher");
118 // define the sort criteria
119 Sort custSort = new Sort(
120 new SortField("publicationDate_", SortField.STRING),
121 SortField.FIELD_SCORE);
122 Searcher searcher = new MultiSearcher(new Searchable[] {
123 new CustomSearcher(reader, 0), new CustomSearcher(reader, 2)});
124 // search and check hits
125 matchHits(searcher, custSort);
128 // make sure the documents returned by the search match the expected list
129 private void matchHits(Searcher searcher, Sort sort) throws IOException {
130 // make a query without sorting first
131 ScoreDoc[] hitsByRank = searcher.search(query, null, Integer.MAX_VALUE).scoreDocs;
132 checkHits(hitsByRank, "Sort by rank: "); // check for duplicates
133 Map<Integer,Integer> resultMap = new TreeMap<Integer,Integer>();
134 // store hits in TreeMap - TreeMap does not allow duplicates; existing
135 // entries are silently overwritten
136 for (int hitid = 0; hitid < hitsByRank.length; ++hitid) {
137 resultMap.put(Integer.valueOf(hitsByRank[hitid].doc), // Key: Lucene
139 Integer.valueOf(hitid)); // Value: Hits-Objekt Index
142 // now make a query using the sort criteria
143 ScoreDoc[] resultSort = searcher.search(query, null, Integer.MAX_VALUE,
145 checkHits(resultSort, "Sort by custom criteria: "); // check for duplicates
147 // besides the sorting both sets of hits must be identical
148 for (int hitid = 0; hitid < resultSort.length; ++hitid) {
149 Integer idHitDate = Integer.valueOf(resultSort[hitid].doc); // document ID
152 if (!resultMap.containsKey(idHitDate)) {
153 log("ID " + idHitDate + " not found. Possibliy a duplicate.");
155 assertTrue(resultMap.containsKey(idHitDate)); // same ID must be in the
156 // Map from the rank-sorted
158 // every hit must appear once in both result sets --> remove it from the
160 // At the end the Map must be empty!
161 resultMap.remove(idHitDate);
163 if (resultMap.size() == 0) {
164 // log("All hits matched");
166 log("Couldn't match " + resultMap.size() + " hits.");
168 assertEquals(resultMap.size(), 0);
172 * Check the hits for duplicates.
176 private void checkHits(ScoreDoc[] hits, String prefix) {
178 Map<Integer,Integer> idMap = new TreeMap<Integer,Integer>();
179 for (int docnum = 0; docnum < hits.length; ++docnum) {
180 Integer luceneId = null;
182 luceneId = Integer.valueOf(hits[docnum].doc);
183 if (idMap.containsKey(luceneId)) {
184 StringBuilder message = new StringBuilder(prefix);
185 message.append("Duplicate key for hit index = ");
186 message.append(docnum);
187 message.append(", previous index = ");
188 message.append((idMap.get(luceneId)).toString());
189 message.append(", Lucene ID = ");
190 message.append(luceneId);
191 log(message.toString());
193 idMap.put(luceneId, Integer.valueOf(docnum));
199 // Simply write to console - choosen to be independant of log4j etc
200 private void log(String message) {
201 if (VERBOSE) System.out.println(message);
204 public class CustomSearcher extends IndexSearcher {
205 private int switcher;
210 public CustomSearcher(IndexReader r, int switcher) {
212 this.switcher = switcher;
219 * org.apache.lucene.search.Searchable#search(org.apache.lucene.search.Query
220 * , org.apache.lucene.search.Filter, int, org.apache.lucene.search.Sort)
223 public TopFieldDocs search(Query query, Filter filter, int nDocs, Sort sort)
225 BooleanQuery bq = new BooleanQuery();
226 bq.add(query, BooleanClause.Occur.MUST);
227 bq.add(new TermQuery(new Term("mandant", Integer.toString(switcher))),
228 BooleanClause.Occur.MUST);
229 return super.search(bq, filter, nDocs, sort);
236 * org.apache.lucene.search.Searchable#search(org.apache.lucene.search.Query
237 * , org.apache.lucene.search.Filter, int)
240 public TopDocs search(Query query, Filter filter, int nDocs)
242 BooleanQuery bq = new BooleanQuery();
243 bq.add(query, BooleanClause.Occur.MUST);
244 bq.add(new TermQuery(new Term("mandant", Integer.toString(switcher))),
245 BooleanClause.Occur.MUST);
246 return super.search(bq, filter, nDocs);
250 private class RandomGen {
251 RandomGen(Random random) {
252 this.random = random;
255 private Random random;
256 private Calendar base = new GregorianCalendar(1980, 1, 1);
258 // Just to generate some different Lucene Date strings
259 private String getLuceneDate() {
260 return DateTools.timeToString(base.getTimeInMillis() + random.nextInt()
261 - Integer.MIN_VALUE, DateTools.Resolution.DAY);