Search in sources :

Example 66 with BooleanQuery

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

the class FieldQuery method flatten.

void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    while (sourceQuery instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) sourceQuery;
        sourceQuery = bq.getQuery();
        boost *= bq.getBoost();
    }
    if (sourceQuery instanceof BooleanQuery) {
        BooleanQuery bq = (BooleanQuery) sourceQuery;
        for (BooleanClause clause : bq) {
            if (!clause.isProhibited()) {
                flatten(clause.getQuery(), reader, flatQueries, boost);
            }
        }
    } else if (sourceQuery instanceof DisjunctionMaxQuery) {
        DisjunctionMaxQuery dmq = (DisjunctionMaxQuery) sourceQuery;
        for (Query query : dmq) {
            flatten(query, reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof TermQuery) {
        if (boost != 1f) {
            sourceQuery = new BoostQuery(sourceQuery, boost);
        }
        if (!flatQueries.contains(sourceQuery))
            flatQueries.add(sourceQuery);
    } else if (sourceQuery instanceof SynonymQuery) {
        SynonymQuery synQuery = (SynonymQuery) sourceQuery;
        for (Term term : synQuery.getTerms()) {
            flatten(new TermQuery(term), reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof PhraseQuery) {
        PhraseQuery pq = (PhraseQuery) sourceQuery;
        if (pq.getTerms().length == 1)
            sourceQuery = new TermQuery(pq.getTerms()[0]);
        if (boost != 1f) {
            sourceQuery = new BoostQuery(sourceQuery, boost);
        }
        flatQueries.add(sourceQuery);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        final Query q = ((ConstantScoreQuery) sourceQuery).getQuery();
        if (q != null) {
            flatten(q, reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof CustomScoreQuery) {
        final Query q = ((CustomScoreQuery) sourceQuery).getSubQuery();
        if (q != null) {
            flatten(q, reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof ToParentBlockJoinQuery) {
        Query childQuery = ((ToParentBlockJoinQuery) sourceQuery).getChildQuery();
        if (childQuery != null) {
            flatten(childQuery, reader, flatQueries, boost);
        }
    } else if (reader != null) {
        Query query = sourceQuery;
        Query rewritten;
        if (sourceQuery instanceof MultiTermQuery) {
            rewritten = new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(MAX_MTQ_TERMS).rewrite(reader, (MultiTermQuery) query);
        } else {
            rewritten = query.rewrite(reader);
        }
        if (rewritten != query) {
            // only rewrite once and then flatten again - the rewritten query could have a speacial treatment
            // if this method is overwritten in a subclass.
            flatten(rewritten, reader, flatQueries, boost);
        }
    // if the query is already rewritten we discard it
    }
// else discard queries
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) BooleanClause(org.apache.lucene.search.BooleanClause) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 67 with BooleanQuery

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

the class QueryTermExtractor method getTerms.

private static final void getTerms(Query query, float boost, HashSet<WeightedTerm> terms, boolean prohibited, String fieldName) {
    try {
        if (query instanceof BoostQuery) {
            BoostQuery boostQuery = (BoostQuery) query;
            getTerms(boostQuery.getQuery(), boost * boostQuery.getBoost(), terms, prohibited, fieldName);
        } else if (query instanceof BooleanQuery)
            getTermsFromBooleanQuery((BooleanQuery) query, boost, terms, prohibited, fieldName);
        else {
            HashSet<Term> nonWeightedTerms = new HashSet<>();
            try {
                EMPTY_INDEXSEARCHER.createNormalizedWeight(query, false).extractTerms(nonWeightedTerms);
            } catch (IOException bogus) {
                throw new RuntimeException("Should not happen on an empty index", bogus);
            }
            for (Iterator<Term> iter = nonWeightedTerms.iterator(); iter.hasNext(); ) {
                Term term = iter.next();
                if ((fieldName == null) || (term.field().equals(fieldName))) {
                    terms.add(new WeightedTerm(boost, term.text()));
                }
            }
        }
    } catch (UnsupportedOperationException ignore) {
    //this is non-fatal for our purposes
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Iterator(java.util.Iterator) IOException(java.io.IOException) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) HashSet(java.util.HashSet)

Example 68 with BooleanQuery

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

the class SynonymTokenizer method testToChildBlockJoinQuery.

public void testToChildBlockJoinQuery() throws Exception {
    BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term(FIELD_NAME, "parent")));
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    booleanQuery.add(new ToChildBlockJoinQuery(new TermQuery(new Term(FIELD_NAME, "parent")), parentFilter), Occur.MUST);
    booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "child")), Occur.MUST);
    query = booleanQuery.build();
    searcher = newSearcher(reader);
    hits = searcher.search(query, 100);
    int maxNumFragmentsRequired = 2;
    QueryScorer scorer = new QueryScorer(query, FIELD_NAME);
    Highlighter highlighter = new Highlighter(this, scorer);
    for (int i = 0; i < hits.totalHits; i++) {
        String text = "parent document";
        final int docId = hits.scoreDocs[i].doc;
        TokenStream tokenStream = getAnyTokenStream(FIELD_NAME, docId);
        highlighter.setTextFragmenter(new SimpleFragmenter(40));
        highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired, "...");
    }
    assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 1);
}
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) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Term(org.apache.lucene.index.Term) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) IntPoint(org.apache.lucene.document.IntPoint) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer)

