Search in sources :

Example 26 with SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class BaseHandlerApiSupport method getApi.

private Api getApi(final V2EndPoint op) {
    final BaseHandlerApiSupport apiHandler = this;
    return new Api(ApiBag.getSpec(op.getSpecName())) {

        @Override
        public void call(SolrQueryRequest req, SolrQueryResponse rsp) {
            SolrParams params = req.getParams();
            SolrRequest.METHOD method = SolrRequest.METHOD.valueOf(req.getHttpMethod());
            List<ApiCommand> commands = commandsMapping.get(method).get(op);
            try {
                if (method == POST) {
                    List<CommandOperation> cmds = req.getCommands(true);
                    if (cmds.size() > 1)
                        throw new SolrException(BAD_REQUEST, "Only one command is allowed");
                    CommandOperation c = cmds.size() == 0 ? null : cmds.get(0);
                    ApiCommand command = null;
                    String commandName = c == null ? null : c.name;
                    for (ApiCommand cmd : commands) {
                        if (Objects.equals(cmd.meta().getName(), commandName)) {
                            command = cmd;
                            break;
                        }
                    }
                    if (command == null) {
                        throw new SolrException(BAD_REQUEST, " no such command " + c);
                    }
                    wrapParams(req, c, command, false);
                    command.invoke(req, rsp, apiHandler);
                } else {
                    if (commands == null || commands.isEmpty()) {
                        rsp.add("error", "No support for : " + method + " at :" + req.getPath());
                        return;
                    }
                    if (commands.size() > 1) {
                        for (ApiCommand command : commands) {
                            if (command.meta().getName().equals(req.getPath())) {
                                commands = Collections.singletonList(command);
                                break;
                            }
                        }
                    }
                    wrapParams(req, new CommandOperation("", Collections.EMPTY_MAP), commands.get(0), true);
                    commands.get(0).invoke(req, rsp, apiHandler);
                }
            } catch (SolrException e) {
                throw e;
            } catch (Exception e) {
                throw new SolrException(BAD_REQUEST, e);
            } finally {
                req.setParams(params);
            }
        }
    };
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) CommandOperation(org.apache.solr.common.util.CommandOperation) SolrRequest(org.apache.solr.client.solrj.SolrRequest) SolrException(org.apache.solr.common.SolrException) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) Api(org.apache.solr.api.Api) SolrException(org.apache.solr.common.SolrException)

Example 27 with SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class DataImportHandler method handleRequestBody.

@Override
@SuppressWarnings("unchecked")
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    rsp.setHttpCaching(false);
    //TODO: figure out why just the first one is OK...
    ContentStream contentStream = null;
    Iterable<ContentStream> streams = req.getContentStreams();
    if (streams != null) {
        for (ContentStream stream : streams) {
            contentStream = stream;
            break;
        }
    }
    SolrParams params = req.getParams();
    NamedList defaultParams = (NamedList) initArgs.get("defaults");
    RequestInfo requestParams = new RequestInfo(req, getParamsMap(params), contentStream);
    String command = requestParams.getCommand();
    if (DataImporter.SHOW_CONF_CMD.equals(command)) {
        String dataConfigFile = params.get("config");
        String dataConfig = params.get("dataConfig");
        if (dataConfigFile != null) {
            dataConfig = SolrWriter.getResourceAsString(req.getCore().getResourceLoader().openResource(dataConfigFile));
        }
        if (dataConfig == null) {
            rsp.add("status", DataImporter.MSG.NO_CONFIG_FOUND);
        } else {
            // Modify incoming request params to add wt=raw
            ModifiableSolrParams rawParams = new ModifiableSolrParams(req.getParams());
            rawParams.set(CommonParams.WT, "raw");
            req.setParams(rawParams);
            ContentStreamBase content = new ContentStreamBase.StringStream(dataConfig);
            rsp.add(RawResponseWriter.CONTENT, content);
        }
        return;
    }
    rsp.add("initArgs", initArgs);
    String message = "";
    if (command != null) {
        rsp.add("command", command);
    }
    // If importer is still null
    if (importer == null) {
        rsp.add("status", DataImporter.MSG.NO_INIT);
        return;
    }
    if (command != null && DataImporter.ABORT_CMD.equals(command)) {
        importer.runCmd(requestParams, null);
    } else if (importer.isBusy()) {
        message = DataImporter.MSG.CMD_RUNNING;
    } else if (command != null) {
        if (DataImporter.FULL_IMPORT_CMD.equals(command) || DataImporter.DELTA_IMPORT_CMD.equals(command) || IMPORT_CMD.equals(command)) {
            importer.maybeReloadConfiguration(requestParams, defaultParams);
            UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessorChain(params);
            UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
            SolrResourceLoader loader = req.getCore().getResourceLoader();
            DIHWriter sw = getSolrWriter(processor, loader, requestParams, req);
            if (requestParams.isDebug()) {
                if (debugEnabled) {
                    // Synchronous request for the debug mode
                    importer.runCmd(requestParams, sw);
                    rsp.add("mode", "debug");
                    rsp.add("documents", requestParams.getDebugInfo().debugDocuments);
                    if (requestParams.getDebugInfo().debugVerboseOutput != null) {
                        rsp.add("verbose-output", requestParams.getDebugInfo().debugVerboseOutput);
                    }
                } else {
                    message = DataImporter.MSG.DEBUG_NOT_ENABLED;
                }
            } else {
                // Asynchronous request for normal mode
                if (requestParams.getContentStream() == null && !requestParams.isSyncMode()) {
                    importer.runAsync(requestParams, sw);
                } else {
                    importer.runCmd(requestParams, sw);
                }
            }
        } else if (DataImporter.RELOAD_CONF_CMD.equals(command)) {
            if (importer.maybeReloadConfiguration(requestParams, defaultParams)) {
                message = DataImporter.MSG.CONFIG_RELOADED;
            } else {
                message = DataImporter.MSG.CONFIG_NOT_RELOADED;
            }
        }
    }
    rsp.add("status", importer.isBusy() ? "busy" : "idle");
    rsp.add("importResponse", message);
    rsp.add("statusMessages", importer.getStatusMessages());
}
Also used : NamedList(org.apache.solr.common.util.NamedList) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) ContentStream(org.apache.solr.common.util.ContentStream) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 28 with SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class DataImportHandler method getSolrWriter.

