add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestCustomSearcherSort.java
1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2005 The Apache Software Foundation
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import java.io.IOException;
20 import java.io.Serializable;
21 import java.util.Calendar;
22 import java.util.GregorianCalendar;
23 import java.util.Map;
24 import java.util.Random;
25 import java.util.TreeMap;
26
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;
35
36 /** Unit test for sorting code. */
37 public class TestCustomSearcherSort extends LuceneTestCase implements Serializable {
38   
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);
44   
45   /**
46    * Create index and query for test cases.
47    */
48   @Override
49   public void setUp() throws Exception {
50     super.setUp();
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
58                           // sort field
59         doc.add(newField("publicationDate_", random.getLuceneDate(),
60             Field.Store.YES, Field.Index.NOT_ANALYZED));
61       }
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));
65       }
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);
70     }
71     reader = writer.getReader();
72     writer.close();
73     query = new TermQuery(new Term("content", "test"));
74   }
75   
76   @Override
77   public void tearDown() throws Exception {
78     reader.close();
79     index.close();
80     super.tearDown();
81   }
82   
83   /**
84    * Run the test using two CustomSearcher instances.
85    */
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);
95   }
96   
97   /**
98    * Run the test using one CustomSearcher wrapped by a MultiSearcher.
99    */
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(
107         reader, 2)});
108     // search and check hits
109     matchHits(searcher, custSort);
110   }
111   
112   /**
113    * Run the test using two CustomSearcher instances.
114    */
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);
125   }
126   
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
137                                                             // Document ID
138           Integer.valueOf(hitid)); // Value: Hits-Objekt Index
139     }
140     
141     // now make a query using the sort criteria
142     ScoreDoc[] resultSort = searcher.search(query, null, Integer.MAX_VALUE,
143         sort).scoreDocs;
144     checkHits(resultSort, "Sort by custom criteria: "); // check for duplicates
145     
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
149                                                                   // from sorted
150                                                                   // search
151       if (!resultMap.containsKey(idHitDate)) {
152         log("ID " + idHitDate + " not found. Possibliy a duplicate.");
153       }
154       assertTrue(resultMap.containsKey(idHitDate)); // same ID must be in the
155                                                     // Map from the rank-sorted
156                                                     // search
157       // every hit must appear once in both result sets --> remove it from the
158       // Map.
159       // At the end the Map must be empty!
160       resultMap.remove(idHitDate);
161     }
162     if (resultMap.size() == 0) {
163       // log("All hits matched");
164     } else {
165       log("Couldn't match " + resultMap.size() + " hits.");
166     }
167     assertEquals(resultMap.size(), 0);
168   }
169   
170   /**
171    * Check the hits for duplicates.
172    * 
173    * @param hits
174    */
175   private void checkHits(ScoreDoc[] hits, String prefix) {
176     if (hits != null) {
177       Map<Integer,Integer> idMap = new TreeMap<Integer,Integer>();
178       for (int docnum = 0; docnum < hits.length; ++docnum) {
179         Integer luceneId = null;
180         
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());
191         } else {
192           idMap.put(luceneId, Integer.valueOf(docnum));
193         }
194       }
195     }
196   }
197   
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);
201   }
202   
203   public class CustomSearcher extends IndexSearcher {
204     private int switcher;
205     
206     /**
207      * @param r
208      */
209     public CustomSearcher(IndexReader r, int switcher) {
210       super(r);
211       this.switcher = switcher;
212     }
213     
214     /*
215      * (non-Javadoc)
216      * 
217      * @see
218      * org.apache.lucene.search.Searchable#search(org.apache.lucene.search.Query
219      * , org.apache.lucene.search.Filter, int, org.apache.lucene.search.Sort)
220      */
221     @Override
222     public TopFieldDocs search(Query query, Filter filter, int nDocs, Sort sort)
223         throws IOException {
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);
229     }
230     
231     /*
232      * (non-Javadoc)
233      * 
234      * @see
235      * org.apache.lucene.search.Searchable#search(org.apache.lucene.search.Query
236      * , org.apache.lucene.search.Filter, int)
237      */
238     @Override
239     public TopDocs search(Query query, Filter filter, int nDocs)
240         throws IOException {
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);
246     }
247   }
248   
249   private class RandomGen {
250     RandomGen(Random random) {
251       this.random = random;
252     }
253     
254     private Random random;
255     private Calendar base = new GregorianCalendar(1980, 1, 1);
256     
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);
261     }
262   }
263 }