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;
}
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() + ')');
}
}
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();
}
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();
}
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");
}
Aggregations