Example usage for org.apache.solr.common.params CommonParams QT

List of usage examples for org.apache.solr.common.params CommonParams QT

Introduction

In this page you can find the example usage for org.apache.solr.common.params CommonParams QT.

Prototype

String QT

To view the source code for org.apache.solr.common.params CommonParams QT.

Click Source Link

Document

the Request Handler (formerly known as the Query Type) - which Request Handler should handle the request

Usage

From source file:org.opensextant.solrtexttagger.AbstractTaggerTest.java

License:Open Source License

@Override
public void setUp() throws Exception {
    super.setUp();
    baseParams.clear();//from  w w w  .  ja v  a2 s .com
    baseParams.set(CommonParams.QT, "/tag");
}

From source file:org.opensextant.solrtexttagger.AbstractTaggerTest.java

License:Open Source License

/** Asserts the tags.  Will call req.close(). */
protected void assertTags(SolrQueryRequest req, TestTag... eTags) throws Exception {
    try {/*from ww w . jav a  2s . c o m*/
        SolrQueryResponse rsp = h.queryAndResponse(req.getParams().get(CommonParams.QT), req);
        TestTag[] aTags = pullTagsFromResponse(req, rsp);

        String message;
        if (aTags.length > 10)
            message = null;
        else
            message = Arrays.asList(aTags).toString();
        Arrays.sort(eTags);
        assertSortedArrayEquals(message, eTags, aTags);

    } finally {
        req.close();
    }
}

From source file:org.restlet.ext.lucene.SolrClientHelper.java

License:Open Source License

@Override
public void handle(Request request, Response response) {
    super.handle(request, response);

    Reference resRef = request.getResourceRef();
    String path = resRef.getPath();

    if (path != null) {
        path = resRef.getPath(true);//from w w w  . j a  va  2s.c  o m
    }

    String coreName = request.getResourceRef().getHostDomain();

    if (coreName == null || "".equals(coreName)) {
        coreName = getContext().getParameters().getFirstValue("DefaultCore");
    }

    SolrCore core = coreContainer.getCore(coreName);

    if (core == null) {
        response.setStatus(Status.SERVER_ERROR_INTERNAL, "No such core: " + coreName);
        return;
    }

    // Extract the handler from the path or params
    SolrRequestHandler handler = core.getRequestHandler(path);

    if (handler == null) {
        if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
            String qt = request.getResourceRef().getQueryAsForm().getFirstValue(CommonParams.QT);
            handler = core.getRequestHandler(qt);
            if (handler == null) {
                response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + qt);
                return;
            }
        }
        // Perhaps the path is to manage the cores
        if (handler == null && coreContainer != null && path.equals(coreContainer.getAdminPath())) {
            handler = coreContainer.getMultiCoreHandler();
        }
    }

    if (handler == null) {
        core.close();
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + path);
        return;
    }

    try {
        SolrQueryRequest solrReq = new SolrRestletQueryRequest(request, core);
        SolrQueryResponse solrResp = new SolrQueryResponse();
        core.execute(handler, solrReq, solrResp);

        if (solrResp.getException() != null) {
            response.setStatus(Status.SERVER_ERROR_INTERNAL, solrResp.getException());
        } else {
            response.setEntity(new SolrRepresentation(MediaType.APPLICATION_XML, solrReq, solrResp));
            response.setStatus(Status.SUCCESS_OK);
        }
    } catch (Exception e) {
        getLogger().log(Level.WARNING, "Unable to evaluate " + resRef.toString(), e);
        response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        core.close();
    }
}

From source file:org.restlet.ext.solr.SolrClientHelper.java

License:LGPL

@Override
public void handle(Request request, Response response) {
    super.handle(request, response);

    Reference resRef = request.getResourceRef();
    String path = resRef.getPath();

    if (path != null) {
        path = resRef.getPath(true);//  w ww  .  j  a v a  2s .  c  o  m
    }

    String coreName = request.getResourceRef().getHostDomain();

    if (coreName == null || "".equals(coreName)) {
        coreName = getContext().getParameters().getFirstValue("DefaultCore");
    }

    SolrCore core = coreContainer.getCore(coreName);

    if (core == null) {
        response.setStatus(Status.SERVER_ERROR_INTERNAL, "No such core: " + coreName);
        return;
    }

    // Extract the handler from the path or params
    SolrRequestHandler handler = core.getRequestHandler(path);

    if (handler == null) {
        if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
            String qt = request.getResourceRef().getQueryAsForm().getFirstValue(CommonParams.QT);
            handler = core.getRequestHandler(qt);
            if (handler == null) {
                response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + qt);
                return;
            }
        }
        // Perhaps the path is to manage the cores
        if (handler == null && coreContainer != null && path.equals(coreContainer.getAdminPath())) {
            handler = coreContainer.getMultiCoreHandler();
        }
    }

    if (handler == null) {
        core.close();
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "unknown handler: " + path);
        return;
    }

    try {
        SolrQueryRequest solrReq = new SolrRestletQueryRequest(request, core);

        SolrQueryResponse solrResp = new SolrQueryResponse();
        core.execute(handler, solrReq, solrResp);

        if (solrResp.getException() != null) {
            response.setStatus(Status.SERVER_ERROR_INTERNAL, solrResp.getException());
        } else {
            response.setEntity(new SolrRepresentation(solrReq, solrResp, core));
            response.setStatus(Status.SUCCESS_OK);
        }
    } catch (Exception e) {
        getLogger().log(Level.WARNING, "Unable to evaluate " + resRef.toString(), e);
        response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        core.close();
    }
}

