01: package org.apache.lucene.search.regex;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 junit.framework.TestCase;
21: import org.apache.lucene.store.RAMDirectory;
22: import org.apache.lucene.index.IndexWriter;
23: import org.apache.lucene.index.Term;
24: import org.apache.lucene.analysis.SimpleAnalyzer;
25: import org.apache.lucene.document.Document;
26: import org.apache.lucene.document.Field;
27: import org.apache.lucene.search.IndexSearcher;
28: import org.apache.lucene.search.Hits;
29: import org.apache.lucene.search.spans.SpanTermQuery;
30: import org.apache.lucene.search.spans.SpanNearQuery;
31: import org.apache.lucene.search.spans.SpanQuery;
32: import org.apache.lucene.search.spans.SpanFirstQuery;
33:
34: public class TestSpanRegexQuery extends TestCase {
35: public void testSpanRegex() throws Exception {
36: RAMDirectory directory = new RAMDirectory();
37: IndexWriter writer = new IndexWriter(directory,
38: new SimpleAnalyzer(), true);
39: Document doc = new Document();
40: // doc.add(new Field("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.TOKENIZED));
41: // writer.addDocument(doc);
42: // doc = new Document();
43: doc.add(new Field("field", "auto update", Field.Store.NO,
44: Field.Index.TOKENIZED));
45: writer.addDocument(doc);
46: doc = new Document();
47: doc.add(new Field("field", "first auto update", Field.Store.NO,
48: Field.Index.TOKENIZED));
49: writer.addDocument(doc);
50: writer.optimize();
51: writer.close();
52:
53: IndexSearcher searcher = new IndexSearcher(directory);
54: SpanRegexQuery srq = new SpanRegexQuery(new Term("field",
55: "aut.*"));
56: SpanFirstQuery sfq = new SpanFirstQuery(srq, 1);
57: // SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6, true);
58: Hits hits = searcher.search(sfq);
59: assertEquals(1, hits.length());
60: }
61: }
|