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.io.IOException;
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;
34 public class TestCachingSpanFilter extends LuceneTestCase {
36 public void testEnforceDeletions() throws Exception {
37 Directory dir = newDirectory();
38 RandomIndexWriter writer = new RandomIndexWriter(
41 newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random)).
42 setMergeScheduler(new SerialMergeScheduler()).
43 // asserts below requires no unexpected merges:
44 setMergePolicy(newLogMergePolicy(10))
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);
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);
60 reader = refreshReader(reader);
62 searcher = newSearcher(reader, false);
64 TopDocs docs = searcher.search(new MatchAllDocsQuery(), 1);
65 assertEquals("Should find a hit...", 1, docs.totalHits);
67 final SpanFilter startFilter = new SpanQueryFilter(new SpanTermQuery(new Term("id", "1")));
70 CachingSpanFilter filter = new CachingSpanFilter(startFilter, CachingWrapperFilter.DeletesMode.IGNORE);
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);
78 // now delete the doc, refresh the reader, and see that
80 _TestUtil.keepFullyDeletedSegments(writer.w);
81 writer.deleteDocuments(new Term("id", "1"));
83 reader = refreshReader(reader);
85 searcher = newSearcher(reader, false);
87 docs = searcher.search(new MatchAllDocsQuery(), filter, 1);
88 assertEquals("[query + filter] Should *not* find a hit...", 0, docs.totalHits);
90 docs = searcher.search(constantScore, 1);
91 assertEquals("[just filter] Should find a hit...", 1, docs.totalHits);
94 // force cache to regenerate:
95 filter = new CachingSpanFilter(startFilter, CachingWrapperFilter.DeletesMode.RECACHE);
97 writer.addDocument(doc);
98 reader = refreshReader(reader);
100 searcher = newSearcher(reader, false);
102 docs = searcher.search(new MatchAllDocsQuery(), filter, 1);
103 assertEquals("[query + filter] Should find a hit...", 1, docs.totalHits);
105 constantScore = new ConstantScoreQuery(filter);
106 docs = searcher.search(constantScore, 1);
107 assertEquals("[just filter] Should find a hit...", 1, docs.totalHits);
109 // NOTE: important to hold ref here so GC doesn't clear
110 // the cache entry! Else the assert below may sometimes
112 IndexReader oldReader = reader;
114 // make sure we get a cache hit when we reopen readers
115 // that had no new deletions
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);
125 // now delete the doc, refresh the reader, and see that it's not there
126 writer.deleteDocuments(new Term("id", "1"));
128 reader = refreshReader(reader);
130 searcher = newSearcher(reader, false);
132 docs = searcher.search(new MatchAllDocsQuery(), filter, 1);
133 assertEquals("[query + filter] Should *not* find a hit...", 0, docs.totalHits);
135 docs = searcher.search(constantScore, 1);
136 assertEquals("[just filter] Should *not* find a hit...", 0, docs.totalHits);
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
142 assertTrue(oldReader != null);
150 private static IndexReader refreshReader(IndexReader reader) throws IOException {
151 IndexReader oldReader = reader;
152 reader = reader.reopen();
153 if (reader != oldReader) {