From source file:org.schedoscope.metascope.index.SolrQueryExecutor.java

License:Apache License

public List<String> suggest(String userInput) {
    List<String> suggestions = new LinkedList<String>();

    SolrQuery query = new SolrQuery();
    query.setParam(CommonParams.QT, "/suggest");
    query.setParam("suggest", true);
    query.setParam(SuggesterParams.SUGGEST_BUILD, true);
    query.setParam(SuggesterParams.SUGGEST_DICT, "metascope");
    query.setParam(SuggesterParams.SUGGEST_Q, userInput);

    /* execute the query */
    QueryResponse queryResponse = null;//from  www  . j ava2s  . c om
    try {
        queryResponse = solrClient.query(query);
    } catch (Exception e) {
        e.printStackTrace();
    }

    List<Suggestion> currentSuggestions = new LinkedList<Suggestion>();
    if (queryResponse != null) {
        SuggesterResponse suggestorRespone = queryResponse.getSuggesterResponse();
        if (suggestorRespone != null) {
            Map<String, List<Suggestion>> suggestorResponeMap = suggestorRespone.getSuggestions();
            for (Entry<String, List<Suggestion>> e : suggestorResponeMap.entrySet()) {
                for (Suggestion suggestion : e.getValue()) {
                    int counter = 0;
                    for (Suggestion s : currentSuggestions) {
                        if (s.getWeight() > suggestion.getWeight()) {
                            counter++;
                        }
                    }
                    currentSuggestions.add(counter, suggestion);
                }
            }
        }
    }

    for (Suggestion suggestion : currentSuggestions) {
        suggestions.add(suggestion.getTerm());
    }

    return suggestions;
}

From source file:org.vootoo.server.RequestProcesser.java

License:Apache License