private DIHWriter getSolrWriter(final UpdateRequestProcessor processor, final SolrResourceLoader loader, final RequestInfo requestParams, SolrQueryRequest req) {
    SolrParams reqParams = req.getParams();
    String writerClassStr = null;
    if (reqParams != null && reqParams.get(PARAM_WRITER_IMPL) != null) {
        writerClassStr = (String) reqParams.get(PARAM_WRITER_IMPL);
    }
    DIHWriter writer;
    if (writerClassStr != null && !writerClassStr.equals(DEFAULT_WRITER_NAME) && !writerClassStr.equals(DocBuilder.class.getPackage().getName() + "." + DEFAULT_WRITER_NAME)) {
        try {
            @SuppressWarnings("unchecked") Class<DIHWriter> writerClass = DocBuilder.loadClass(writerClassStr, req.getCore());
            Constructor<DIHWriter> cnstr = writerClass.getConstructor(new Class[] { UpdateRequestProcessor.class, SolrQueryRequest.class });
            return cnstr.newInstance((Object) processor, (Object) req);
        } catch (Exception e) {
            throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Unable to load Writer implementation:" + writerClassStr, e);
        }
    } else {
        return new SolrWriter(processor, req) {

            @Override
            public boolean upload(SolrInputDocument document) {
                try {
                    return super.upload(document);
                } catch (RuntimeException e) {
                    LOG.error("Exception while adding: " + document, e);
                    return false;
                }
            }
        };
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 29 with SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class FieldAnalysisRequestHandler method resolveAnalysisRequest.

// ================================================= Helper methods ================================================
/**
   * Resolves the AnalysisRequest based on the parameters in the given SolrParams.
   *
   * @param req the request
   *
   * @return AnalysisRequest containing all the information about what needs to be analyzed, and using what
   *         fields/types
   */
FieldAnalysisRequest resolveAnalysisRequest(SolrQueryRequest req) throws SolrException {
    SolrParams solrParams = req.getParams();
    FieldAnalysisRequest analysisRequest = new FieldAnalysisRequest();
    boolean useDefaultSearchField = true;
    if (solrParams.get(AnalysisParams.FIELD_TYPE) != null) {
        analysisRequest.setFieldTypes(Arrays.asList(solrParams.get(AnalysisParams.FIELD_TYPE).split(",")));
        useDefaultSearchField = false;
    }
    if (solrParams.get(AnalysisParams.FIELD_NAME) != null) {
        analysisRequest.setFieldNames(Arrays.asList(solrParams.get(AnalysisParams.FIELD_NAME).split(",")));
        useDefaultSearchField = false;
    }
    if (useDefaultSearchField) {
        if (solrParams.get(CommonParams.DF) != null) {
            analysisRequest.addFieldName(solrParams.get(CommonParams.DF));
        } else {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Field analysis request must contain one of analysis.fieldtype, analysis.fieldname or df.");
        }
    }
    analysisRequest.setQuery(solrParams.get(AnalysisParams.QUERY, solrParams.get(CommonParams.Q)));
    String value = solrParams.get(AnalysisParams.FIELD_VALUE);
    if (analysisRequest.getQuery() == null && value == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "One of analysis.fieldvalue, q, or analysis.query parameters must be specified");
    }
    Iterable<ContentStream> streams = req.getContentStreams();
    if (streams != null) {
        // NOTE: Only the first content stream is currently processed
        for (ContentStream stream : streams) {
            Reader reader = null;
            try {
                reader = stream.getReader();
                value = IOUtils.toString(reader);
            } catch (IOException e) {
            // do nothing, leave value set to the request parameter
            } finally {
                IOUtils.closeQuietly(reader);
            }
            break;
        }
    }
    analysisRequest.setFieldValue(value);
    analysisRequest.setShowMatch(solrParams.getBool(AnalysisParams.SHOW_MATCH, false));
    return analysisRequest;
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrParams(org.apache.solr.common.params.SolrParams) Reader(java.io.Reader) IOException(java.io.IOException) FieldAnalysisRequest(org.apache.solr.client.solrj.request.FieldAnalysisRequest) SolrException(org.apache.solr.common.SolrException)

Example 30 with SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class DocumentAnalysisRequestHandler method resolveAnalysisRequest.

//================================================ Helper Methods ==================================================
/**
   * Resolves the {@link DocumentAnalysisRequest} from the given solr request.
   *
   * @param req The solr request.
   *
   * @return The resolved document analysis request.
   *
   * @throws IOException        Thrown when reading/parsing the content stream of the request fails.
   * @throws XMLStreamException Thrown when reading/parsing the content stream of the request fails.
   */
DocumentAnalysisRequest resolveAnalysisRequest(SolrQueryRequest req) throws IOException, XMLStreamException {
    DocumentAnalysisRequest request = new DocumentAnalysisRequest();
    SolrParams params = req.getParams();
    String query = params.get(AnalysisParams.QUERY, params.get(CommonParams.Q, null));
    request.setQuery(query);
    boolean showMatch = params.getBool(AnalysisParams.SHOW_MATCH, false);
    request.setShowMatch(showMatch);
    ContentStream stream = extractSingleContentStream(req);
    InputStream is = null;
    XMLStreamReader parser = null;
    try {
        is = stream.getStream();
        final String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
        parser = (charset == null) ? inputFactory.createXMLStreamReader(is) : inputFactory.createXMLStreamReader(is, charset);
        while (true) {
            int event = parser.next();
            switch(event) {
                case XMLStreamConstants.END_DOCUMENT:
                    {
                        parser.close();
                        return request;
                    }
                case XMLStreamConstants.START_ELEMENT:
                    {
                        String currTag = parser.getLocalName();
                        if ("doc".equals(currTag)) {
                            log.trace("Reading doc...");
                            SolrInputDocument document = readDocument(parser, req.getSchema());
                            request.addDocument(document);
                        }
                        break;
                    }
            }
        }
    } finally {
        if (parser != null)
            parser.close();
        IOUtils.closeQuietly(is);
    }
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrInputDocument(org.apache.solr.common.SolrInputDocument) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) SolrParams(org.apache.solr.common.params.SolrParams) DocumentAnalysisRequest(org.apache.solr.client.solrj.request.DocumentAnalysisRequest)

Aggregations

SolrParams (org.apache.solr.common.params.SolrParams)310 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)179 SolrException (org.apache.solr.common.SolrException)78 Test (org.junit.Test)45 Tuple (org.apache.solr.client.solrj.io.Tuple)43 SolrDocument (org.apache.solr.common.SolrDocument)42 ArrayList (java.util.ArrayList)41 NamedList (org.apache.solr.common.util.NamedList)40 MapSolrParams (org.apache.solr.common.params.MapSolrParams)37 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)37 IOException (java.io.IOException)35 SolrDocumentList (org.apache.solr.common.SolrDocumentList)34 HashMap (java.util.HashMap)33 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)30 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)27 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)26 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)24 Map (java.util.Map)22 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)22 SolrCore (org.apache.solr.core.SolrCore)20