Search in sources :

Example 26 with SyntaxError

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");
}
Also used : Query(org.apache.lucene.search.Query) SyntaxError(org.apache.solr.search.SyntaxError)

Example 27 with SyntaxError

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");
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) ArrayList(java.util.ArrayList) SyntaxError(org.apache.solr.search.SyntaxError)

Example 28 with SyntaxError

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;
}
Also used : Query(org.apache.lucene.search.Query) QParser(org.apache.solr.search.QParser) AutomatonQuery(org.apache.lucene.search.AutomatonQuery) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) FieldType(org.apache.solr.schema.FieldType) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryBuilder(org.apache.lucene.util.QueryBuilder) SolrException(org.apache.solr.common.SolrException) SchemaField(org.apache.solr.schema.SchemaField) RegexpQuery(org.apache.lucene.search.RegexpQuery) Operations(org.apache.lucene.util.automaton.Operations) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SyntaxError(org.apache.solr.search.SyntaxError) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Map(java.util.Map) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) EnumSet(java.util.EnumSet) Automata(org.apache.lucene.util.automaton.Automata) ReversedWildcardFilterFactory(org.apache.solr.analysis.ReversedWildcardFilterFactory) TokenizerChain(org.apache.solr.analysis.TokenizerChain) TextField(org.apache.solr.schema.TextField) Automaton(org.apache.lucene.util.automaton.Automaton) Analyzer(org.apache.lucene.analysis.Analyzer) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Collectors(java.util.stream.Collectors) WildcardQuery(org.apache.lucene.search.WildcardQuery) BooleanClause(org.apache.lucene.search.BooleanClause) IndexSchema(org.apache.solr.schema.IndexSchema) List(java.util.List) FilterQuery(org.apache.solr.query.FilterQuery) StringReader(java.io.StringReader) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TokenFilterFactory(org.apache.lucene.analysis.util.TokenFilterFactory) Collections(java.util.Collections) ReverseStringFilter(org.apache.lucene.analysis.reverse.ReverseStringFilter) Operator(org.apache.solr.parser.QueryParser.Operator) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) AutomatonQuery(org.apache.lucene.search.AutomatonQuery) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) BooleanClause(org.apache.lucene.search.BooleanClause) TextField(org.apache.solr.schema.TextField) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with SyntaxError

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;
        }
    }
}
Also used : SyntaxError(org.apache.solr.search.SyntaxError) StrParser(org.apache.solr.search.StrParser)

Example 30 with SyntaxError

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);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) Query(org.apache.lucene.search.Query) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) SyntaxError(org.apache.solr.search.SyntaxError) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) QueryWrapperFilter(org.apache.solr.search.QueryWrapperFilter) SolrException(org.apache.solr.common.SolrException)

Aggregations

SyntaxError (org.apache.solr.search.SyntaxError)35 SolrException (org.apache.solr.common.SolrException)23 Query (org.apache.lucene.search.Query)21 QParser (org.apache.solr.search.QParser)12 ArrayList (java.util.ArrayList)10 SchemaField (org.apache.solr.schema.SchemaField)10 BooleanQuery (org.apache.lucene.search.BooleanQuery)8 SolrParams (org.apache.solr.common.params.SolrParams)8 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)6 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)6 IOException (java.io.IOException)5 List (java.util.List)5 NamedList (org.apache.solr.common.util.NamedList)5 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)5 Map (java.util.Map)4 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)4 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)4 FieldType (org.apache.solr.schema.FieldType)4 IndexSchema (org.apache.solr.schema.IndexSchema)4 DocList (org.apache.solr.search.DocList)4