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 final static int INDEX_SIZE = atLeast(2000);
46 * Create index and query for test cases.
49 public void setUp() throws Exception {
51 index = newDirectory();
52 RandomIndexWriter writer = new RandomIndexWriter(random, index);
53 RandomGen random = new RandomGen(this.random);
54 for (int i = 0; i < INDEX_SIZE; ++i) { // don't decrease; if to low the
55 // problem doesn't show up
56 Document doc = new Document();
57 if ((i % 5) != 0) { // some documents must not have an entry in the first
59 doc.add(newField("publicationDate_", random.getLuceneDate(),
60 Field.Store.YES, Field.Index.NOT_ANALYZED));
62 if ((i % 7) == 0) { // some documents to match the query (see below)
63 doc.add(newField("content", "test", Field.Store.YES,
64 Field.Index.ANALYZED));
66 // every document has a defined 'mandant' field
67 doc.add(newField("mandant", Integer.toString(i % 3), Field.Store.YES,
68 Field.Index.NOT_ANALYZED));
69 writer.addDocument(doc);
71 reader = writer.getReader();
73 query = new TermQuery(new Term("content", "test"));
77 public void tearDown() throws Exception {
84 * Run the test using two CustomSearcher instances.
86 public void testFieldSortCustomSearcher() throws Exception {
87 // log("Run testFieldSortCustomSearcher");
88 // define the sort criteria
89 Sort custSort = new Sort(
90 new SortField("publicationDate_", SortField.STRING),
91 SortField.FIELD_SCORE);
92 Searcher searcher = new CustomSearcher(reader, 2);
93 // search and check hits
94 matchHits(searcher, custSort);
98 * Run the test using one CustomSearcher wrapped by a MultiSearcher.
100 public void testFieldSortSingleSearcher() throws Exception {
101 // log("Run testFieldSortSingleSearcher");
102 // define the sort criteria
103 Sort custSort = new Sort(
104 new SortField("publicationDate_", SortField.STRING),
105 SortField.FIELD_SCORE);
106 Searcher searcher = new MultiSearcher(new Searcher[] {new CustomSearcher(
108 // search and check hits
109 matchHits(searcher, custSort);
113 * Run the test using two CustomSearcher instances.
115 public void testFieldSortMultiCustomSearcher() throws Exception {
116 // log("Run testFieldSortMultiCustomSearcher");
117 // define the sort criteria
118 Sort custSort = new Sort(
119 new SortField("publicationDate_", SortField.STRING),
120 SortField.FIELD_SCORE);
121 Searcher searcher = new MultiSearcher(new Searchable[] {
122 new CustomSearcher(reader, 0), new CustomSearcher(reader, 2)});
123 // search and check hits
124 matchHits(searcher, custSort);
127 // make sure the documents returned by the search match the expected list
128 private void matchHits(Searcher searcher, Sort sort) throws IOException {
129 // make a query without sorting first
130 ScoreDoc[] hitsByRank = searcher.search(query, null, Integer.MAX_VALUE).scoreDocs;
131 checkHits(hitsByRank, "Sort by rank: "); // check for duplicates
132 Map<Integer,Integer> resultMap = new TreeMap<Integer,Integer>();
133 // store hits in TreeMap - TreeMap does not allow duplicates; existing
134 // entries are silently overwritten
135 for (int hitid = 0; hitid < hitsByRank.length; ++hitid) {
136 resultMap.put(Integer.valueOf(hitsByRank[hitid].doc), // Key: Lucene
138 Integer.valueOf(hitid)); // Value: Hits-Objekt Index
141 // now make a query using the sort criteria
142 ScoreDoc[] resultSort = searcher.search(query, null, Integer.MAX_VALUE,
144 checkHits(resultSort, "Sort by custom criteria: "); // check for duplicates
146 // besides the sorting both sets of hits must be identical
147 for (int hitid = 0; hitid < resultSort.length; ++hitid) {
148 Integer idHitDate = Integer.valueOf(resultSort[hitid].doc); // document ID
151 if (!resultMap.containsKey(idHitDate)) {
152 log("ID " + idHitDate + " not found. Possibliy a duplicate.");
154 assertTrue(resultMap.containsKey(idHitDate)); // same ID must be in the
155 // Map from the rank-sorted
157 // every hit must appear once in both result sets --> remove it from the
159 // At the end the Map must be empty!
160 resultMap.remove(idHitDate);
162 if (resultMap.size() == 0) {
163 // log("All hits matched");
165 log("Couldn't match " + resultMap.size() + " hits.");
167 assertEquals(resultMap.size(), 0);
171 * Check the hits for duplicates.
175 private void checkHits(ScoreDoc[] hits, String prefix) {
177 Map<Integer,Integer> idMap = new TreeMap<Integer,Integer>();
178 for (int docnum = 0; docnum < hits.length; ++docnum) {
179 Integer luceneId = null;
181 luceneId = Integer.valueOf(hits[docnum].doc);
182 if (idMap.containsKey(luceneId)) {
183 StringBuilder message = new StringBuilder(prefix);
184 message.append("Duplicate key for hit index = ");
185 message.append(docnum);
186 message.append(", previous index = ");
187 message.append((idMap.get(luceneId)).toString());
188 message.append(", Lucene ID = ");
189 message.append(luceneId);
190 log(message.toString());
192 idMap.put(luceneId, Integer.valueOf(docnum));
198 // Simply write to console - choosen to be independant of log4j etc
199 private void log(String message) {
200 if (VERBOSE) System.out.println(message);
203 public class CustomSearcher extends IndexSearcher {
204 private int switcher;
209 public CustomSearcher(IndexReader r, int switcher) {
211 this.switcher = switcher;
218 * org.apache.lucene.search.Searchable#search(org.apache.lucene.search.Query
219 * , org.apache.lucene.search.Filter, int, org.apache.lucene.search.Sort)
222 public TopFieldDocs search(Query query, Filter filter, int nDocs, Sort sort)
224 BooleanQuery bq = new BooleanQuery();
225 bq.add(query, BooleanClause.Occur.MUST);
226 bq.add(new TermQuery(new Term("mandant", Integer.toString(switcher))),
227 BooleanClause.Occur.MUST);
228 return super.search(bq, filter, nDocs, sort);
235 * org.apache.lucene.search.Searchable#search(org.apache.lucene.search.Query
236 * , org.apache.lucene.search.Filter, int)
239 public TopDocs search(Query query, Filter filter, int nDocs)
241 BooleanQuery bq = new BooleanQuery();
242 bq.add(query, BooleanClause.Occur.MUST);
243 bq.add(new TermQuery(new Term("mandant", Integer.toString(switcher))),
244 BooleanClause.Occur.MUST);
245 return super.search(bq, filter, nDocs);
249 private class RandomGen {
250 RandomGen(Random random) {
251 this.random = random;
254 private Random random;
255 private Calendar base = new GregorianCalendar(1980, 1, 1);
257 // Just to generate some different Lucene Date strings
258 private String getLuceneDate() {
259 return DateTools.timeToString(base.getTimeInMillis() + random.nextInt()
260 - Integer.MIN_VALUE, DateTools.Resolution.DAY);