Search in sources :

Example 31 with SyntaxError

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

the class MoreLikeThisHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    SolrParams params = req.getParams();
    long timeAllowed = (long) params.getInt(CommonParams.TIME_ALLOWED, -1);
    if (timeAllowed > 0) {
        SolrQueryTimeoutImpl.set(timeAllowed);
    }
    try {
        // Set field flags
        ReturnFields returnFields = new SolrReturnFields(req);
        rsp.setReturnFields(returnFields);
        int flags = 0;
        if (returnFields.wantsScore()) {
            flags |= SolrIndexSearcher.GET_SCORES;
        }
        String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
        String q = params.get(CommonParams.Q);
        Query query = null;
        SortSpec sortSpec = null;
        List<Query> filters = null;
        try {
            if (q != null) {
                QParser parser = QParser.getParser(q, defType, req);
                query = parser.getQuery();
                sortSpec = parser.getSortSpec(true);
            }
            String[] fqs = req.getParams().getParams(CommonParams.FQ);
            if (fqs != null && fqs.length != 0) {
                filters = new ArrayList<>();
                for (String fq : fqs) {
                    if (fq != null && fq.trim().length() != 0) {
                        QParser fqp = QParser.getParser(fq, req);
                        filters.add(fqp.getQuery());
                    }
                }
            }
        } catch (SyntaxError e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }
        SolrIndexSearcher searcher = req.getSearcher();
        MoreLikeThisHelper mlt = new MoreLikeThisHelper(params, searcher);
        // Hold on to the interesting terms if relevant
        TermStyle termStyle = TermStyle.get(params.get(MoreLikeThisParams.INTERESTING_TERMS));
        List<InterestingTerm> interesting = (termStyle == TermStyle.NONE) ? null : new ArrayList<>(mlt.mlt.getMaxQueryTerms());
        DocListAndSet mltDocs = null;
        // Parse Required Params
        // This will either have a single Reader or valid query
        Reader reader = null;
        try {
            if (q == null || q.trim().length() < 1) {
                Iterable<ContentStream> streams = req.getContentStreams();
                if (streams != null) {
                    Iterator<ContentStream> iter = streams.iterator();
                    if (iter.hasNext()) {
                        reader = iter.next().getReader();
                    }
                    if (iter.hasNext()) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis does not support multiple ContentStreams");
                    }
                }
            }
            int start = params.getInt(CommonParams.START, CommonParams.START_DEFAULT);
            int rows = params.getInt(CommonParams.ROWS, CommonParams.ROWS_DEFAULT);
            // --------------------------------------------------------------------------------
            if (reader != null) {
                mltDocs = mlt.getMoreLikeThis(reader, start, rows, filters, interesting, flags);
            } else if (q != null) {
                // Matching options
                boolean includeMatch = params.getBool(MoreLikeThisParams.MATCH_INCLUDE, true);
                int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0);
                // Find the base match
                DocList match = searcher.getDocList(query, null, null, matchOffset, 1, // only get the first one...
                flags);
                if (includeMatch) {
                    rsp.add("match", match);
                }
                // This is an iterator, but we only handle the first match
                DocIterator iterator = match.iterator();
                if (iterator.hasNext()) {
                    // do a MoreLikeThis query for each document in results
                    int id = iterator.nextDoc();
                    mltDocs = mlt.getMoreLikeThis(id, start, rows, filters, interesting, flags);
                }
            } else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis requires either a query (?q=) or text to find similar documents.");
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        if (mltDocs == null) {
            // avoid NPE
            mltDocs = new DocListAndSet();
        }
        rsp.addResponse(mltDocs.docList);
        if (interesting != null) {
            if (termStyle == TermStyle.DETAILS) {
                NamedList<Float> it = new NamedList<>();
                for (InterestingTerm t : interesting) {
                    it.add(t.term.toString(), t.boost);
                }
                rsp.add("interestingTerms", it);
            } else {
                List<String> it = new ArrayList<>(interesting.size());
                for (InterestingTerm t : interesting) {
                    it.add(t.term.text());
                }
                rsp.add("interestingTerms", it);
            }
        }
        // maybe facet the results
        if (params.getBool(FacetParams.FACET, false)) {
            if (mltDocs.docSet == null) {
                rsp.add("facet_counts", null);
            } else {
                SimpleFacets f = new SimpleFacets(req, mltDocs.docSet, params);
                rsp.add("facet_counts", FacetComponent.getFacetCounts(f));
            }
        }
        boolean dbg = req.getParams().getBool(CommonParams.DEBUG_QUERY, false);
        boolean dbgQuery = false, dbgResults = false;
        if (dbg == false) {
            //if it's true, we are doing everything anyway.
            String[] dbgParams = req.getParams().getParams(CommonParams.DEBUG);
            if (dbgParams != null) {
                for (String dbgParam : dbgParams) {
                    if (dbgParam.equals(CommonParams.QUERY)) {
                        dbgQuery = true;
                    } else if (dbgParam.equals(CommonParams.RESULTS)) {
                        dbgResults = true;
                    }
                }
            }
        } else {
            dbgQuery = true;
            dbgResults = true;
        }
        // Copied from StandardRequestHandler... perhaps it should be added to doStandardDebug?
        if (dbg == true) {
            try {
                NamedList<Object> dbgInfo = SolrPluginUtils.doStandardDebug(req, q, mlt.getRawMLTQuery(), mltDocs.docList, dbgQuery, dbgResults);
                if (null != dbgInfo) {
                    if (null != filters) {
                        dbgInfo.add("filter_queries", req.getParams().getParams(CommonParams.FQ));
                        List<String> fqs = new ArrayList<>(filters.size());
                        for (Query fq : filters) {
                            fqs.add(QueryParsing.toString(fq, req.getSchema()));
                        }
                        dbgInfo.add("parsed_filter_queries", fqs);
                    }
                    rsp.add("debug", dbgInfo);
                }
            } catch (Exception e) {
                SolrException.log(log, "Exception during debug", e);
                rsp.add("exception_during_debug", SolrException.toStr(e));
            }
        }
    } catch (ExitableDirectoryReader.ExitingReaderException ex) {
        log.warn("Query: " + req.getParamString() + "; " + ex.getMessage());
    } finally {
        SolrQueryTimeoutImpl.reset();
    }
}
Also used : DocIterator(org.apache.solr.search.DocIterator) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DocListAndSet(org.apache.solr.search.DocListAndSet) ArrayList(java.util.ArrayList) ExitableDirectoryReader(org.apache.lucene.index.ExitableDirectoryReader) Reader(java.io.Reader) IndexReader(org.apache.lucene.index.IndexReader) ExitableDirectoryReader(org.apache.lucene.index.ExitableDirectoryReader) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ContentStream(org.apache.solr.common.util.ContentStream) SyntaxError(org.apache.solr.search.SyntaxError) TermStyle(org.apache.solr.common.params.MoreLikeThisParams.TermStyle) SolrException(org.apache.solr.common.SolrException) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) NamedList(org.apache.solr.common.util.NamedList) SimpleFacets(org.apache.solr.request.SimpleFacets) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) QParser(org.apache.solr.search.QParser) SolrParams(org.apache.solr.common.params.SolrParams) SortSpec(org.apache.solr.search.SortSpec) DocList(org.apache.solr.search.DocList)

