mirror of https://github.com/apache/lucene.git
LUCENE-470 - Added some more regex tests, contributed by Paul Elschot
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@348689 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
13ea2b0c3f
commit
cf1d106504
|
@ -1,5 +1,21 @@
|
||||||
package org.apache.lucene.search.regex;
|
package org.apache.lucene.search.regex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 2005 Apache Software Foundation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import org.apache.lucene.store.RAMDirectory;
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
@ -8,23 +24,70 @@ import org.apache.lucene.analysis.SimpleAnalyzer;
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.apache.lucene.search.Hits;
|
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
|
|
||||||
public class TestRegexQuery extends TestCase {
|
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||||
public void testRegex() throws Exception {
|
import org.apache.lucene.search.spans.SpanQuery;
|
||||||
RAMDirectory directory = new RAMDirectory();
|
|
||||||
IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), true);
|
|
||||||
Document doc = new Document();
|
|
||||||
doc.add(new Field("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.TOKENIZED));
|
|
||||||
writer.addDocument(doc);
|
|
||||||
writer.optimize();
|
|
||||||
writer.close();
|
|
||||||
|
|
||||||
IndexSearcher searcher = new IndexSearcher(directory);
|
public class TestRegexQuery extends TestCase {
|
||||||
Query query = new RegexQuery(new Term("field", "q.[aeiou]c.*"));
|
private IndexSearcher searcher;
|
||||||
Hits hits = searcher.search(query);
|
private final String FN = "field";
|
||||||
assertEquals(1, hits.length());
|
|
||||||
|
public void setUp() {
|
||||||
|
RAMDirectory directory = new RAMDirectory();
|
||||||
|
try {
|
||||||
|
IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), true);
|
||||||
|
Document doc = new Document();
|
||||||
|
doc.add(new Field(FN, "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.TOKENIZED));
|
||||||
|
writer.addDocument(doc);
|
||||||
|
writer.optimize();
|
||||||
|
writer.close();
|
||||||
|
searcher = new IndexSearcher(directory);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tearDown() {
|
||||||
|
try {
|
||||||
|
searcher.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Term newTerm(String value) { return new Term(FN, value); }
|
||||||
|
|
||||||
|
private int regexQueryNrHits(String regex) throws Exception {
|
||||||
|
Query query = new RegexQuery( newTerm(regex));
|
||||||
|
return searcher.search(query).length();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int spanRegexQueryNrHits(String regex1, String regex2, int slop, boolean ordered) throws Exception {
|
||||||
|
SpanRegexQuery srq1 = new SpanRegexQuery( newTerm(regex1));
|
||||||
|
SpanRegexQuery srq2 = new SpanRegexQuery( newTerm(regex2));
|
||||||
|
SpanNearQuery query = new SpanNearQuery( new SpanQuery[]{srq1, srq2}, slop, ordered);
|
||||||
|
return searcher.search(query).length();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRegex1() throws Exception {
|
||||||
|
assertEquals(1, regexQueryNrHits("q.[aeiou]c.*"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRegex2() throws Exception {
|
||||||
|
assertEquals(0, regexQueryNrHits(".[aeiou]c.*"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRegex3() throws Exception {
|
||||||
|
assertEquals(0, regexQueryNrHits("q.[aeiou]c"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSpanRegex1() throws Exception {
|
||||||
|
assertEquals(1, spanRegexQueryNrHits("q.[aeiou]c.*", "dog", 6, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSpanRegex2() throws Exception {
|
||||||
|
assertEquals(0, spanRegexQueryNrHits("q.[aeiou]c.*", "dog", 5, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue