Search in sources :

Example 51 with BooleanQuery

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);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Term(org.apache.lucene.index.Term)

Example 52 with BooleanQuery

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"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashSet(java.util.HashSet)

Example 53 with BooleanQuery

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);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 54 with BooleanQuery

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);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Term(org.apache.lucene.index.Term)

Example 55 with BooleanQuery

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) PrefixQuery(org.apache.lucene.search.PrefixQuery) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)297 TermQuery (org.apache.lucene.search.TermQuery)176 Term (org.apache.lucene.index.Term)144 Query (org.apache.lucene.search.Query)129 BooleanClause (org.apache.lucene.search.BooleanClause)89 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)70 BoostQuery (org.apache.lucene.search.BoostQuery)58 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)52 PhraseQuery (org.apache.lucene.search.PhraseQuery)50 ArrayList (java.util.ArrayList)47 TopDocs (org.apache.lucene.search.TopDocs)47 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)43 WildcardQuery (org.apache.lucene.search.WildcardQuery)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)40 IndexReader (org.apache.lucene.index.IndexReader)39 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 Test (org.junit.Test)39 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)38 Document (org.apache.lucene.document.Document)36 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)33