use of org.apache.lucene.search.RegexpQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterMTQ method testOneRegexp.
public void testOneRegexp() throws Exception {
RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
Field body = new Field("body", "", fieldType);
Document doc = new Document();
doc.add(body);
body.setStringValue("This is a test.");
iw.addDocument(doc);
body.setStringValue("Test a one sentence document.");
iw.addDocument(doc);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher searcher = newSearcher(ir);
UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
Query query = new RegexpQuery(new Term("body", "te.*"));
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertEquals(2, topDocs.totalHits);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertEquals(2, snippets.length);
assertEquals("This is a <b>test</b>.", snippets[0]);
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
// wrong field
BooleanQuery bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD).add(new RegexpQuery(new Term("bogus", "te.*")), BooleanClause.Occur.SHOULD).build();
topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
assertEquals(2, topDocs.totalHits);
snippets = highlighter.highlight("body", bq, topDocs);
assertEquals(2, snippets.length);
assertEquals("This is a test.", snippets[0]);
assertEquals("Test a one sentence document.", snippets[1]);
ir.close();
}
use of org.apache.lucene.search.RegexpQuery in project lucene-solr by apache.
the class TestMemoryIndexAgainstRAMDir method testNullPointerException.
// LUCENE-3831
public void testNullPointerException() throws IOException {
RegexpQuery regex = new RegexpQuery(new Term("field", "worl."));
SpanQuery wrappedquery = new SpanMultiTermQueryWrapper<>(regex);
MemoryIndex mindex = randomMemoryIndex();
mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", "hello there"));
// This throws an NPE
assertEquals(0, mindex.search(wrappedquery), 0.00001f);
TestUtil.checkReader(mindex.createSearcher().getIndexReader());
}
use of org.apache.lucene.search.RegexpQuery in project lucene-solr by apache.
the class TestQPHelper method testRegexQueryParsing.
public void testRegexQueryParsing() throws Exception {
final String[] fields = { "b", "t" };
final StandardQueryParser parser = new StandardQueryParser();
parser.setMultiFields(fields);
parser.setDefaultOperator(StandardQueryConfigHandler.Operator.AND);
parser.setAnalyzer(new MockAnalyzer(random()));
BooleanQuery.Builder exp = new BooleanQuery.Builder();
//TODO spezification? was "MUST"
exp.add(new BooleanClause(new RegexpQuery(new Term("b", "ab.+")), BooleanClause.Occur.SHOULD));
//TODO spezification? was "MUST"
exp.add(new BooleanClause(new RegexpQuery(new Term("t", "ab.+")), BooleanClause.Occur.SHOULD));
assertEquals(exp.build(), parser.parse("/ab.+/", null));
RegexpQuery regexpQueryexp = new RegexpQuery(new Term("test", "[abc]?[0-9]"));
assertEquals(regexpQueryexp, parser.parse("test:/[abc]?[0-9]/", null));
}
use of org.apache.lucene.search.RegexpQuery in project Krill by KorAP.
the class TestRegexIndex method testWildcardQuestionMarkRewritten.
@Test
public void testWildcardQuestionMarkRewritten() throws IOException {
ki = new KrillIndex();
ki.addDoc(createFieldDoc1());
ki.commit();
// meine? /+w1:2,s0 &Erfahrung
// meine? rewritten into meine.
SpanMultiTermQueryWrapper<RegexpQuery> mtq = new SpanMultiTermQueryWrapper<RegexpQuery>(new RegexpQuery(new Term("tokens", "s:meine.")));
SpanMultipleDistanceQuery mdsq = new SpanMultipleDistanceQuery(new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true);
kr = ki.search(mdsq, (short) 10);
assertEquals(3, kr.getMatches().size());
}
use of org.apache.lucene.search.RegexpQuery in project Krill by KorAP.
the class TestRegexIndex method testWildcardPlusRewritten2.
@Test
public void testWildcardPlusRewritten2() throws IOException {
ki = new KrillIndex();
ki.addDoc(createFieldDoc1());
ki.commit();
// C2 mein+ /+w1:2,s0 &Erfahrung
// mein+ rewritten into mein.?
SpanMultiTermQueryWrapper<RegexpQuery> mtq = new SpanMultiTermQueryWrapper<RegexpQuery>(new RegexpQuery(new Term("tokens", "s:mein.?")));
SpanMultipleDistanceQuery mdsq = new SpanMultipleDistanceQuery(new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true);
kr = ki.search(mdsq, (short) 10);
assertEquals(2, kr.getMatches().size());
}
Aggregations