Search in sources :

Example 76 with BooleanClause

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

the class MaxScoreQParser method parse.

/**
   * Parses the query exactly like the Lucene parser does, but
   * delegates all SHOULD clauses to DisjunctionMaxQuery with
   * meaning only the clause with the max score will contribute
   * to the overall score, unless the tie parameter is specified.
   * <br>
   * The max() is only calculated from the SHOULD clauses.
   * Any MUST clauses will be passed through as separate
   * BooleanClauses and thus always contribute to the score.
   * @return the resulting Query
   * @throws org.apache.solr.search.SyntaxError if parsing fails
   */
@Override
public Query parse() throws SyntaxError {
    Query q = super.parse();
    float boost = 1f;
    if (q instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) q;
        boost = bq.getBoost();
        q = bq.getQuery();
    }
    if (q instanceof BooleanQuery == false) {
        if (boost != 1f) {
            q = new BoostQuery(q, boost);
        }
        return q;
    }
    BooleanQuery obq = (BooleanQuery) q;
    Collection<Query> should = new ArrayList<>();
    Collection<BooleanClause> prohibOrReq = new ArrayList<>();
    BooleanQuery.Builder newqb = new BooleanQuery.Builder();
    for (BooleanClause clause : obq) {
        if (clause.isProhibited() || clause.isRequired()) {
            prohibOrReq.add(clause);
        } else {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(clause);
            should.add(bq.build());
        }
    }
    if (should.size() > 0) {
        DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(should, tie);
        newqb.add(dmq, BooleanClause.Occur.SHOULD);
    }
    for (BooleanClause c : prohibOrReq) {
        newqb.add(c);
    }
    Query newq = newqb.build();
    if (boost != 1f) {
        newq = new BoostQuery(newq, boost);
    }
    return newq;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 77 with BooleanClause

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

the class QueryParsing method toString.

/**
   * @see #toString(Query,IndexSchema)
   */
