Search in sources :

Example 1 with SolrResponse

use of org.apache.solr.client.solrj.SolrResponse in project lucene-solr by apache.

the class VelocityResponseWriter method createContext.

private VelocityContext createContext(SolrQueryRequest request, SolrQueryResponse response) {
    VelocityContext context = new VelocityContext();
    // Register useful Velocity "tools"
    String locale = request.getParams().get(LOCALE);
    Map toolConfig = new HashMap();
    toolConfig.put("locale", locale);
    // TODO: add test; TODO: should this be overridable with a custom "log" named tool?
    context.put("log", log);
    context.put("esc", new EscapeTool());
    context.put("date", new ComparisonDateTool());
    context.put("list", new ListTool());
    context.put(SORT, new SortTool());
    MathTool mathTool = new MathTool();
    mathTool.configure(toolConfig);
    context.put("math", mathTool);
    NumberTool numberTool = new NumberTool();
    numberTool.configure(toolConfig);
    context.put("number", numberTool);
    DisplayTool displayTool = new DisplayTool();
    displayTool.configure(toolConfig);
    context.put("display", displayTool);
    ResourceTool resourceTool = new SolrVelocityResourceTool(request.getCore().getSolrConfig().getResourceLoader().getClassLoader());
    resourceTool.configure(toolConfig);
    context.put("resource", resourceTool);
    // Custom tools can override any of the built-in tools provided above, by registering one with the same name
    for (String name : customTools.keySet()) {
        Object customTool = SolrCore.createInstance(customTools.get(name), Object.class, "VrW custom tool: " + name, request.getCore(), request.getCore().getResourceLoader());
        if (customTool instanceof LocaleConfig) {
            ((LocaleConfig) customTool).configure(toolConfig);
        }
        context.put(name, customTool);
    }
    // custom tools _cannot_ override context objects added below, like $request and $response
    // TODO: at least log a warning when one of the *fixed* tools classes in name with a custom one, currently silently ignored
    // Turn the SolrQueryResponse into a SolrResponse.
    // QueryResponse has lots of conveniences suitable for a view
    // Problem is, which SolrResponse class to use?
    // One patch to SOLR-620 solved this by passing in a class name as
    // as a parameter and using reflection and Solr's class loader to
    // create a new instance.  But for now the implementation simply
    // uses QueryResponse, and if it chokes in a known way, fall back
    // to bare bones SolrResponseBase.
    // Can this writer know what the handler class is?  With echoHandler=true it can get its string name at least
    SolrResponse rsp = new QueryResponse();
    NamedList<Object> parsedResponse = BinaryResponseWriter.getParsedResponse(request, response);
    try {
        rsp.setResponse(parsedResponse);
        // page only injected if QueryResponse works
        // page tool only makes sense for a SearchHandler request
        context.put("page", new PageTool(request, response));
        context.put("debug", ((QueryResponse) rsp).getDebugMap());
    } catch (ClassCastException e) {
        // known edge case where QueryResponse's extraction assumes "response" is a SolrDocumentList
        // (AnalysisRequestHandler emits a "response")
        rsp = new SolrResponseBase();
        rsp.setResponse(parsedResponse);
    }
    context.put("request", request);
    context.put("response", rsp);
    return context;
}
Also used : DisplayTool(org.apache.velocity.tools.generic.DisplayTool) ResourceTool(org.apache.velocity.tools.generic.ResourceTool) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) LocaleConfig(org.apache.velocity.tools.generic.LocaleConfig) EscapeTool(org.apache.velocity.tools.generic.EscapeTool) MathTool(org.apache.velocity.tools.generic.MathTool) SolrResponse(org.apache.solr.client.solrj.SolrResponse) ComparisonDateTool(org.apache.velocity.tools.generic.ComparisonDateTool) NumberTool(org.apache.velocity.tools.generic.NumberTool) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrResponseBase(org.apache.solr.client.solrj.response.SolrResponseBase) ListTool(org.apache.velocity.tools.generic.ListTool) HashMap(java.util.HashMap) Map(java.util.Map) SortTool(org.apache.velocity.tools.generic.SortTool)