Example 32 with SyntaxError

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

the class RequestHandlerBase method handleRequest.

@Override
public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
    requests.inc();
    Timer.Context timer = requestTimes.time();
    try {
        if (pluginInfo != null && pluginInfo.attributes.containsKey(USEPARAM))
            req.getContext().put(USEPARAM, pluginInfo.attributes.get(USEPARAM));
        SolrPluginUtils.setDefaults(this, req, defaults, appends, invariants);
        req.getContext().remove(USEPARAM);
        rsp.setHttpCaching(httpCaching);
        handleRequestBody(req, rsp);
        // count timeouts
        NamedList header = rsp.getResponseHeader();
        if (header != null) {
            Object partialResults = header.get(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY);
            boolean timedOut = partialResults == null ? false : (Boolean) partialResults;
            if (timedOut) {
                numTimeouts.mark();
                rsp.setHttpCaching(false);
            }
        }
    } catch (Exception e) {
        boolean incrementErrors = true;
        boolean isServerError = true;
        if (e instanceof SolrException) {
            SolrException se = (SolrException) e;
            if (se.code() == SolrException.ErrorCode.CONFLICT.code) {
                incrementErrors = false;
            } else if (se.code() >= 400 && se.code() < 500) {
                isServerError = false;
            }
        } else {
            if (e instanceof SyntaxError) {
                isServerError = false;
                e = new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
            }
        }
        rsp.setException(e);
        if (incrementErrors) {
            SolrException.log(log, e);
            numErrors.mark();
            if (isServerError) {
                numServerErrors.mark();
            } else {
                numClientErrors.mark();
            }
        }
    } finally {
        long elapsed = timer.stop();
        totalTime.inc(elapsed);
    }
}
Also used : Timer(com.codahale.metrics.Timer) SyntaxError(org.apache.solr.search.SyntaxError) NamedList(org.apache.solr.common.util.NamedList) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException)

