use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterStrictPhrases method testPhraseNotInDoc.
public void testPhraseNotInDoc() throws IOException {
// query matches this; highlight it
indexWriter.addDocument(newDoc("Whatever yin"));
// query does NOT match this, only the SHOULD clause does
indexWriter.addDocument(newDoc("nextdoc yin"));
initReaderSearcherHighlighter();
BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "whatever")), BooleanClause.Occur.MUST).add(newPhraseQuery("body", "nextdoc yin"), BooleanClause.Occur.SHOULD).add(newPhraseQuery("body", "nonexistent yin"), BooleanClause.Occur.SHOULD).build();
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(new String[] { "<b>Whatever</b> yin" }, snippets);
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class FieldQueryTest method testFlattenBoolean.
public void testFlattenBoolean() throws Exception {
initBoost();
BooleanQuery.Builder booleanQueryB = new BooleanQuery.Builder();
booleanQueryB.add(tq("A"), Occur.MUST);
booleanQueryB.add(tq("B"), Occur.MUST);
booleanQueryB.add(tq("C"), Occur.SHOULD);
BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
innerQuery.add(tq("D"), Occur.MUST);
innerQuery.add(tq("E"), Occur.MUST);
booleanQueryB.add(innerQuery.build(), Occur.MUST_NOT);
Query booleanQuery = booleanQueryB.build();
booleanQuery = new BoostQuery(booleanQuery, boost);
FieldQuery fq = new FieldQuery(booleanQuery, true, true);
Set<Query> flatQueries = new HashSet<>();
fq.flatten(booleanQuery, reader, flatQueries, 1f);
assertCollectionQueries(flatQueries, tq(boost, "A"), tq(boost, "B"), tq(boost, "C"));
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterStrictPhrases method testMtq.
/**
* Like {@link #testRewriteAndMtq} but no rewrite.
*/
public void testMtq() throws IOException {
indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
initReaderSearcherHighlighter();
SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("body", "bravo")), // does NOT rewrite
new SpanTermQuery(new Term("body", "charlie")) }, 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.
//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> <b>charlie</b> - charlie bravo alpha" }, snippets);
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterStrictPhrases method testWithSameTermQuery.
public void testWithSameTermQuery() throws IOException {
indexWriter.addDocument(newDoc("Yin yang, yin gap yang"));
initReaderSearcherHighlighter();
BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "yin")), BooleanClause.Occur.MUST).add(newPhraseQuery("body", "yin yang"), BooleanClause.Occur.MUST).add(new TermQuery(new Term("title", "yang")), BooleanClause.Occur.SHOULD).build();
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(new String[] { "<b>Yin</b> <b>yang</b>, <b>yin</b> gap yang" }, snippets);
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterMTQ method testWithMaxLenAndMultipleWildcardMatches.
public void testWithMaxLenAndMultipleWildcardMatches() throws IOException {
RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
Field body = new Field("body", "", fieldType);
Document doc = new Document();
doc.add(body);
//tests interleaving of multiple wildcard matches with the CompositePostingsEnum
//In this case the CompositePostingsEnum will have an underlying PostingsEnum that jumps form pos 1 to 9 for bravo
//and a second with position 2 for Bravado
body.setStringValue("Alpha Bravo Bravado foo foo foo. Foo foo Alpha Bravo");
iw.addDocument(doc);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher searcher = newSearcher(ir);
UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
//a little past first sentence
highlighter.setMaxLength(32);
BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "alpha")), BooleanClause.Occur.MUST).add(new PrefixQuery(new Term("body", "bra")), BooleanClause.Occur.MUST).build();
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
//ask for 2 but we'll only get 1
String[] snippets = highlighter.highlight("body", query, topDocs, 2);
assertArrayEquals(new String[] { "<b>Alpha</b> <b>Bravo</b> <b>Bravado</b> foo foo foo." }, snippets);
ir.close();
}
Aggregations