Search in sources :

Example 41 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project zm-mailbox by Zimbra.

the class LuceneQueryOperation method combineOps.

@Override
protected QueryOperation combineOps(QueryOperation other, boolean union) {
    assert (!haveRunSearch);
    if (union) {
        if (other.hasNoResults()) {
            queryInfo.addAll(other.getResultInfo());
            // a query for (other OR nothing) == other
            return this;
        }
    } else {
        if (other.hasAllResults()) {
            if (other.hasSpamTrashSetting()) {
                forceHasSpamTrashSetting();
            }
            queryInfo.addAll(other.getResultInfo());
            // we match all results.  (other AND anything) == other
            return this;
        }
    }
    if (other instanceof LuceneQueryOperation) {
        LuceneQueryOperation otherLucene = (LuceneQueryOperation) other;
        if (union) {
            queryString = '(' + queryString + ") OR (" + otherLucene.queryString + ')';
        } else {
            queryString = '(' + queryString + ") AND (" + otherLucene.queryString + ')';
        }
        BooleanQuery top = new BooleanQuery();
        if (union) {
            if (luceneQuery instanceof BooleanQuery) {
                orCopy((BooleanQuery) luceneQuery, top);
            } else {
                top.add(new BooleanClause(luceneQuery, Occur.SHOULD));
            }
            if (otherLucene.luceneQuery instanceof BooleanQuery) {
                orCopy((BooleanQuery) otherLucene.luceneQuery, top);
            } else {
                top.add(new BooleanClause(otherLucene.luceneQuery, Occur.SHOULD));
            }
        } else {
            if (luceneQuery instanceof BooleanQuery) {
                andCopy((BooleanQuery) luceneQuery, top);
            } else {
                top.add(new BooleanClause(luceneQuery, Occur.MUST));
            }
            if (otherLucene.luceneQuery instanceof BooleanQuery) {
                andCopy((BooleanQuery) otherLucene.luceneQuery, top);
            } else {
                top.add(new BooleanClause(otherLucene.luceneQuery, Occur.MUST));
            }
        }
        luceneQuery = top;
        queryInfo.addAll(other.getResultInfo());
        if (other.hasSpamTrashSetting()) {
            forceHasSpamTrashSetting();
        }
        return this;
    }
    return null;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 42 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class SynonymTokenizer method testOverlapAnalyzer2.

public void testOverlapAnalyzer2() throws Exception {
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            String s = "Hi-Speed10 foo";
            Query query;
            Highlighter highlighter;
            String result;
            query = new TermQuery(new Term("text", "foo"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2(), s, 3, "...");
            assertEquals("Hi-Speed10 <B>foo</B>", result);
            query = new TermQuery(new Term("text", "10"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2(), s, 3, "...");
            assertEquals("Hi-Speed<B>10</B> foo", result);
            query = new TermQuery(new Term("text", "hi"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2(), s, 3, "...");
            assertEquals("<B>Hi</B>-Speed10 foo", result);
            query = new TermQuery(new Term("text", "speed"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2(), s, 3, "...");
            assertEquals("Hi-<B>Speed</B>10 foo", result);
            query = new TermQuery(new Term("text", "hispeed"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2(), s, 3, "...");
            assertEquals("<B>Hi-Speed</B>10 foo", result);
            BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
            booleanQuery.add(new TermQuery(new Term("text", "hi")), Occur.SHOULD);
            booleanQuery.add(new TermQuery(new Term("text", "speed")), Occur.SHOULD);
            query = booleanQuery.build();
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2(), s, 3, "...");
            assertEquals("<B>Hi-Speed</B>10 foo", result);
            // ///////////////// same tests, just put the bigger overlapping token
            // first
            query = new TermQuery(new Term("text", "foo"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
            assertEquals("Hi-Speed10 <B>foo</B>", result);
            query = new TermQuery(new Term("text", "10"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
            assertEquals("Hi-Speed<B>10</B> foo", result);
            query = new TermQuery(new Term("text", "hi"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
            assertEquals("<B>Hi</B>-Speed10 foo", result);
            query = new TermQuery(new Term("text", "speed"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
            assertEquals("Hi-<B>Speed</B>10 foo", result);
            query = new TermQuery(new Term("text", "hispeed"));
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
            assertEquals("<B>Hi-Speed</B>10 foo", result);
            query = booleanQuery.build();
            highlighter = getHighlighter(query, "text", HighlighterTest.this);
            result = highlighter.getBestFragments(getTS2a(), s, 3, "...");
            assertEquals("<B>Hi-Speed</B>10 foo", result);
        }
    };
    helper.start();
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) Query(org.apache.lucene.search.Query) SpanPayloadCheckQuery(org.apache.lucene.queries.payloads.SpanPayloadCheckQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Term(org.apache.lucene.index.Term)

Example 43 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class TestUnifiedHighlighter method testMultipleTerms.

public void testMultipleTerms() 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. Just a test highlighting from postings. Feel free to ignore.");
    iw.addDocument(doc);
    body.setStringValue("Highlighting the first term. Hope it works.");
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
    BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "highlighting")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("body", "just")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("body", "first")), BooleanClause.Occur.SHOULD).build();
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String[] snippets = highlighter.highlight("body", query, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("<b>Just</b> a test <b>highlighting</b> from postings. ", snippets[0]);
    assertEquals("<b>Highlighting</b> the <b>first</b> term. ", snippets[1]);
    ir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 44 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class TestUnifiedHighlighter method testBooleanMustNot.

public void testBooleanMustNot() throws Exception {
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
    Field body = new Field("body", "This sentence has both terms.  This sentence has only terms.", fieldType);
    Document document = new Document();
    document.add(body);
    iw.addDocument(document);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    BooleanQuery query2 = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "both")), BooleanClause.Occur.MUST_NOT).build();
    BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "terms")), BooleanClause.Occur.SHOULD).add(query2, BooleanClause.Occur.SHOULD).build();
    TopDocs topDocs = searcher.search(query, 10);
    assertEquals(1, topDocs.totalHits);
    UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
    highlighter.setMaxLength(Integer.MAX_VALUE - 1);
    String[] snippets = highlighter.highlight("body", query, topDocs, 2);
    assertEquals(1, snippets.length);
    assertFalse(snippets[0].contains("<b>both</b>"));
    ir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 45 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.

the class TestUnifiedHighlighter method testCambridgeMA.

public void testCambridgeMA() throws Exception {
    BufferedReader r = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("CambridgeMA.utf8"), StandardCharsets.UTF_8));
    String text = r.readLine();
    r.close();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
    Field body = new Field("body", text, fieldType);
    Document document = new Document();
    document.add(body);
    iw.addDocument(document);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "porter")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("body", "square")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("body", "massachusetts")), BooleanClause.Occur.SHOULD).build();
    TopDocs topDocs = searcher.search(query, 10);
    assertEquals(1, topDocs.totalHits);
    UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
    highlighter.setMaxLength(Integer.MAX_VALUE - 1);
    String[] snippets = highlighter.highlight("body", query, topDocs, 2);
    assertEquals(1, snippets.length);
    assertTrue(snippets[0].contains("<b>Square</b>"));
    assertTrue(snippets[0].contains("<b>Porter</b>"));
    ir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) InputStreamReader(java.io.InputStreamReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) BufferedReader(java.io.BufferedReader) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)223 TermQuery (org.apache.lucene.search.TermQuery)134 Term (org.apache.lucene.index.Term)114 Query (org.apache.lucene.search.Query)94 BooleanClause (org.apache.lucene.search.BooleanClause)60 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)53 BoostQuery (org.apache.lucene.search.BoostQuery)50 PhraseQuery (org.apache.lucene.search.PhraseQuery)45 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)45 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 TopDocs (org.apache.lucene.search.TopDocs)39 IndexReader (org.apache.lucene.index.IndexReader)38 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)34 IndexSearcher (org.apache.lucene.search.IndexSearcher)34 WildcardQuery (org.apache.lucene.search.WildcardQuery)34 Document (org.apache.lucene.document.Document)30 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)28 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)28 ArrayList (java.util.ArrayList)27 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)27