pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / TestCachingSpanFilter.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.io.IOException;
21
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.SerialMergeScheduler;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.search.spans.SpanTermQuery;
29 import org.apache.lucene.analysis.MockAnalyzer;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32 import org.apache.lucene.util._TestUtil;
33
34 public class TestCachingSpanFilter extends LuceneTestCase {
35
36   public void testEnforceDeletions() throws Exception {
37     Directory dir = newDirectory();
38     RandomIndexWriter writer = new RandomIndexWriter(
39         random,
40         dir,
41         newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random)).
42             setMergeScheduler(new SerialMergeScheduler()).
43             // asserts below requires no unexpected merges:
44             setMergePolicy(newLogMergePolicy(10))
45     );
46
47     // NOTE: cannot use writer.getReader because RIW (on
48     // flipping a coin) may give us a newly opened reader,
49     // but we use .reopen on this reader below and expect to
50     // (must) get an NRT reader:
51     IndexReader reader = IndexReader.open(writer.w, true);
52     // same reason we don't wrap?
53     IndexSearcher searcher = newSearcher(reader, false);
54
55     // add a doc, refresh the reader, and check that its there
56     Document doc = new Document();
57     doc.add(newField("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
58     writer.addDocument(doc);
59
60     reader = refreshReader(reader);
61     searcher.close();
62     searcher = newSearcher(reader, false);
63
64     TopDocs docs = searcher.search(new MatchAllDocsQuery(), 1);
65     assertEquals("Should find a hit...", 1, docs.totalHits);
66
67     final SpanFilter startFilter = new SpanQueryFilter(new SpanTermQuery(new Term("id", "1")));
68
69     // ignore deletions
70     CachingSpanFilter filter = new CachingSpanFilter(startFilter, CachingWrapperFilter.DeletesMode.IGNORE);
71         
72     docs = searcher.search(new MatchAllDocsQuery(), filter, 1);
73     assertEquals("[query + filter] Should find a hit...", 1, docs.totalHits);
74     ConstantScoreQuery constantScore = new ConstantScoreQuery(filter);
75     docs = searcher.search(constantScore, 1);
76     assertEquals("[just filter] Should find a hit...", 1, docs.totalHits);
77
78     // now delete the doc, refresh the reader, and see that
79     // it's not there
80     _TestUtil.keepFullyDeletedSegments(writer.w);
81     writer.deleteDocuments(new Term("id", "1"));
82
83     reader = refreshReader(reader);
84     searcher.close();
85     searcher = newSearcher(reader, false);
86
87     docs = searcher.search(new MatchAllDocsQuery(), filter, 1);
88     assertEquals("[query + filter] Should *not* find a hit...", 0, docs.totalHits);
89
90     docs = searcher.search(constantScore, 1);
91     assertEquals("[just filter] Should find a hit...", 1, docs.totalHits);
92
93
94     // force cache to regenerate:
95     filter = new CachingSpanFilter(startFilter, CachingWrapperFilter.DeletesMode.RECACHE);
96
97     writer.addDocument(doc);
98     reader = refreshReader(reader);
99     searcher.close();
100     searcher = newSearcher(reader, false);
101         
102     docs = searcher.search(new MatchAllDocsQuery(), filter, 1);
103     assertEquals("[query + filter] Should find a hit...", 1, docs.totalHits);
104
105     constantScore = new ConstantScoreQuery(filter);
106     docs = searcher.search(constantScore, 1);
107     assertEquals("[just filter] Should find a hit...", 1, docs.totalHits);
108
109     // NOTE: important to hold ref here so GC doesn't clear
110     // the cache entry!  Else the assert below may sometimes
111     // fail:
112     IndexReader oldReader = reader;
113
114     // make sure we get a cache hit when we reopen readers
115     // that had no new deletions
116     // Deletes nothing:
117     writer.deleteDocuments(new Term("foo", "bar"));
118     reader = refreshReader(reader);
119     assertTrue(reader == oldReader);
120     int missCount = filter.missCount;
121     docs = searcher.search(constantScore, 1);
122     assertEquals("[just filter] Should find a hit...", 1, docs.totalHits);
123     assertEquals(missCount, filter.missCount);
124
125     // now delete the doc, refresh the reader, and see that it's not there
126     writer.deleteDocuments(new Term("id", "1"));
127
128     reader = refreshReader(reader);
129     searcher.close();
130     searcher = newSearcher(reader, false);
131
132     docs = searcher.search(new MatchAllDocsQuery(), filter, 1);
133     assertEquals("[query + filter] Should *not* find a hit...", 0, docs.totalHits);
134
135     docs = searcher.search(constantScore, 1);
136     assertEquals("[just filter] Should *not* find a hit...", 0, docs.totalHits);
137
138     // NOTE: silliness to make sure JRE does not optimize
139     // away our holding onto oldReader to prevent
140     // CachingWrapperFilter's WeakHashMap from dropping the
141     // entry:
142     assertTrue(oldReader != null);
143
144     searcher.close();
145     writer.close();
146     reader.close();
147     dir.close();
148   }
149
150   private static IndexReader refreshReader(IndexReader reader) throws IOException {
151     IndexReader oldReader = reader;
152     reader = reader.reopen();
153     if (reader != oldReader) {
154       oldReader.close();
155     }
156     return reader;
157   }
158 }