~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestDateFilter.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 org.apache.lucene.store.Directory;
21
 
import org.apache.lucene.util.LuceneTestCase;
22
 
 
23
 
import org.apache.lucene.document.DateTools;
24
 
import org.apache.lucene.document.Document;
25
 
import org.apache.lucene.document.Field;
26
 
import org.apache.lucene.index.IndexReader;
27
 
import org.apache.lucene.index.RandomIndexWriter;
28
 
import org.apache.lucene.index.Term;
29
 
 
30
 
import java.io.IOException;
31
 
 
32
 
/**
33
 
 * DateFilter JUnit tests.
34
 
 * 
35
 
 * 
36
 
 * @version $Revision: 1066722 $
37
 
 */
38
 
public class TestDateFilter extends LuceneTestCase {
39
 
 
40
 
  /**
41
 
   *
42
 
   */
43
 
  public void testBefore() throws IOException {
44
 
    // create an index
45
 
    Directory indexStore = newDirectory();
46
 
    RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
47
 
    
48
 
    long now = System.currentTimeMillis();
49
 
    
50
 
    Document doc = new Document();
51
 
    // add time that is in the past
52
 
    doc.add(newField("datefield", DateTools.timeToString(now - 1000,
53
 
        DateTools.Resolution.MILLISECOND), Field.Store.YES,
54
 
        Field.Index.NOT_ANALYZED));
55
 
    doc.add(newField("body", "Today is a very sunny day in New York City",
56
 
        Field.Store.YES, Field.Index.ANALYZED));
57
 
    writer.addDocument(doc);
58
 
    
59
 
    IndexReader reader = writer.getReader();
60
 
    writer.close();
61
 
    IndexSearcher searcher = newSearcher(reader);
62
 
    
63
 
    // filter that should preserve matches
64
 
    // DateFilter df1 = DateFilter.Before("datefield", now);
65
 
    TermRangeFilter df1 = new TermRangeFilter("datefield", DateTools
66
 
        .timeToString(now - 2000, DateTools.Resolution.MILLISECOND), DateTools
67
 
        .timeToString(now, DateTools.Resolution.MILLISECOND), false, true);
68
 
    // filter that should discard matches
69
 
    // DateFilter df2 = DateFilter.Before("datefield", now - 999999);
70
 
    TermRangeFilter df2 = new TermRangeFilter("datefield", DateTools
71
 
        .timeToString(0, DateTools.Resolution.MILLISECOND), DateTools
72
 
        .timeToString(now - 2000, DateTools.Resolution.MILLISECOND), true,
73
 
        false);
74
 
    
75
 
    // search something that doesn't exist with DateFilter
76
 
    Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
77
 
    
78
 
    // search for something that does exists
79
 
    Query query2 = new TermQuery(new Term("body", "sunny"));
80
 
    
81
 
    ScoreDoc[] result;
82
 
    
83
 
    // ensure that queries return expected results without DateFilter first
84
 
    result = searcher.search(query1, null, 1000).scoreDocs;
85
 
    assertEquals(0, result.length);
86
 
    
87
 
    result = searcher.search(query2, null, 1000).scoreDocs;
88
 
    assertEquals(1, result.length);
89
 
    
90
 
    // run queries with DateFilter
91
 
    result = searcher.search(query1, df1, 1000).scoreDocs;
92
 
    assertEquals(0, result.length);
93
 
    
94
 
    result = searcher.search(query1, df2, 1000).scoreDocs;
95
 
    assertEquals(0, result.length);
96
 
    
97
 
    result = searcher.search(query2, df1, 1000).scoreDocs;
98
 
    assertEquals(1, result.length);
99
 
    
100
 
    result = searcher.search(query2, df2, 1000).scoreDocs;
101
 
    assertEquals(0, result.length);
102
 
    searcher.close();
103
 
    reader.close();
104
 
    indexStore.close();
105
 
  }
106
 
  
107
 
  /**
108
 
   *
109
 
   */
110
 
  public void testAfter() throws IOException {
111
 
    // create an index
112
 
    Directory indexStore = newDirectory();
113
 
    RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
114
 
    
115
 
    long now = System.currentTimeMillis();
116
 
    
117
 
    Document doc = new Document();
118
 
    // add time that is in the future
119
 
    doc.add(newField("datefield", DateTools.timeToString(now + 888888,
120
 
        DateTools.Resolution.MILLISECOND), Field.Store.YES,
121
 
        Field.Index.NOT_ANALYZED));
122
 
    doc.add(newField("body", "Today is a very sunny day in New York City",
123
 
        Field.Store.YES, Field.Index.ANALYZED));
124
 
    writer.addDocument(doc);
125
 
    
126
 
    IndexReader reader = writer.getReader();
127
 
    writer.close();
128
 
    IndexSearcher searcher = newSearcher(reader);
129
 
    
130
 
    // filter that should preserve matches
131
 
    // DateFilter df1 = DateFilter.After("datefield", now);
132
 
    TermRangeFilter df1 = new TermRangeFilter("datefield", DateTools
133
 
        .timeToString(now, DateTools.Resolution.MILLISECOND), DateTools
134
 
        .timeToString(now + 999999, DateTools.Resolution.MILLISECOND), true,
135
 
        false);
136
 
    // filter that should discard matches
137
 
    // DateFilter df2 = DateFilter.After("datefield", now + 999999);
138
 
    TermRangeFilter df2 = new TermRangeFilter("datefield", DateTools
139
 
        .timeToString(now + 999999, DateTools.Resolution.MILLISECOND),
140
 
        DateTools.timeToString(now + 999999999,
141
 
            DateTools.Resolution.MILLISECOND), false, true);
142
 
    
143
 
    // search something that doesn't exist with DateFilter
144
 
    Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
145
 
    
146
 
    // search for something that does exists
147
 
    Query query2 = new TermQuery(new Term("body", "sunny"));
148
 
    
149
 
    ScoreDoc[] result;
150
 
    
151
 
    // ensure that queries return expected results without DateFilter first
152
 
    result = searcher.search(query1, null, 1000).scoreDocs;
153
 
    assertEquals(0, result.length);
154
 
    
155
 
    result = searcher.search(query2, null, 1000).scoreDocs;
156
 
    assertEquals(1, result.length);
157
 
    
158
 
    // run queries with DateFilter
159
 
    result = searcher.search(query1, df1, 1000).scoreDocs;
160
 
    assertEquals(0, result.length);
161
 
    
162
 
    result = searcher.search(query1, df2, 1000).scoreDocs;
163
 
    assertEquals(0, result.length);
164
 
    
165
 
    result = searcher.search(query2, df1, 1000).scoreDocs;
166
 
    assertEquals(1, result.length);
167
 
    
168
 
    result = searcher.search(query2, df2, 1000).scoreDocs;
169
 
    assertEquals(0, result.length);
170
 
    searcher.close();
171
 
    reader.close();
172
 
    indexStore.close();
173
 
  }
174
 
}