use of org.apache.solr.search.SyntaxError in project lucene-solr by apache.
the class QueryParser method TopLevelQuery.
// This makes sure that there is no garbage after the query string
public final Query TopLevelQuery(String field) throws ParseException, SyntaxError {
Query q;
q = Query(field);
jj_consume_token(0);
{
if (true)
return q;
}
throw new Error("Missing return statement in function");
}
use of org.apache.solr.search.SyntaxError 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");
}
use of org.apache.solr.search.SyntaxError in project lucene-solr by apache.
the class SolrQueryParserBase method getBooleanQuery.
/**
* Factory method for generating query, given a set of clauses.
* By default creates a boolean query composed of clauses passed in.
*
* Can be overridden by extending classes, to modify query being
* returned.
*
* @param clauses List that contains {@link org.apache.lucene.search.BooleanClause} instances
* to join.
*
* @return Resulting {@link org.apache.lucene.search.Query} object.
*/
protected Query getBooleanQuery(List<BooleanClause> clauses) throws SyntaxError {
if (clauses.size() == 0) {
// all clause words were filtered away by the analyzer.
return null;
}
SchemaField sfield = null;
List<RawQuery> fieldValues = null;
boolean onlyRawQueries = true;
int allRawQueriesTermCount = 0;
for (BooleanClause clause : clauses) {
if (clause.getQuery() instanceof RawQuery) {
allRawQueriesTermCount += ((RawQuery) clause.getQuery()).getTermCount();
} else {
onlyRawQueries = false;
}
}
boolean useTermsQuery = (flags & QParser.FLAG_FILTER) != 0 && allRawQueriesTermCount > TERMS_QUERY_THRESHOLD;
BooleanQuery.Builder booleanBuilder = newBooleanQuery();
Map<SchemaField, List<RawQuery>> fmap = new HashMap<>();
for (BooleanClause clause : clauses) {
Query subq = clause.getQuery();
if (subq instanceof RawQuery) {
if (clause.getOccur() != BooleanClause.Occur.SHOULD) {
// We only collect optional terms for set queries. Since this isn't optional,
// convert the raw query to a normal query and handle as usual.
clause = new BooleanClause(rawToNormal(subq), clause.getOccur());
} else {
// Optional raw query.
RawQuery rawq = (RawQuery) subq;
// only look up fmap and type info on a field change
if (sfield != rawq.sfield) {
sfield = rawq.sfield;
fieldValues = fmap.get(sfield);
// the "useTermQuery" check.
if ((fieldValues == null && useTermsQuery) || !sfield.indexed()) {
fieldValues = new ArrayList<>(2);
fmap.put(sfield, fieldValues);
}
}
if (fieldValues != null) {
fieldValues.add(rawq);
continue;
}
clause = new BooleanClause(rawToNormal(subq), clause.getOccur());
}
}
booleanBuilder.add(clause);
}
for (Map.Entry<SchemaField, List<RawQuery>> entry : fmap.entrySet()) {
sfield = entry.getKey();
fieldValues = entry.getValue();
FieldType ft = sfield.getType();
// TODO: pull more of this logic out to FieldType? We would need to be able to add clauses to our existing booleanBuilder.
int termCount = fieldValues.stream().mapToInt(RawQuery::getTermCount).sum();
if ((sfield.indexed() && termCount < TERMS_QUERY_THRESHOLD) || termCount == 1) {
// use boolean query instead
for (RawQuery rawq : fieldValues) {
Query subq;
if (ft.isTokenized() && sfield.indexed()) {
boolean fieldAutoGenPhraseQueries = ft instanceof TextField && ((TextField) ft).getAutoGeneratePhraseQueries();
boolean fieldEnableGraphQueries = ft instanceof TextField && ((TextField) ft).getEnableGraphQueries();
subq = newFieldQuery(getAnalyzer(), sfield.getName(), rawq.getJoinedExternalVal(), false, fieldAutoGenPhraseQueries, fieldEnableGraphQueries);
booleanBuilder.add(subq, BooleanClause.Occur.SHOULD);
} else {
for (String externalVal : rawq.getExternalVals()) {
subq = ft.getFieldQuery(this.parser, sfield, externalVal);
booleanBuilder.add(subq, BooleanClause.Occur.SHOULD);
}
}
}
} else {
List<String> externalVals = fieldValues.stream().flatMap(rawq -> rawq.getExternalVals().stream()).collect(Collectors.toList());
Query subq = ft.getSetQuery(this.parser, sfield, externalVals);
// if this is everything, don't wrap in a boolean query
if (onlyRawQueries && termCount == allRawQueriesTermCount)
return subq;
booleanBuilder.add(subq, BooleanClause.Occur.SHOULD);
}
}
BooleanQuery bq = booleanBuilder.build();
if (bq.clauses().size() == 1) {
// Unwrap single SHOULD query
BooleanClause clause = bq.clauses().iterator().next();
if (clause.getOccur() == BooleanClause.Occur.SHOULD) {
return clause.getQuery();
}
}
return bq;
}
use of org.apache.solr.search.SyntaxError in project lucene-solr by apache.
the class MacroExpander method _expand.
private String _expand(String val) {
// quickest short circuit
int idx = val.indexOf(macroStart.charAt(0));
if (idx < 0)
return val;
// start of the unprocessed part of the string
int start = 0;
StringBuilder sb = null;
for (; ; ) {
idx = val.indexOf(macroStart, idx);
int matchedStart = idx;
// check if escaped
if (idx > 0) {
// check if escaped...
// TODO: what if you *want* to actually have a backslash... perhaps that's when we allow changing
// of the escape character?
char ch = val.charAt(idx - 1);
if (ch == escape) {
idx += macroStart.length();
continue;
}
} else if (idx < 0) {
if (sb == null)
return val;
sb.append(val.substring(start));
return sb.toString();
}
// found unescaped "${"
idx += macroStart.length();
int rbrace = val.indexOf('}', idx);
if (rbrace == -1) {
// no matching close brace...
continue;
}
if (sb == null) {
sb = new StringBuilder(val.length() * 2);
}
if (matchedStart > 0) {
sb.append(val.substring(start, matchedStart));
}
// update "start" to be at the end of ${...}
start = rbrace + 1;
// String inbetween = val.substring(idx, rbrace);
StrParser parser = new StrParser(val, idx, rbrace);
try {
String paramName = parser.getId();
String defVal = null;
boolean hasDefault = parser.opt(':');
if (hasDefault) {
defVal = val.substring(parser.pos, rbrace);
}
// in the event that expansions become context dependent... consult original?
String[] replacementList = orig.get(paramName);
// TODO - handle a list somehow...
String replacement = replacementList != null ? replacementList[0] : defVal;
if (replacement != null) {
String expandedReplacement = expand(replacement);
if (failOnMissingParams && expandedReplacement == null) {
return null;
}
sb.append(expandedReplacement);
} else if (failOnMissingParams) {
return null;
}
} catch (SyntaxError syntaxError) {
// append the part we would have skipped
sb.append(val.substring(matchedStart, start));
continue;
}
}
}
use of org.apache.solr.search.SyntaxError in project lucene-solr by apache.
the class ChildDocTransformer method create.
@Override
public DocTransformer create(String field, SolrParams params, SolrQueryRequest req) {
SchemaField uniqueKeyField = req.getSchema().getUniqueKeyField();
if (uniqueKeyField == null) {
throw new SolrException(ErrorCode.BAD_REQUEST, " ChildDocTransformer requires the schema to have a uniqueKeyField.");
}
String parentFilter = params.get("parentFilter");
if (parentFilter == null) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Parent filter should be sent as parentFilter=filterCondition");
}
String childFilter = params.get("childFilter");
int limit = params.getInt("limit", 10);
BitSetProducer parentsFilter = null;
try {
Query parentFilterQuery = QParser.getParser(parentFilter, req).getQuery();
parentsFilter = new QueryBitSetProducer(new QueryWrapperFilter(parentFilterQuery));
} catch (SyntaxError syntaxError) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Failed to create correct parent filter query");
}
Query childFilterQuery = null;
if (childFilter != null) {
try {
childFilterQuery = QParser.getParser(childFilter, req).getQuery();
} catch (SyntaxError syntaxError) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Failed to create correct child filter query");
}
}
return new ChildDocTransformer(field, parentsFilter, uniqueKeyField, req.getSchema(), childFilterQuery, limit);
}
Aggregations