Search in sources :

Example 11 with SyntaxError

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

the class FacetProcessor method evalFilters.

private void evalFilters() throws IOException {
    if (freq.domain.filters == null || freq.domain.filters.isEmpty())
        return;
    List<Query> qlist = new ArrayList<>(freq.domain.filters.size());
    // TODO: prevent parsing filters each time!
    for (Object rawFilter : freq.domain.filters) {
        if (rawFilter instanceof String) {
            QParser parser = null;
            try {
                parser = QParser.getParser((String) rawFilter, fcontext.req);
                parser.setIsFilter(true);
                Query symbolicFilter = parser.getQuery();
                qlist.add(symbolicFilter);
            } catch (SyntaxError syntaxError) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, syntaxError);
            }
        } else if (rawFilter instanceof Map) {
            Map<String, Object> m = (Map<String, Object>) rawFilter;
            String type;
            Object args;
            if (m.size() == 1) {
                Map.Entry<String, Object> entry = m.entrySet().iterator().next();
                type = entry.getKey();
                args = entry.getValue();
            } else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't convert map to query:" + rawFilter);
            }
            if (!"param".equals(type)) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown type. Can't convert map to query:" + rawFilter);
            }
            String tag;
            if (!(args instanceof String)) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't retrieve non-string param:" + args);
            }
            tag = (String) args;
            String[] qstrings = fcontext.req.getParams().getParams(tag);
            if (qstrings != null) {
                for (String qstring : qstrings) {
                    QParser parser = null;
                    try {
                        parser = QParser.getParser((String) qstring, fcontext.req);
                        parser.setIsFilter(true);
                        Query symbolicFilter = parser.getQuery();
                        qlist.add(symbolicFilter);
                    } catch (SyntaxError syntaxError) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, syntaxError);
                    }
                }
            }
        } else {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Bad query (expected a string):" + rawFilter);
        }
    }
    this.filter = fcontext.searcher.getDocSet(qlist);
}
Also used : Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) SyntaxError(org.apache.solr.search.SyntaxError) ArrayList(java.util.ArrayList) QParser(org.apache.solr.search.QParser) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) SolrException(org.apache.solr.common.SolrException)

Example 12 with SyntaxError

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

the class DirectUpdateHandler2 method getQuery.

