use of org.apache.lucene.search.PrefixQuery in project lucene-solr by apache.
the class MyCrazyCustomField method getPrefixQuery.
@Override
public Query getPrefixQuery(QParser parser, SchemaField sf, String termStr) {
if (termStr.equals("foo")) {
termStr = "bar";
} else if (termStr.equals("bar")) {
termStr = "foo";
}
PrefixQuery query = new PrefixQuery(new Term(sf.getName(), termStr));
query.setRewriteMethod(sf.getType().getRewriteMethod(parser, sf));
return query;
}
use of org.apache.lucene.search.PrefixQuery in project lucene-solr by apache.
the class TestExitableDirectoryReader method testExitableFilterIndexReader.
/**
* Tests timing out of TermsEnum iterations
* @throws Exception on error
*/
@Ignore("this test relies on wall clock time and sometimes false fails")
public void testExitableFilterIndexReader() throws Exception {
Directory directory = newDirectory();
IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(new MockAnalyzer(random())));
Document d1 = new Document();
d1.add(newTextField("default", "one two", Field.Store.YES));
writer.addDocument(d1);
Document d2 = new Document();
d2.add(newTextField("default", "one three", Field.Store.YES));
writer.addDocument(d2);
Document d3 = new Document();
d3.add(newTextField("default", "ones two four", Field.Store.YES));
writer.addDocument(d3);
writer.forceMerge(1);
writer.commit();
writer.close();
DirectoryReader directoryReader;
DirectoryReader exitableDirectoryReader;
IndexReader reader;
IndexSearcher searcher;
Query query = new PrefixQuery(new Term("default", "o"));
// Set a fairly high timeout value (1 second) and expect the query to complete in that time frame.
// Not checking the validity of the result, all we are bothered about in this test is the timing out.
directoryReader = DirectoryReader.open(directory);
exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(1000));
reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
searcher = new IndexSearcher(reader);
searcher.search(query, 10);
reader.close();
// Set a really low timeout value (1 millisecond) and expect an Exception
directoryReader = DirectoryReader.open(directory);
exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(1));
reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
IndexSearcher slowSearcher = new IndexSearcher(reader);
expectThrows(ExitingReaderException.class, () -> {
slowSearcher.search(query, 10);
});
reader.close();
// Set maximum time out and expect the query to complete.
// Not checking the validity of the result, all we are bothered about in this test is the timing out.
directoryReader = DirectoryReader.open(directory);
exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(Long.MAX_VALUE));
reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
searcher = new IndexSearcher(reader);
searcher.search(query, 10);
reader.close();
// Set a negative time allowed and expect the query to complete (should disable timeouts)
// Not checking the validity of the result, all we are bothered about in this test is the timing out.
directoryReader = DirectoryReader.open(directory);
exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(-189034L));
reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
searcher = new IndexSearcher(reader);
searcher.search(query, 10);
reader.close();
directory.close();
}
use of org.apache.lucene.search.PrefixQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterStrictPhrases method testRewriteAndMtq.
/**
* Test it does *not* highlight the same term's not next to the span-near. "charlie" in this case.
* This particular example exercises "Rewrite" plus "MTQ" in the same query.
*/
public void testRewriteAndMtq() throws IOException {
indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
initReaderSearcherHighlighter();
SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("body", "bravo")), // REWRITES
new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch"))) }, 0, true);
BooleanQuery query = new BooleanQuery.Builder().add(snq, BooleanClause.Occur.MUST).add(new PrefixQuery(new Term("body", "al")), // MTQ
BooleanClause.Occur.MUST).add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST).add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD).build();
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>" }, snippets);
// do again, this time with MTQ disabled. We should only find "alpha bravo".
//disable but leave phrase processing enabled
highlighter.setHandleMultiTermQuery(false);
topDocs = searcher.search(query, 10, Sort.INDEXORDER);
snippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha" }, snippets);
}
use of org.apache.lucene.search.PrefixQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterStrictPhrases method testRewrite.
/**
* Like {@link #testRewriteAndMtq} but no freestanding MTQ
*/
public void testRewrite() throws IOException {
indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
initReaderSearcherHighlighter();
SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("body", "bravo")), // REWRITES
new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch"))) }, 0, true);
BooleanQuery query = new BooleanQuery.Builder().add(snq, BooleanClause.Occur.MUST).add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST).add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD).build();
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo alpha" }, snippets);
// do again, this time with MTQ disabled. We should only find "alpha bravo".
//disable but leave phrase processing enabled
highlighter.setHandleMultiTermQuery(false);
topDocs = searcher.search(query, 10, Sort.INDEXORDER);
snippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha" }, snippets);
}
use of org.apache.lucene.search.PrefixQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterStrictPhrases method testMultiValued.
public void testMultiValued() throws IOException {
indexWriter.addDocument(newDoc("one bravo three", "four bravo six"));
initReaderSearcherHighlighter();
BooleanQuery query = new BooleanQuery.Builder().add(newPhraseQuery("body", "one bravo"), BooleanClause.Occur.MUST).add(newPhraseQuery("body", "four bravo"), BooleanClause.Occur.MUST).add(new PrefixQuery(new Term("body", "br")), BooleanClause.Occur.MUST).build();
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
String[] snippets = highlighter.highlight("body", query, topDocs, 2);
assertArrayEquals(new String[] { "<b>one</b> <b>bravo</b> three... <b>four</b> <b>bravo</b> six" }, snippets);
// now test phraseQuery won't span across values
assert indexAnalyzer.getPositionIncrementGap("body") > 0;
PhraseQuery phraseQuery = newPhraseQuery("body", "three four");
// 1 too little; won't span
phraseQuery = setSlop(phraseQuery, indexAnalyzer.getPositionIncrementGap("body") - 1);
query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "bravo")), BooleanClause.Occur.MUST).add(phraseQuery, BooleanClause.Occur.SHOULD).build();
topDocs = searcher.search(query, 10);
snippets = highlighter.highlight("body", query, topDocs, 2);
assertEquals("one <b>bravo</b> three... four <b>bravo</b> six", snippets[0]);
// and add just enough slop to cross the values:
phraseQuery = newPhraseQuery("body", "three four");
// just enough to span
phraseQuery = setSlop(phraseQuery, indexAnalyzer.getPositionIncrementGap("body"));
query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "bravo")), BooleanClause.Occur.MUST).add(phraseQuery, // must match and it will
BooleanClause.Occur.MUST).build();
topDocs = searcher.search(query, 10);
assertEquals(1, topDocs.totalHits);
snippets = highlighter.highlight("body", query, topDocs, 2);
assertEquals("one <b>bravo</b> <b>three</b>... <b>four</b> <b>bravo</b> six", snippets[0]);
}
Aggregations