Example 2 with SolrResponse

use of org.apache.solr.client.solrj.SolrResponse in project lucene-solr by apache.

the class RealTimeGetComponent method mergeResponses.

private void mergeResponses(ResponseBuilder rb) {
    SolrDocumentList docList = new SolrDocumentList();
    for (ShardRequest sreq : rb.finished) {
        // can get more than one response
        for (ShardResponse srsp : sreq.responses) {
            SolrResponse sr = srsp.getSolrResponse();
            NamedList nl = sr.getResponse();
            SolrDocumentList subList = (SolrDocumentList) nl.get("response");
            docList.addAll(subList);
        }
    }
    addDocListToResponse(rb, docList);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrResponse(org.apache.solr.client.solrj.SolrResponse)

Example 3 with SolrResponse

use of org.apache.solr.client.solrj.SolrResponse in project lucene-solr by apache.

the class ExactStatsCache method mergeToGlobalStats.

@Override
public void mergeToGlobalStats(SolrQueryRequest req, List<ShardResponse> responses) {
    Set<Object> allTerms = new HashSet<>();
    for (ShardResponse r : responses) {
        LOG.debug("Merging to global stats, shard={}, response={}", r.getShard(), r.getSolrResponse().getResponse());
        String shard = r.getShard();
        SolrResponse res = r.getSolrResponse();
        NamedList<Object> nl = res.getResponse();
        // TODO: nl == null if not all shards respond (no server hosting shard)
        String termStatsString = (String) nl.get(TERM_STATS_KEY);
        if (termStatsString != null) {
            addToPerShardTermStats(req, shard, termStatsString);
        }
        List<Object> terms = nl.getAll(TERMS_KEY);
        allTerms.addAll(terms);
        String colStatsString = (String) nl.get(COL_STATS_KEY);
        Map<String, CollectionStats> colStats = StatsUtil.colStatsMapFromString(colStatsString);
        if (colStats != null) {
            addToPerShardColStats(req, shard, colStats);
        }
    }
    if (allTerms.size() > 0) {
        req.getContext().put(TERMS_KEY, Lists.newArrayList(allTerms));
    }
    if (LOG.isDebugEnabled())
        printStats(req);
}
Also used : ShardResponse(org.apache.solr.handler.component.ShardResponse) SolrResponse(org.apache.solr.client.solrj.SolrResponse) HashSet(java.util.HashSet)

Example 4 with SolrResponse

use of org.apache.solr.client.solrj.SolrResponse in project lucene-solr by apache.

the class CollectionsHandler method handleResponse.

private SolrResponse handleResponse(String operation, ZkNodeProps m, SolrQueryResponse rsp, long timeout) throws KeeperException, InterruptedException {
    long time = System.nanoTime();
    if (m.containsKey(ASYNC) && m.get(ASYNC) != null) {
        String asyncId = m.getStr(ASYNC);
        if (asyncId.equals("-1")) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "requestid can not be -1. It is reserved for cleanup purposes.");
        }
        NamedList<String> r = new NamedList<>();
        if (coreContainer.getZkController().getOverseerCompletedMap().contains(asyncId) || coreContainer.getZkController().getOverseerFailureMap().contains(asyncId) || coreContainer.getZkController().getOverseerRunningMap().contains(asyncId) || overseerCollectionQueueContains(asyncId)) {
            r.add("error", "Task with the same requestid already exists.");
        } else {
            coreContainer.getZkController().getOverseerCollectionQueue().offer(Utils.toJSON(m));
        }
        r.add(CoreAdminParams.REQUESTID, (String) m.get(ASYNC));
        SolrResponse response = new OverseerSolrResponse(r);
        rsp.getValues().addAll(response.getResponse());
        return response;
    }
    QueueEvent event = coreContainer.getZkController().getOverseerCollectionQueue().offer(Utils.toJSON(m), timeout);
    if (event.getBytes() != null) {
        SolrResponse response = SolrResponse.deserialize(event.getBytes());
        rsp.getValues().addAll(response.getResponse());
        SimpleOrderedMap exp = (SimpleOrderedMap) response.getResponse().get("exception");
        if (exp != null) {
            Integer code = (Integer) exp.get("rspCode");
            rsp.setException(new SolrException(code != null && code != -1 ? ErrorCode.getErrorCode(code) : ErrorCode.SERVER_ERROR, (String) exp.get("msg")));
        }
        return response;
    } else {
        if (System.nanoTime() - time >= TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS)) {
            throw new SolrException(ErrorCode.SERVER_ERROR, operation + " the collection time out:" + timeout / 1000 + "s");
        } else if (event.getWatchedEvent() != null) {
            throw new SolrException(ErrorCode.SERVER_ERROR, operation + " the collection error [Watcher fired on path: " + event.getWatchedEvent().getPath() + " state: " + event.getWatchedEvent().getState() + " type " + event.getWatchedEvent().getType() + "]");
        } else {
            throw new SolrException(ErrorCode.SERVER_ERROR, operation + " the collection unknown case");
        }
    }
}
Also used : OverseerSolrResponse(org.apache.solr.cloud.OverseerSolrResponse) NamedList(org.apache.solr.common.util.NamedList) QueueEvent(org.apache.solr.cloud.OverseerTaskQueue.QueueEvent) StrUtils.formatString(org.apache.solr.common.util.StrUtils.formatString) SolrResponse(org.apache.solr.client.solrj.SolrResponse) OverseerSolrResponse(org.apache.solr.cloud.OverseerSolrResponse) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrException(org.apache.solr.common.SolrException)

