1
package org.apache.lucene.search;
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.util.Arrays;
22
import org.apache.lucene.util.LuceneTestCase;
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;
38
* Test date sorting, i.e. auto-sorting of fields with type "long".
39
* See http://issues.apache.org/jira/browse/LUCENE-1045
41
public class TestDateSort extends LuceneTestCase {
43
private static final String TEXT_FIELD = "text";
44
private static final String DATE_TIME_FIELD = "dateTime";
46
private Directory directory;
47
private IndexReader reader;
50
public void setUp() throws Exception {
52
// Create an index writer.
53
directory = newDirectory();
54
RandomIndexWriter writer = new RandomIndexWriter(random, directory, new MockAnalyzer(random));
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));
66
// Add the fifth document. text = "Document 5" dateTime = Oct 12 13:25:43 EDT 2007
67
writer.addDocument(createDocument("Document 5", 1192209943000L));
69
reader = writer.getReader();
74
public void tearDown() throws Exception {
80
public void testReverseDateSort() throws Exception {
81
IndexSearcher searcher = newSearcher(reader);
83
Sort sort = new Sort(new SortField(DATE_TIME_FIELD, SortField.STRING, true));
85
QueryParser queryParser = new QueryParser(TEST_VERSION_CURRENT, TEXT_FIELD, new MockAnalyzer(random));
86
Query query = queryParser.parse("Document");
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;
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";
106
assertEquals(Arrays.asList(expectedOrder), Arrays.asList(actualOrder));
109
private Document createDocument(String text, long time) {
110
Document document = new Document();
112
// Add the text field.
113
Field textField = newField(TEXT_FIELD, text, Field.Store.YES, Field.Index.ANALYZED);
114
document.add(textField);
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);