add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queries / src / test / org / apache / lucene / search / regex / TestSpanRegexQuery.java
1 package org.apache.lucene.search.regex;
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.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;
38
39 public class TestSpanRegexQuery extends LuceneTestCase {
40   
41   Directory indexStoreA;
42   Directory indexStoreB;
43   
44   @Override
45   public void setUp() throws Exception {
46     super.setUp();
47     indexStoreA = newDirectory();
48     indexStoreB = newDirectory();
49   }
50   
51   @Override
52   public void tearDown() throws Exception {
53     indexStoreA.close();
54     indexStoreB.close();
55     super.tearDown();
56   }
57   
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);
70     doc = new Document();
71     doc.add(newField("field", "first auto update", Field.Store.NO,
72         Field.Index.ANALYZED));
73     writer.addDocument(doc);
74     writer.optimize();
75     writer.close();
76
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,
81     // true);
82     int numHits = searcher.search(sfq, null, 1000).totalHits;
83     assertEquals(1, numHits);
84     searcher.close();
85     directory.close();
86   }
87   
88   public void testSpanRegexBug() throws CorruptIndexException, IOException {
89     createRAMDirectories();
90
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,
94         true);
95
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();
104
105     // Will fail here
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);
111   }
112   
113   /** remove in lucene 4.0 */
114   @Deprecated
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);
131     writer.optimize();
132     writer.close();
133
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,
138     // true);
139     int numHits = searcher.search(sfq, null, 1000).totalHits;
140     assertEquals(1, numHits);
141     searcher.close();
142     directory.close();
143   }
144
145   /** remove in lucene 4.0 */
146   @Deprecated
147   public void testSpanRegexBugOld() throws CorruptIndexException, IOException {
148     createRAMDirectories();
149
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,
153         true);
154
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();
163
164     // Will fail here
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);
170   }
171
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));
178
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));
183
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);
188     writerA.optimize();
189     writerA.close();
190
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);
195     writerB.optimize();
196     writerB.close();
197   }
198 }