Example 5 with SolrResponse

use of org.apache.solr.client.solrj.SolrResponse in project lucene-solr by apache.

the class CollectionsHandler method invokeAction.

void invokeAction(SolrQueryRequest req, SolrQueryResponse rsp, CoreContainer cores, CollectionAction action, CollectionOperation operation) throws Exception {
    if (!coreContainer.isZooKeeperAware()) {
        throw new SolrException(BAD_REQUEST, "Invalid request. collections can be accessed only in SolrCloud mode");
    }
    SolrResponse response = null;
    Map<String, Object> props = operation.execute(req, rsp, this);
    String asyncId = req.getParams().get(ASYNC);
    if (props != null) {
        if (asyncId != null) {
            props.put(ASYNC, asyncId);
        }
        props.put(QUEUE_OPERATION, operation.action.toLower());
        ZkNodeProps zkProps = new ZkNodeProps(props);
        if (operation.sendToOCPQueue) {
            response = handleResponse(operation.action.toLower(), zkProps, rsp, operation.timeOut);
        } else
            Overseer.getStateUpdateQueue(coreContainer.getZkController().getZkClient()).offer(Utils.toJSON(props));
        final String collectionName = zkProps.getStr(NAME);
        if (action.equals(CollectionAction.CREATE) && asyncId == null) {
            if (rsp.getException() == null) {
                waitForActiveCollection(collectionName, zkProps, cores, response);
            }
        }
    }
}
Also used : ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) SolrResponse(org.apache.solr.client.solrj.SolrResponse) OverseerSolrResponse(org.apache.solr.cloud.OverseerSolrResponse) StrUtils.formatString(org.apache.solr.common.util.StrUtils.formatString) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrResponse (org.apache.solr.client.solrj.SolrResponse)9 OverseerSolrResponse (org.apache.solr.cloud.OverseerSolrResponse)3 SolrException (org.apache.solr.common.SolrException)3 QueueEvent (org.apache.solr.cloud.OverseerTaskQueue.QueueEvent)2 NamedList (org.apache.solr.common.util.NamedList)2 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)2 StrUtils.formatString (org.apache.solr.common.util.StrUtils.formatString)2 ShardResponse (org.apache.solr.handler.component.ShardResponse)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrResponseBase (org.apache.solr.client.solrj.response.SolrResponseBase)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)1 VelocityContext (org.apache.velocity.VelocityContext)1 ComparisonDateTool (org.apache.velocity.tools.generic.ComparisonDateTool)1 DisplayTool (org.apache.velocity.tools.generic.DisplayTool)1