001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.Arrays;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.lucene.analysis.WhitespaceAnalyzer;
025: import org.apache.lucene.document.DateTools;
026: import org.apache.lucene.document.Document;
027: import org.apache.lucene.document.Field;
028: import org.apache.lucene.index.IndexWriter;
029: import org.apache.lucene.queryParser.QueryParser;
030: import org.apache.lucene.search.Hits;
031: import org.apache.lucene.search.IndexSearcher;
032: import org.apache.lucene.search.Query;
033: import org.apache.lucene.search.Sort;
034: import org.apache.lucene.search.SortField;
035: import org.apache.lucene.store.Directory;
036: import org.apache.lucene.store.RAMDirectory;
037:
038: /**
039: * Test date sorting, i.e. auto-sorting of fields with type "long".
040: * See http://issues.apache.org/jira/browse/LUCENE-1045
041: */
042: public class TestDateSort extends TestCase {
043:
044: private static final String TEXT_FIELD = "text";
045: private static final String DATE_TIME_FIELD = "dateTime";
046:
047: private static Directory directory;
048:
049: public void setUp() throws Exception {
050: // Create an index writer.
051: directory = new RAMDirectory();
052: IndexWriter writer = new IndexWriter(directory,
053: new WhitespaceAnalyzer(), true);
054:
055: // oldest doc:
056: // Add the first document. text = "Document 1" dateTime = Oct 10 03:25:22 EDT 2007
057: writer
058: .addDocument(createDocument("Document 1",
059: 1192001122000L));
060: // Add the second document. text = "Document 2" dateTime = Oct 10 03:25:26 EDT 2007
061: writer
062: .addDocument(createDocument("Document 2",
063: 1192001126000L));
064: // Add the third document. text = "Document 3" dateTime = Oct 11 07:12:13 EDT 2007
065: writer
066: .addDocument(createDocument("Document 3",
067: 1192101133000L));
068: // Add the fourth document. text = "Document 4" dateTime = Oct 11 08:02:09 EDT 2007
069: writer
070: .addDocument(createDocument("Document 4",
071: 1192104129000L));
072: // latest doc:
073: // Add the fifth document. text = "Document 5" dateTime = Oct 12 13:25:43 EDT 2007
074: writer
075: .addDocument(createDocument("Document 5",
076: 1192209943000L));
077:
078: writer.optimize();
079: writer.close();
080: }
081:
082: public void testReverseDateSort() throws Exception {
083: IndexSearcher searcher = new IndexSearcher(directory);
084:
085: // Create a Sort object. reverse is set to true.
086: // problem occurs only with SortField.AUTO:
087: Sort sort = new Sort(new SortField(DATE_TIME_FIELD,
088: SortField.AUTO, true));
089:
090: QueryParser queryParser = new QueryParser(TEXT_FIELD,
091: new WhitespaceAnalyzer());
092: Query query = queryParser.parse("Document");
093:
094: // Execute the search and process the search results.
095: String[] actualOrder = new String[5];
096: Hits hits = searcher.search(query, sort);
097: for (int i = 0; i < hits.length(); i++) {
098: Document document = hits.doc(i);
099: String text = document.get(TEXT_FIELD);
100: actualOrder[i] = text;
101: }
102: searcher.close();
103:
104: // Set up the expected order (i.e. Document 5, 4, 3, 2, 1).
105: String[] expectedOrder = new String[5];
106: expectedOrder[0] = "Document 5";
107: expectedOrder[1] = "Document 4";
108: expectedOrder[2] = "Document 3";
109: expectedOrder[3] = "Document 2";
110: expectedOrder[4] = "Document 1";
111:
112: assertEquals(Arrays.asList(expectedOrder), Arrays
113: .asList(actualOrder));
114: }
115:
116: private static Document createDocument(String text, long time) {
117: Document document = new Document();
118:
119: // Add the text field.
120: Field textField = new Field(TEXT_FIELD, text, Field.Store.YES,
121: Field.Index.TOKENIZED);
122: document.add(textField);
123:
124: // Add the date/time field.
125: String dateTimeString = DateTools.timeToString(time,
126: DateTools.Resolution.SECOND);
127: Field dateTimeField = new Field(DATE_TIME_FIELD,
128: dateTimeString, Field.Store.YES,
129: Field.Index.UN_TOKENIZED);
130: document.add(dateTimeField);
131:
132: return document;
133: }
134:
135: }
|