Example 33 with SyntaxError

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

the class ExportWriter method write.

public void write(OutputStream os) throws IOException {
    QueryResponseWriter rw = req.getCore().getResponseWriters().get(wt);
    if (rw instanceof BinaryResponseWriter) {
        //todo add support for other writers after testing
        writer = new JavaBinCodec(os, null);
    } else {
        respWriter = new OutputStreamWriter(os, StandardCharsets.UTF_8);
        writer = JSONResponseWriter.getPushWriter(respWriter, req, res);
    }
    Exception exception = res.getException();
    if (exception != null) {
        if (!(exception instanceof IgnoreException)) {
            writeException(exception, writer, false);
        }
        return;
    }
    SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
    SortSpec sortSpec = info.getResponseBuilder().getSortSpec();
    if (sortSpec == null) {
        writeException((new IOException(new SyntaxError("No sort criteria was provided."))), writer, true);
        return;
    }
    SolrIndexSearcher searcher = req.getSearcher();
    Sort sort = searcher.weightSort(sortSpec.getSort());
    if (sort == null) {
        writeException((new IOException(new SyntaxError("No sort criteria was provided."))), writer, true);
        return;
    }
    if (sort != null && sort.needsScores()) {
        writeException((new IOException(new SyntaxError("Scoring is not currently supported with xsort."))), writer, true);
        return;
    }
    // This came to light in the very artifical case of indexing a single doc to Cloud.
    if (req.getContext().get("totalHits") != null) {
        totalHits = ((Integer) req.getContext().get("totalHits")).intValue();
        sets = (FixedBitSet[]) req.getContext().get("export");
        if (sets == null) {
            writeException((new IOException(new SyntaxError("xport RankQuery is required for xsort: rq={!xport}"))), writer, true);
            return;
        }
    }
    SolrParams params = req.getParams();
    String fl = params.get("fl");
    String[] fields = null;
    if (fl == null) {
        writeException((new IOException(new SyntaxError("export field list (fl) must be specified."))), writer, true);
        return;
    } else {
        fields = fl.split(",");
        for (int i = 0; i < fields.length; i++) {
            fields[i] = fields[i].trim();
            if (fields[i].equals("score")) {
                writeException((new IOException(new SyntaxError("Scoring is not currently supported with xsort."))), writer, true);
                return;
            }
        }
    }
    try {
        fieldWriters = getFieldWriters(fields, req.getSearcher());
    } catch (Exception e) {
        writeException(e, writer, true);
        return;
    }
    writer.writeMap(m -> {
        m.put("responseHeader", singletonMap("status", 0));
        m.put("response", (MapWriter) mw -> {
            mw.put("numFound", totalHits);
            mw.put("docs", (IteratorWriter) iw -> writeDocs(req, iw, sort));
        });
    });
}
Also used : BitSetIterator(org.apache.lucene.util.BitSetIterator) Date(java.util.Date) EntryWriter(org.apache.solr.common.MapWriter.EntryWriter) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IndexableField(org.apache.lucene.index.IndexableField) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) BoolField(org.apache.solr.schema.BoolField) StrField(org.apache.solr.schema.StrField) LoggerFactory(org.slf4j.LoggerFactory) LongValues(org.apache.lucene.util.LongValues) SolrParams(org.apache.solr.common.params.SolrParams) Collections.singletonList(java.util.Collections.singletonList) SolrException(org.apache.solr.common.SolrException) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortField(org.apache.lucene.search.SortField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) PrintWriter(java.io.PrintWriter) MapWriter(org.apache.solr.common.MapWriter) Sort(org.apache.lucene.search.Sort) BytesRef(org.apache.lucene.util.BytesRef) SolrCore(org.apache.solr.core.SolrCore) MethodHandles(java.lang.invoke.MethodHandles) StandardCharsets(java.nio.charset.StandardCharsets) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap) List(java.util.List) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LeafReader(org.apache.lucene.index.LeafReader) IteratorWriter(org.apache.solr.common.IteratorWriter) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) TrieDateField(org.apache.solr.schema.TrieDateField) PushWriter(org.apache.solr.common.PushWriter) NumericDocValues(org.apache.lucene.index.NumericDocValues) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) FieldType(org.apache.solr.schema.FieldType) FixedBitSet(org.apache.lucene.util.FixedBitSet) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) SortSpec(org.apache.solr.search.SortSpec) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) SchemaField(org.apache.solr.schema.SchemaField) TrieFloatField(org.apache.solr.schema.TrieFloatField) TrieLongField(org.apache.solr.schema.TrieLongField) SyntaxError(org.apache.solr.search.SyntaxError) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) TrieIntField(org.apache.solr.schema.TrieIntField) OutputStreamWriter(java.io.OutputStreamWriter) Collections.singletonMap(java.util.Collections.singletonMap) SortedDocValues(org.apache.lucene.index.SortedDocValues) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) OutputStream(java.io.OutputStream) ArrayUtil(org.apache.lucene.util.ArrayUtil) Logger(org.slf4j.Logger) MultiDocValues(org.apache.lucene.index.MultiDocValues) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) IOException(java.io.IOException) TrieDoubleField(org.apache.solr.schema.TrieDoubleField) IndexSchema(org.apache.solr.schema.IndexSchema) DocValues(org.apache.lucene.index.DocValues) Closeable(java.io.Closeable) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) IOException(java.io.IOException) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SyntaxError(org.apache.solr.search.SyntaxError) FixedBitSet(org.apache.lucene.util.FixedBitSet) IteratorWriter(org.apache.solr.common.IteratorWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) Sort(org.apache.lucene.search.Sort) SolrParams(org.apache.solr.common.params.SolrParams) OutputStreamWriter(java.io.OutputStreamWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) SortSpec(org.apache.solr.search.SortSpec)