private Query getQuery(DeleteUpdateCommand cmd) {
    Query q;
    try {
        // move this higher in the stack?
        QParser parser = QParser.getParser(cmd.getQuery(), cmd.req);
        q = parser.getQuery();
        q = QueryUtils.makeQueryable(q);
        // Make sure not to delete newer versions
        if (ulog != null && cmd.getVersion() != 0 && cmd.getVersion() != -Long.MAX_VALUE) {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(q, Occur.MUST);
            SchemaField sf = ulog.getVersionInfo().getVersionField();
            ValueSource vs = sf.getType().getValueSource(sf, null);
            ValueSourceRangeFilter filt = new ValueSourceRangeFilter(vs, Long.toString(Math.abs(cmd.getVersion())), null, true, true);
            FunctionRangeQuery range = new FunctionRangeQuery(filt);
            // formulated in the "MUST_NOT" sense so we can delete docs w/o a version (some tests depend on this...)
            bq.add(range, Occur.MUST_NOT);
            q = bq.build();
        }
        return q;
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ValueSourceRangeFilter(org.apache.solr.search.function.ValueSourceRangeFilter) SyntaxError(org.apache.solr.search.SyntaxError) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) SolrException(org.apache.solr.common.SolrException)

Example 13 with SyntaxError

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

the class FacetingAccumulator method processQueryFacets.

/**
   * Initiates the collecting of query facets
   * @param filter the base filter to work against
   * @throws IOException if searching failed
   */
public void processQueryFacets(final Filter filter) throws IOException {
    for (QueryFacetRequest qfr : queryFacets) {
        for (String query : qfr.getQueries()) {
            if (query.contains(AnalyticsParams.RESULT) && !query.contains(AnalyticsParams.QUERY_RESULT)) {
                try {
                    String[] pivotStr = ExpressionFactory.getArguments(query.substring(query.indexOf('(') + 1, query.lastIndexOf(')')).trim());
                    if (pivotStr.length == 1) {
                        query = getResult(pivotStr[0]);
                    } else if (pivotStr.length == 3) {
                        query = getResult(pivotStr[0], pivotStr[1], pivotStr[2]);
                    } else {
                        throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " has an invalid amount of arguments.");
                    }
                } catch (IndexOutOfBoundsException e) {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " is invalid. Lacks parentheses.", e);
                }
            } else if (query.contains(AnalyticsParams.QUERY_RESULT)) {
                try {
                    String[] pivotStr = ExpressionFactory.getArguments(query.substring(query.indexOf('(') + 1, query.lastIndexOf(')')).trim());
                    if (pivotStr.length == 3) {
                        query = getQueryResult(qfr.getName(), pivotStr[0], pivotStr[1], pivotStr[2]);
                    } else {
                        throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " has an invalid amount of arguments.");
                    }
                } catch (IndexOutOfBoundsException e) {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " is invalid. Lacks parentheses.", e);
                }
            }
            QueryFacetAccumulator qAcc = new QueryFacetAccumulator(this, qfr.getName(), query);
            final Query q;
            try {
                q = QParser.getParser(query, queryRequest).getQuery();
            } catch (SyntaxError e) {
                throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid query '" + query + "'", e);
            }
            // The searcher sends docIds to the QueryFacetAccumulator which forwards
            // them to <code>collectQuery()</code> in this class for collection.
            Query filtered = new BooleanQuery.Builder().add(q, Occur.MUST).add(filter, Occur.FILTER).build();
            searcher.search(filtered, qAcc);
            computeQueryFacet(qfr.getName());
            queryCount++;
        }
    }
}
Also used : QueryFacetAccumulator(org.apache.solr.analytics.accumulator.facet.QueryFacetAccumulator) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) SyntaxError(org.apache.solr.search.SyntaxError) QueryFacetRequest(org.apache.solr.analytics.request.QueryFacetRequest) SolrException(org.apache.solr.common.SolrException)

Example 14 with SyntaxError

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

the class HighlightComponent method prepare.

@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    rb.doHighlights = solrConfigHighlighter.isHighlightingEnabled(params);
    if (rb.doHighlights) {
        rb.setNeedDocList(true);
        String hlq = params.get(HighlightParams.Q);
        String hlparser = Objects.firstNonNull(params.get(HighlightParams.QPARSER), params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE));
        if (hlq != null) {
            try {
                QParser parser = QParser.getParser(hlq, hlparser, rb.req);
                rb.setHighlightQuery(parser.getHighlightQuery());
            } catch (SyntaxError e) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
            }
        }
    }
}
Also used : SyntaxError(org.apache.solr.search.SyntaxError) QParser(org.apache.solr.search.QParser) SolrParams(org.apache.solr.common.params.SolrParams) SolrException(org.apache.solr.common.SolrException)

Example 15 with SyntaxError

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

the class ClassificationUpdateProcessorFactory method getInstance.

@Override
public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
    String trainingFilterQueryString = (params.get(KNN_FILTER_QUERY));
    try {
        if (trainingFilterQueryString != null && !trainingFilterQueryString.isEmpty()) {
            Query trainingFilterQuery = this.parseFilterQuery(trainingFilterQueryString, params, req);
            classificationParams.setTrainingFilterQuery(trainingFilterQuery);
        }
    } catch (SyntaxError | RuntimeException syntaxError) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Classification UpdateProcessor Training Filter Query: '" + trainingFilterQueryString + "' is not supported", syntaxError);
    }
    IndexSchema schema = req.getSchema();
    IndexReader indexReader = req.getSearcher().getIndexReader();
    return new ClassificationUpdateProcessor(classificationParams, next, indexReader, schema);
}
Also used : Query(org.apache.lucene.search.Query) SyntaxError(org.apache.solr.search.SyntaxError) IndexReader(org.apache.lucene.index.IndexReader) IndexSchema(org.apache.solr.schema.IndexSchema) 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