public static void toString(Query query, IndexSchema schema, Appendable out, int flags) throws IOException {
    // clear the boosted / is clause flags for recursion
    int subflag = flags & ~(FLAG_BOOSTED | FLAG_IS_CLAUSE);
    if (query instanceof TermQuery) {
        TermQuery q = (TermQuery) query;
        Term t = q.getTerm();
        FieldType ft = writeFieldName(t.field(), schema, out, flags);
        writeFieldVal(t.bytes(), ft, out, flags);
    } else if (query instanceof TermRangeQuery) {
        TermRangeQuery q = (TermRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesLower() ? '[' : '{');
        BytesRef lt = q.getLowerTerm();
        BytesRef ut = q.getUpperTerm();
        if (lt == null) {
            out.append('*');
        } else {
            writeFieldVal(lt, ft, out, flags);
        }
        out.append(" TO ");
        if (ut == null) {
            out.append('*');
        } else {
            writeFieldVal(ut, ft, out, flags);
        }
        out.append(q.includesUpper() ? ']' : '}');
    } else if (query instanceof LegacyNumericRangeQuery) {
        LegacyNumericRangeQuery q = (LegacyNumericRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesMin() ? '[' : '{');
        Number lt = q.getMin();
        Number ut = q.getMax();
        if (lt == null) {
            out.append('*');
        } else {
            out.append(lt.toString());
        }
        out.append(" TO ");
        if (ut == null) {
            out.append('*');
        } else {
            out.append(ut.toString());
        }
        out.append(q.includesMax() ? ']' : '}');
    } else if (query instanceof BooleanQuery) {
        BooleanQuery q = (BooleanQuery) query;
        boolean needParens = false;
        if (q.getMinimumNumberShouldMatch() != 0 || (flags & (FLAG_IS_CLAUSE | FLAG_BOOSTED)) != 0) {
            needParens = true;
        }
        if (needParens) {
            out.append('(');
        }
        boolean first = true;
        for (BooleanClause c : q.clauses()) {
            if (!first) {
                out.append(' ');
            } else {
                first = false;
            }
            if (c.isProhibited()) {
                out.append('-');
            } else if (c.isRequired()) {
                out.append('+');
            }
            Query subQuery = c.getQuery();
            toString(subQuery, schema, out, subflag | FLAG_IS_CLAUSE);
        }
        if (needParens) {
            out.append(')');
        }
        if (q.getMinimumNumberShouldMatch() > 0) {
            out.append('~');
            out.append(Integer.toString(q.getMinimumNumberShouldMatch()));
        }
    } else if (query instanceof PrefixQuery) {
        PrefixQuery q = (PrefixQuery) query;
        Term prefix = q.getPrefix();
        FieldType ft = writeFieldName(prefix.field(), schema, out, flags);
        out.append(prefix.text());
        out.append('*');
    } else if (query instanceof WildcardQuery) {
        out.append(query.toString());
    } else if (query instanceof FuzzyQuery) {
        out.append(query.toString());
    } else if (query instanceof ConstantScoreQuery) {
        out.append(query.toString());
    } else if (query instanceof WrappedQuery) {
        WrappedQuery q = (WrappedQuery) query;
        out.append(q.getOptions());
        toString(q.getWrappedQuery(), schema, out, subflag);
    } else if (query instanceof BoostQuery) {
        BoostQuery q = (BoostQuery) query;
        toString(q.getQuery(), schema, out, subflag | FLAG_BOOSTED);
        out.append("^");
        out.append(Float.toString(q.getBoost()));
    } else {
        out.append(query.getClass().getSimpleName() + '(' + query.toString() + ')');
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) FieldType(org.apache.solr.schema.FieldType) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 78 with BooleanClause

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

the class TestBlockJoinValidation method createChildrenQueryWithOneParent.

private static Query createChildrenQueryWithOneParent(int childNumber) {
    TermQuery childQuery = new TermQuery(new Term("child", createFieldValue(childNumber)));
    Query randomParentQuery = new TermQuery(new Term("id", createFieldValue(getRandomParentId())));
    BooleanQuery.Builder childrenQueryWithRandomParent = new BooleanQuery.Builder();
    childrenQueryWithRandomParent.add(new BooleanClause(childQuery, BooleanClause.Occur.SHOULD));
    childrenQueryWithRandomParent.add(new BooleanClause(randomParentQuery, BooleanClause.Occur.SHOULD));
    return childrenQueryWithRandomParent.build();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term)

Example 79 with BooleanClause

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

the class TestBlockJoinValidation method createParentsQueryWithOneChild.

private static Query createParentsQueryWithOneChild(int randomChildNumber) {
    BooleanQuery.Builder childQueryWithRandomParent = new BooleanQuery.Builder();
    Query parentsQuery = new TermQuery(new Term("parent", createFieldValue(getRandomParentNumber())));
    childQueryWithRandomParent.add(new BooleanClause(parentsQuery, BooleanClause.Occur.SHOULD));
    childQueryWithRandomParent.add(new BooleanClause(randomChildQuery(randomChildNumber), BooleanClause.Occur.SHOULD));
    return childQueryWithRandomParent.build();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term)

Example 80 with BooleanClause

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

the class QueryParser method Query.

public final Query Query(String field) throws ParseException, SyntaxError {
    List<BooleanClause> clauses = new ArrayList<BooleanClause>();
    Query q;
    int conj, mods;
    if (jj_2_1(2)) {
        MultiTerm(field, clauses);
    } else {
        switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
            case NOT:
            case PLUS:
            case MINUS:
            case BAREOPER:
            case LPAREN:
            case STAR:
            case QUOTED:
            case TERM:
            case PREFIXTERM:
            case WILDTERM:
            case REGEXPTERM:
            case RANGEIN_START:
            case RANGEEX_START:
            case LPARAMS:
            case FILTER:
            case NUMBER:
                mods = Modifiers();
                q = Clause(field);
                addClause(clauses, CONJ_NONE, mods, q);
                break;
            default:
                jj_la1[4] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
    }
    label_1: while (true) {
        switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
            case AND:
            case OR:
            case NOT:
            case PLUS:
            case MINUS:
            case BAREOPER:
            case LPAREN:
            case STAR:
            case QUOTED:
            case TERM:
            case PREFIXTERM:
            case WILDTERM:
            case REGEXPTERM:
            case RANGEIN_START:
            case RANGEEX_START:
            case LPARAMS:
            case FILTER:
            case NUMBER:
                ;
                break;
            default:
                jj_la1[5] = jj_gen;
                break label_1;
        }
        if (jj_2_2(2)) {
            MultiTerm(field, clauses);
        } else {
            switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                case AND:
                case OR:
                case NOT:
                case PLUS:
                case MINUS:
                case BAREOPER:
                case LPAREN:
                case STAR:
                case QUOTED:
                case TERM:
                case PREFIXTERM:
                case WILDTERM:
                case REGEXPTERM:
                case RANGEIN_START:
                case RANGEEX_START:
                case LPARAMS:
                case FILTER:
                case NUMBER:
                    conj = Conjunction();
                    mods = Modifiers();
                    q = Clause(field);
                    addClause(clauses, conj, mods, q);
                    break;
                default:
                    jj_la1[6] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
        }
    }
    if (clauses.size() == 1 && clauses.get(0).getOccur() == BooleanClause.Occur.SHOULD) {
        Query firstQuery = clauses.get(0).getQuery();
        if (!(firstQuery instanceof RawQuery) || ((RawQuery) firstQuery).getTermCount() == 1) {
            {
                if (true)
                    return rawToNormal(firstQuery);
            }
        }
    }
    {
        if (true)
            return getBooleanQuery(clauses);
    }
    throw new Error("Missing return statement in function");
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) ArrayList(java.util.ArrayList) SyntaxError(org.apache.solr.search.SyntaxError)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)102 BooleanQuery (org.apache.lucene.search.BooleanQuery)92 Query (org.apache.lucene.search.Query)56 TermQuery (org.apache.lucene.search.TermQuery)46 Term (org.apache.lucene.index.Term)44 BoostQuery (org.apache.lucene.search.BoostQuery)33 ArrayList (java.util.ArrayList)23 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)14 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13 PrefixQuery (org.apache.lucene.search.PrefixQuery)13