Example 34 with SyntaxError

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

the class DateRangeField method getRangeQuery.

@Override
public Query getRangeQuery(QParser parser, SchemaField field, String startStr, String endStr, boolean minInclusive, boolean maxInclusive) {
    if (parser == null) {
        //null when invoked by SimpleFacets.  But getQueryFromSpatialArgs expects to get localParams.
        final SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();
        parser = new QParser("", null, requestInfo.getReq().getParams(), requestInfo.getReq()) {

            @Override
            public Query parse() throws SyntaxError {
                throw new IllegalStateException();
            }
        };
    }
    Calendar startCal;
    if (startStr == null) {
        startCal = tree.newCal();
    } else {
        startCal = parseCalendar(startStr);
        if (!minInclusive) {
            startCal.add(Calendar.MILLISECOND, 1);
        }
    }
    Calendar endCal;
    if (endStr == null) {
        endCal = tree.newCal();
    } else {
        endCal = parseCalendar(endStr);
        if (!maxInclusive) {
            endCal.add(Calendar.MILLISECOND, -1);
        }
    }
    Shape shape = tree.toRangeShape(tree.toShape(startCal), tree.toShape(endCal));
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, shape);
    return getQueryFromSpatialArgs(parser, field, spatialArgs);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) NRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.NRShape) Shape(org.locationtech.spatial4j.shape.Shape) UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) Query(org.apache.lucene.search.Query) SyntaxError(org.apache.solr.search.SyntaxError) Calendar(java.util.Calendar) QParser(org.apache.solr.search.QParser) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo)

Example 35 with SyntaxError

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

the class GeoDistValueSourceParser method parseSfield.

private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError {
    String sfield = fp.getParam(SpatialParams.FIELD);
    if (sfield == null)
        return null;
    SchemaField sf = fp.getReq().getSchema().getField(sfield);
    FieldType type = sf.getType();
    if (type instanceof AbstractSpatialFieldType) {
        AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
        return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield), asft.getDistanceUnits());
    }
    ValueSource vs = type.getValueSource(sf, fp);
    if (vs instanceof MultiValueSource) {
        return (MultiValueSource) vs;
    }
    throw new SyntaxError("Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SyntaxError(org.apache.solr.search.SyntaxError) ValueSource(org.apache.lucene.queries.function.ValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) FieldType(org.apache.solr.schema.FieldType) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType)

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