Example 69 with BooleanQuery

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

the class TestQPHelper method testSimple.

public void testSimple() throws Exception {
    assertQueryEquals("field=a", null, "a");
    assertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");
    assertQueryEquals("term term term", null, "term term term");
    assertQueryEquals("t�rm term term", new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false), "t�rm term term");
    assertQueryEquals("�mlaut", new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false), "�mlaut");
    // FIXME: change MockAnalyzer to not extend CharTokenizer for this test
    //assertQueryEquals("\"\"", new KeywordAnalyzer(), "");
    //assertQueryEquals("foo:\"\"", new KeywordAnalyzer(), "foo:");
    assertQueryEquals("a AND b", null, "+a +b");
    assertQueryEquals("(a AND b)", null, "+a +b");
    assertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
    assertQueryEquals("a AND NOT b", null, "+a -b");
    assertQueryEquals("a AND -b", null, "+a -b");
    assertQueryEquals("a AND !b", null, "+a -b");
    assertQueryEquals("a && b", null, "+a +b");
    assertQueryEquals("a && ! b", null, "+a -b");
    assertQueryEquals("a OR b", null, "a b");
    assertQueryEquals("a || b", null, "a b");
    assertQueryEquals("a OR !b", null, "a -b");
    assertQueryEquals("a OR ! b", null, "a -b");
    assertQueryEquals("a OR -b", null, "a -b");
    assertQueryEquals("+term -term term", null, "+term -term term");
    assertQueryEquals("foo:term AND field:anotherTerm", null, "+foo:term +anotherterm");
    assertQueryEquals("term AND \"phrase phrase\"", null, "+term +\"phrase phrase\"");
    assertQueryEquals("\"hello there\"", null, "\"hello there\"");
    assertTrue(getQuery("a AND b", null) instanceof BooleanQuery);
    assertTrue(getQuery("hello", null) instanceof TermQuery);
    assertTrue(getQuery("\"hello there\"", null) instanceof PhraseQuery);
    assertQueryEquals("germ term^2.0", null, "germ (term)^2.0");
    assertQueryEquals("(term)^2.0", null, "(term)^2.0");
    assertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");
    assertQueryEquals("term^2.0", null, "(term)^2.0");
    assertQueryEquals("term^2", null, "(term)^2.0");
    assertQueryEquals("\"germ term\"^2.0", null, "(\"germ term\")^2.0");
    assertQueryEquals("\"term germ\"^2", null, "(\"term germ\")^2.0");
    assertQueryEquals("(foo OR bar) AND (baz OR boo)", null, "+(foo bar) +(baz boo)");
    assertQueryEquals("((a OR b) AND NOT c) OR d", null, "(+(a b) -c) d");
    assertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)", null, "+(apple \"steve jobs\") -(foo bar baz)");
    assertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"", null, "+(title:dog title:cat) -author:\"bob dole\"");
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) TermQuery(org.apache.lucene.search.TermQuery) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery)

Example 70 with BooleanQuery

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

the class SimpleFragmentsBuilderTest method test3Frags.

public void test3Frags() throws Exception {
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    booleanQuery.add(new TermQuery(new Term(F, "a")), BooleanClause.Occur.SHOULD);
    booleanQuery.add(new TermQuery(new Term(F, "c")), BooleanClause.Occur.SHOULD);
    FieldFragList ffl = ffl(booleanQuery.build(), "a b b b b b b b b b b b a b a b b b b b c a a b b");
    SimpleFragmentsBuilder sfb = new SimpleFragmentsBuilder();
    String[] f = sfb.createFragments(reader, 0, F, ffl, 3);
    assertEquals(3, f.length);
    assertEquals("<b>a</b> b b b b b b b b b b", f[0]);
    assertEquals("b b <b>a</b> b <b>a</b> b b b b b c", f[1]);
    assertEquals("<b>c</b> <b>a</b> <b>a</b> b b", f[2]);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term)

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