public void handleRequest(RequestGetter requestGetter) {
    MDCLoggingContext.reset();/* ww  w . ja  v  a  2 s  .c  o m*/
    MDCLoggingContext.setNode(cores);

    String path = requestGetter.getPath();
    solrParams = requestGetter.getSolrParams();
    SolrRequestHandler handler = null;
    String corename = "";
    String origCorename = null;
    try {
        // set a request timer which can be reused by requests if needed
        //req.setAttribute(SolrRequestParsers.REQUEST_TIMER_SERVLET_ATTRIBUTE, new RTimer());
        // put the core container in request attribute
        //req.setAttribute("org.apache.solr.CoreContainer", cores);
        // check for management path
        String alternate = cores.getManagementPath();
        if (alternate != null && path.startsWith(alternate)) {
            path = path.substring(0, alternate.length());
        }
        // unused feature ?
        int idx = path.indexOf(':');
        if (idx > 0) {
            // save the portion after the ':' for a 'handler' path parameter
            path = path.substring(0, idx);
        }

        boolean usingAliases = false;
        List<String> collectionsList = null;

        // Check for container handlers
        handler = cores.getRequestHandler(path);
        if (handler != null) {
            solrReq = parseSolrQueryRequest(SolrRequestParsers.DEFAULT, requestGetter);
            handleAdminRequest(handler, solrReq);
            return;
        } else {
            //otherwise, we should find a core from the path
            idx = path.indexOf("/", 1);
            if (idx > 1) {
                // try to get the corename as a request parameter first
                corename = path.substring(1, idx);

                // look at aliases
                if (cores.isZooKeeperAware()) {
                    origCorename = corename;
                    ZkStateReader reader = cores.getZkController().getZkStateReader();
                    aliases = reader.getAliases();
                    if (aliases != null && aliases.collectionAliasSize() > 0) {
                        usingAliases = true;
                        String alias = aliases.getCollectionAlias(corename);
                        if (alias != null) {
                            collectionsList = StrUtils.splitSmart(alias, ",", true);
                            corename = collectionsList.get(0);
                        }
                    }
                }

                core = cores.getCore(corename);

                if (core != null) {
                    path = path.substring(idx);
                }
            }

            //add collection name
            if (core == null && StringUtils.isNotBlank(requestGetter.getCollection())) {
                corename = requestGetter.getCollection();
                core = cores.getCore(corename);
            }

            if (core == null) {
                if (!cores.isZooKeeperAware()) {
                    core = cores.getCore("");
                }
            }
        }

        if (core == null && cores.isZooKeeperAware()) {
            // we couldn't find the core - lets make sure a collection was not specified instead
            core = getCoreByCollection(cores, corename);

            if (core != null) {
                // we found a core, update the path
                path = path.substring(idx);
            }

            // try the default core
            if (core == null) {
                core = cores.getCore("");
                if (core != null) {
                }
            }
        }

        // With a valid core...
        if (core != null) {
            MDCLoggingContext.setCore(core);
            final SolrConfig config = core.getSolrConfig();
            // get or create/cache the parser for the core
            SolrRequestParsers parser = config.getRequestParsers();

            // Determine the handler from the url path if not set
            // (we might already have selected the cores handler)
            if (handler == null && path.length() > 1) { // don't match "" or "/" as valid path
                handler = core.getRequestHandler(path);

                if (handler == null) {
                    //may be a restlet path
                    // Handle /schema/* paths via Restlet
                    if (path.equals("/schema") || path.startsWith("/schema/")) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                "unsupport /schema/**, use http solr");
                    }

                }
                // no handler yet but allowed to handle select; let's check
                if (handler == null && parser.isHandleSelect()) {
                    if ("/select".equals(path) || "/select/".equals(path)) {
                        solrReq = parseSolrQueryRequest(parser, requestGetter);

                        invalidStates = checkStateIsValid(cores,
                                solrReq.getParams().get(CloudSolrClient.STATE_VERSION));
                        String qt = solrReq.getParams().get(CommonParams.QT);
                        handler = core.getRequestHandler(qt);
                        if (handler == null) {
                            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                    "unknown handler: " + qt);
                        }
                        if (qt != null && qt.startsWith("/") && (handler instanceof ContentStreamHandlerBase)) {
                            //For security reasons it's a bad idea to allow a leading '/', ex: /select?qt=/update see SOLR-3161
                            //There was no restriction from Solr 1.4 thru 3.5 and it's not supported for update handlers.
                            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                    "Invalid Request Handler ('qt').  Do not use /select to access: " + qt);
                        }
                    }
                }
            }

            // With a valid handler and a valid core...
            if (handler != null) {
                // if not a /select, create the request
                if (solrReq == null) {
                    solrReq = parseSolrQueryRequest(parser, requestGetter);
                }

                if (usingAliases) {
                    processAliases(solrReq, aliases, collectionsList);
                }

                SolrQueryResponse solrRsp = new SolrQueryResponse();
                SolrRequestInfo.setRequestInfo(new SolrRequestInfo(solrReq, solrRsp));
                this.execute(handler, solrReq, solrRsp);
                QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
                if (invalidStates != null)
                    solrReq.getContext().put(CloudSolrClient.STATE_VERSION, invalidStates);
                writeResponse(solrRsp, responseWriter, solrReq);

                return; // we are done with a valid handler
            }
        }
        logger.debug("no handler or core retrieved for {}, follow through...", path);
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "no handler or core retrieved for " + path);
    } catch (Throwable ex) {
        sendError(core, solrReq, ex);
        // walk the the entire cause chain to search for an Error
        Throwable t = ex;
        while (t != null) {
            if (t instanceof Error) {
                if (t != ex) {
                    logger.error(
                            "An Error was wrapped in another exception - please report complete stacktrace on SOLR-6161",
                            ex);
                }
                throw (Error) t;
            }
            t = t.getCause();
        }
        return;
    } finally {
        try {
            if (solrReq != null) {
                logger.debug("Closing out SolrRequest: {}", solrReq);
                solrReq.close();
            }
        } finally {
            try {
                if (core != null) {
                    core.close();
                }
            } finally {
                SolrRequestInfo.clearRequestInfo();
            }

        }
        MDCLoggingContext.clear();
    }
}

From source file:solr2155.solr.search.function.distance.MultiDistanceFunctionTest.java

License:Apache License

/** TODO propose that this go into Solr's test harness. */
private void assertQScore(SolrQueryRequest req, int docIdx, float targetScore) throws Exception {
    try {//from  w ww .  j  av a2s.c  o  m
        String handler = req.getParams().get(CommonParams.QT);
        SolrQueryResponse resp = h.queryAndResponse(handler, req);
        //      ResultContext resCtx = (ResultContext) resp.getValues().get("response");
        final DocList docList = (DocList) resp.getValues().get("response");
        assertTrue("expected more docs", docList.size() >= docIdx + 1);
        assertTrue("expected scores", docList.hasScores());
        DocIterator docIterator = docList.iterator();
        for (int i = -1; i < docIdx; i++) {//loops at least once
            docIterator.nextDoc();
        }
        float gotScore = docIterator.score();
        assertEquals(gotScore, targetScore, 0.0001);
    } finally {
        req.close();
    }
}