Search in sources :

Example 16 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class UUIDUpdateProcessorFallbackTest method processAdd.

/**
   * Runs a document through the specified chain, and returns the final 
   * document used when the chain is completed (NOTE: some chains may 
   * modify the document in place
   */
SolrInputDocument processAdd(final String chain, final SolrInputDocument docIn) throws IOException {
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);
    assertNotNull("No Chain named: " + chain, pc);
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
    try {
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = docIn;
        UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
        processor.processAdd(cmd);
        return cmd.solrDoc;
    } finally {
        SolrRequestInfo.clearRequestInfo();
        req.close();
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrCore(org.apache.solr.core.SolrCore) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 17 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo 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 18 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class ReRankCollector method topDocs.

public TopDocs topDocs(int start, int howMany) {
    try {
        TopDocs mainDocs = mainCollector.topDocs(0, Math.max(reRankDocs, length));
        if (mainDocs.totalHits == 0 || mainDocs.scoreDocs.length == 0) {
            return mainDocs;
        }
        ScoreDoc[] mainScoreDocs = mainDocs.scoreDocs;
        ScoreDoc[] reRankScoreDocs = new ScoreDoc[Math.min(mainScoreDocs.length, reRankDocs)];
        System.arraycopy(mainScoreDocs, 0, reRankScoreDocs, 0, reRankScoreDocs.length);
        mainDocs.scoreDocs = reRankScoreDocs;
        TopDocs rescoredDocs = reRankQueryRescorer.rescore(searcher, mainDocs, mainDocs.scoreDocs.length);
        //Lower howMany to return if we've collected fewer documents.
        howMany = Math.min(howMany, mainScoreDocs.length);
        if (boostedPriority != null) {
            SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
            Map requestContext = null;
            if (info != null) {
                requestContext = info.getReq().getContext();
            }
            IntIntHashMap boostedDocs = QueryElevationComponent.getBoostDocs((SolrIndexSearcher) searcher, boostedPriority, requestContext);
            Arrays.sort(rescoredDocs.scoreDocs, new BoostedComp(boostedDocs, mainDocs.scoreDocs, rescoredDocs.getMaxScore()));
        }
        if (howMany == rescoredDocs.scoreDocs.length) {
            // Just return the rescoredDocs
            return rescoredDocs;
        } else if (howMany > rescoredDocs.scoreDocs.length) {
            //We need to return more then we've reRanked, so create the combined page.
            ScoreDoc[] scoreDocs = new ScoreDoc[howMany];
            //lay down the initial docs
            System.arraycopy(mainScoreDocs, 0, scoreDocs, 0, scoreDocs.length);
            //overlay the re-ranked docs.
            System.arraycopy(rescoredDocs.scoreDocs, 0, scoreDocs, 0, rescoredDocs.scoreDocs.length);
            rescoredDocs.scoreDocs = scoreDocs;
            return rescoredDocs;
        } else {
            //We've rescored more then we need to return.
            ScoreDoc[] scoreDocs = new ScoreDoc[howMany];
            System.arraycopy(rescoredDocs.scoreDocs, 0, scoreDocs, 0, howMany);
            rescoredDocs.scoreDocs = scoreDocs;
            return rescoredDocs;
        }
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) Map(java.util.Map) IntFloatHashMap(com.carrotsearch.hppc.IntFloatHashMap) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 19 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class QuerySenderListener method newSearcher.

@Override
public void newSearcher(SolrIndexSearcher newSearcher, SolrIndexSearcher currentSearcher) {
    final SolrIndexSearcher searcher = newSearcher;
    log.info("QuerySenderListener sending requests to " + newSearcher);
    List<NamedList> allLists = (List<NamedList>) getArgs().get("queries");
    if (allLists == null)
        return;
    boolean createNewReqInfo = SolrRequestInfo.getRequestInfo() == null;
    for (NamedList nlst : allLists) {
        SolrQueryRequest req = null;
        try {
            // bind the request to a particular searcher (the newSearcher)
            NamedList params = addEventParms(currentSearcher, nlst);
            // for this, we default to distrib = false
            if (params.get(DISTRIB) == null) {
                params.add(DISTRIB, false);
            }
            req = new LocalSolrQueryRequest(getCore(), params) {

                @Override
                public SolrIndexSearcher getSearcher() {
                    return searcher;
                }

                @Override
                public void close() {
                }
            };
            SolrQueryResponse rsp = new SolrQueryResponse();
            if (createNewReqInfo) {
                // SolrRequerstInfo for this thread could have been transferred from the parent
                // thread.
                SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
            }
            getCore().execute(getCore().getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
            // Retrieve the Document instances (not just the ids) to warm
            // the OS disk cache, and any Solr document cache.  Only the top
            // level values in the NamedList are checked for DocLists.
            NamedList values = rsp.getValues();
            for (int i = 0; i < values.size(); i++) {
                Object o = values.getVal(i);
                if (o instanceof ResultContext) {
                    o = ((ResultContext) o).getDocList();
                }
                if (o instanceof DocList) {
                    DocList docs = (DocList) o;
                    for (DocIterator iter = docs.iterator(); iter.hasNext(); ) {
                        newSearcher.doc(iter.nextDoc());
                    }
                }
            }
        } catch (Exception e) {
        // do nothing... we want to continue with the other requests.
        // the failure should have already been logged.
        } finally {
            if (req != null)
                req.close();
            if (createNewReqInfo)
                SolrRequestInfo.clearRequestInfo();
        }
    }
    log.info("QuerySenderListener done.");
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) DocIterator(org.apache.solr.search.DocIterator) NamedList(org.apache.solr.common.util.NamedList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) DocList(org.apache.solr.search.DocList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) DocList(org.apache.solr.search.DocList)

Example 20 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class DirectUpdateHandler2 method closeWriter.

// IndexWriterCloser interface method - called from solrCoreState.decref(this)
@Override
public void closeWriter(IndexWriter writer) throws IOException {
    assert TestInjection.injectNonGracefullClose(core.getCoreContainer());
    boolean clearRequestInfo = false;
    solrCoreState.getCommitLock().lock();
    try {
        SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
        SolrQueryResponse rsp = new SolrQueryResponse();
        if (SolrRequestInfo.getRequestInfo() == null) {
            clearRequestInfo = true;
            // important for debugging
            SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        }
        if (!commitOnClose) {
            if (writer != null) {
                writer.rollback();
            }
            // means we can't delete them on windows (needed for tests)
            if (ulog != null)
                ulog.close(false);
            return;
        }
        // do a commit before we quit?     
        boolean tryToCommit = writer != null && ulog != null && ulog.hasUncommittedChanges() && ulog.getState() == UpdateLog.State.ACTIVE;
        try {
            if (tryToCommit) {
                log.info("Committing on IndexWriter close.");
                CommitUpdateCommand cmd = new CommitUpdateCommand(req, false);
                cmd.openSearcher = false;
                cmd.waitSearcher = false;
                cmd.softCommit = false;
                synchronized (solrCoreState.getUpdateLock()) {
                    ulog.preCommit(cmd);
                }
                // todo: refactor this shared code (or figure out why a real CommitUpdateCommand can't be used)
                SolrIndexWriter.setCommitData(writer, cmd.getVersion());
                writer.commit();
                synchronized (solrCoreState.getUpdateLock()) {
                    ulog.postCommit(cmd);
                }
            }
        } catch (Throwable th) {
            log.error("Error in final commit", th);
            if (th instanceof OutOfMemoryError) {
                throw (OutOfMemoryError) th;
            }
        }
        // cap any ulog files.
        try {
            if (ulog != null)
                ulog.close(false);
        } catch (Throwable th) {
            log.error("Error closing log files", th);
            if (th instanceof OutOfMemoryError) {
                throw (OutOfMemoryError) th;
            }
        }
        if (writer != null) {
            writer.close();
        }
    } finally {
        solrCoreState.getCommitLock().unlock();
        if (clearRequestInfo)
            SolrRequestInfo.clearRequestInfo();
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)29 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)22 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)16 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)9 SolrCore (org.apache.solr.core.SolrCore)9 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)8 Map (java.util.Map)5 QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)5 Query (org.apache.lucene.search.Query)4 SolrException (org.apache.solr.common.SolrException)4 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)4 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)3 HashMap (java.util.HashMap)3 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)3 NamedList (org.apache.solr.common.util.NamedList)3 BinaryResponseWriter (org.apache.solr.response.BinaryResponseWriter)3 ResultContext (org.apache.solr.response.ResultContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2