1
package org.apache.lucene.search.regex;
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.analysis.MockAnalyzer;
23
import org.apache.lucene.document.Document;
24
import org.apache.lucene.document.Field;
25
import org.apache.lucene.index.CorruptIndexException;
26
import org.apache.lucene.index.IndexWriter;
27
import org.apache.lucene.index.Term;
28
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
29
import org.apache.lucene.search.IndexSearcher;
30
import org.apache.lucene.search.MultiSearcher;
31
import org.apache.lucene.search.spans.SpanFirstQuery;
32
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
33
import org.apache.lucene.search.spans.SpanNearQuery;
34
import org.apache.lucene.search.spans.SpanQuery;
35
import org.apache.lucene.store.Directory;
36
import org.apache.lucene.store.LockObtainFailedException;
37
import org.apache.lucene.util.LuceneTestCase;
39
public class TestSpanRegexQuery extends LuceneTestCase {
41
Directory indexStoreA;
42
Directory indexStoreB;
45
public void setUp() throws Exception {
47
indexStoreA = newDirectory();
48
indexStoreB = newDirectory();
52
public void tearDown() throws Exception {
58
public void testSpanRegex() throws Exception {
59
Directory directory = newDirectory();
60
IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(
61
TEST_VERSION_CURRENT, new MockAnalyzer(random)));
62
Document doc = new Document();
63
// doc.add(newField("field", "the quick brown fox jumps over the lazy dog",
64
// Field.Store.NO, Field.Index.ANALYZED));
65
// writer.addDocument(doc);
66
// doc = new Document();
67
doc.add(newField("field", "auto update", Field.Store.NO,
68
Field.Index.ANALYZED));
69
writer.addDocument(doc);
71
doc.add(newField("field", "first auto update", Field.Store.NO,
72
Field.Index.ANALYZED));
73
writer.addDocument(doc);
77
IndexSearcher searcher = new IndexSearcher(directory, true);
78
SpanQuery srq = new SpanMultiTermQueryWrapper<RegexQuery>(new RegexQuery(new Term("field", "aut.*")));
79
SpanFirstQuery sfq = new SpanFirstQuery(srq, 1);
80
// SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6,
82
int numHits = searcher.search(sfq, null, 1000).totalHits;
83
assertEquals(1, numHits);
88
public void testSpanRegexBug() throws CorruptIndexException, IOException {
89
createRAMDirectories();
91
SpanQuery srq = new SpanMultiTermQueryWrapper<RegexQuery>(new RegexQuery(new Term("field", "a.*")));
92
SpanQuery stq = new SpanMultiTermQueryWrapper<RegexQuery>(new RegexQuery(new Term("field", "b.*")));
93
SpanNearQuery query = new SpanNearQuery(new SpanQuery[] { srq, stq }, 6,
96
// 1. Search the same store which works
97
IndexSearcher[] arrSearcher = new IndexSearcher[2];
98
arrSearcher[0] = new IndexSearcher(indexStoreA, true);
99
arrSearcher[1] = new IndexSearcher(indexStoreB, true);
100
MultiSearcher searcher = new MultiSearcher(arrSearcher);
101
int numHits = searcher.search(query, null, 1000).totalHits;
102
arrSearcher[0].close();
103
arrSearcher[1].close();
106
// We expect 2 but only one matched
107
// The rewriter function only write it once on the first IndexSearcher
108
// So it's using term: a1 b1 to search on the second IndexSearcher
109
// As a result, it won't match the document in the second IndexSearcher
110
assertEquals(2, numHits);
113
/** remove in lucene 4.0 */
115
public void testSpanRegexOld() throws Exception {
116
Directory directory = newDirectory();
117
IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(
118
TEST_VERSION_CURRENT, new MockAnalyzer(random)));
119
Document doc = new Document();
120
// doc.add(newField("field", "the quick brown fox jumps over the lazy dog",
121
// Field.Store.NO, Field.Index.ANALYZED));
122
// writer.addDocument(doc);
123
// doc = new Document();
124
doc.add(newField("field", "auto update", Field.Store.NO,
125
Field.Index.ANALYZED));
126
writer.addDocument(doc);
127
doc = new Document();
128
doc.add(newField("field", "first auto update", Field.Store.NO,
129
Field.Index.ANALYZED));
130
writer.addDocument(doc);
134
IndexSearcher searcher = new IndexSearcher(directory, true);
135
SpanRegexQuery srq = new SpanRegexQuery(new Term("field", "aut.*"));
136
SpanFirstQuery sfq = new SpanFirstQuery(srq, 1);
137
// SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6,
139
int numHits = searcher.search(sfq, null, 1000).totalHits;
140
assertEquals(1, numHits);
145
/** remove in lucene 4.0 */
147
public void testSpanRegexBugOld() throws CorruptIndexException, IOException {
148
createRAMDirectories();
150
SpanRegexQuery srq = new SpanRegexQuery(new Term("field", "a.*"));
151
SpanRegexQuery stq = new SpanRegexQuery(new Term("field", "b.*"));
152
SpanNearQuery query = new SpanNearQuery(new SpanQuery[] { srq, stq }, 6,
155
// 1. Search the same store which works
156
IndexSearcher[] arrSearcher = new IndexSearcher[2];
157
arrSearcher[0] = new IndexSearcher(indexStoreA, true);
158
arrSearcher[1] = new IndexSearcher(indexStoreB, true);
159
MultiSearcher searcher = new MultiSearcher(arrSearcher);
160
int numHits = searcher.search(query, null, 1000).totalHits;
161
arrSearcher[0].close();
162
arrSearcher[1].close();
165
// We expect 2 but only one matched
166
// The rewriter function only write it once on the first IndexSearcher
167
// So it's using term: a1 b1 to search on the second IndexSearcher
168
// As a result, it won't match the document in the second IndexSearcher
169
assertEquals(2, numHits);
172
private void createRAMDirectories() throws CorruptIndexException,
173
LockObtainFailedException, IOException {
174
// creating a document to store
175
Document lDoc = new Document();
176
lDoc.add(newField("field", "a1 b1", Field.Store.NO,
177
Field.Index.ANALYZED_NO_NORMS));
179
// creating a document to store
180
Document lDoc2 = new Document();
181
lDoc2.add(newField("field", "a2 b2", Field.Store.NO,
182
Field.Index.ANALYZED_NO_NORMS));
184
// creating first index writer
185
IndexWriter writerA = new IndexWriter(indexStoreA, newIndexWriterConfig(
186
TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
187
writerA.addDocument(lDoc);
191
// creating second index writer
192
IndexWriter writerB = new IndexWriter(indexStoreB, newIndexWriterConfig(
193
TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));
194
writerB.addDocument(lDoc2);