pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / TestDateSort.java
1 package org.apache.lucene.search;
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.util.Arrays;
21
22 import org.apache.lucene.util.LuceneTestCase;
23
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.analysis.WhitespaceAnalyzer;
26 import org.apache.lucene.document.DateTools;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.index.IndexReader;
30 import org.apache.lucene.index.RandomIndexWriter;
31 import org.apache.lucene.queryParser.QueryParser;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.search.Sort;
34 import org.apache.lucene.search.SortField;
35 import org.apache.lucene.store.Directory;
36
37 /**
38  * Test date sorting, i.e. auto-sorting of fields with type "long".
39  * See http://issues.apache.org/jira/browse/LUCENE-1045 
40  */
41 public class TestDateSort extends LuceneTestCase {
42
43   private static final String TEXT_FIELD = "text";
44   private static final String DATE_TIME_FIELD = "dateTime";
45
46   private Directory directory;
47   private IndexReader reader;
48
49   @Override
50   public void setUp() throws Exception {
51     super.setUp();
52     // Create an index writer.
53     directory = newDirectory();
54     RandomIndexWriter writer = new RandomIndexWriter(random, directory, new MockAnalyzer(random));
55
56     // oldest doc:
57     // Add the first document.  text = "Document 1"  dateTime = Oct 10 03:25:22 EDT 2007
58     writer.addDocument(createDocument("Document 1", 1192001122000L));
59     // Add the second document.  text = "Document 2"  dateTime = Oct 10 03:25:26 EDT 2007 
60     writer.addDocument(createDocument("Document 2", 1192001126000L));
61     // Add the third document.  text = "Document 3"  dateTime = Oct 11 07:12:13 EDT 2007 
62     writer.addDocument(createDocument("Document 3", 1192101133000L));
63     // Add the fourth document.  text = "Document 4"  dateTime = Oct 11 08:02:09 EDT 2007
64     writer.addDocument(createDocument("Document 4", 1192104129000L));
65     // latest doc:
66     // Add the fifth document.  text = "Document 5"  dateTime = Oct 12 13:25:43 EDT 2007
67     writer.addDocument(createDocument("Document 5", 1192209943000L));
68
69     reader = writer.getReader();
70     writer.close();
71   }
72
73   @Override
74   public void tearDown() throws Exception {
75     reader.close();
76     directory.close();
77     super.tearDown();
78   }
79
80   public void testReverseDateSort() throws Exception {
81     IndexSearcher searcher = newSearcher(reader);
82
83     Sort sort = new Sort(new SortField(DATE_TIME_FIELD, SortField.STRING, true));
84
85     QueryParser queryParser = new QueryParser(TEST_VERSION_CURRENT, TEXT_FIELD, new MockAnalyzer(random));
86     Query query = queryParser.parse("Document");
87
88     // Execute the search and process the search results.
89     String[] actualOrder = new String[5];
90     ScoreDoc[] hits = searcher.search(query, null, 1000, sort).scoreDocs;
91     for (int i = 0; i < hits.length; i++) {
92       Document document = searcher.doc(hits[i].doc);
93       String text = document.get(TEXT_FIELD);
94       actualOrder[i] = text;
95     }
96     searcher.close();
97
98     // Set up the expected order (i.e. Document 5, 4, 3, 2, 1).
99     String[] expectedOrder = new String[5];
100     expectedOrder[0] = "Document 5";
101     expectedOrder[1] = "Document 4";
102     expectedOrder[2] = "Document 3";
103     expectedOrder[3] = "Document 2";
104     expectedOrder[4] = "Document 1";
105
106     assertEquals(Arrays.asList(expectedOrder), Arrays.asList(actualOrder));
107   }
108
109   private Document createDocument(String text, long time) {
110     Document document = new Document();
111
112     // Add the text field.
113     Field textField = newField(TEXT_FIELD, text, Field.Store.YES, Field.Index.ANALYZED);
114     document.add(textField);
115
116     // Add the date/time field.
117     String dateTimeString = DateTools.timeToString(time, DateTools.Resolution.SECOND);
118     Field dateTimeField = newField(DATE_TIME_FIELD, dateTimeString, Field.Store.YES,
119         Field.Index.NOT_ANALYZED);
120     document.add(dateTimeField);
121
122     return document;
123   }
124
125 }