Search in sources :

Example 1 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class VelocityResponseWriterTest method testFileResourceLoader.

@Test
public void testFileResourceLoader() throws Exception {
    VelocityResponseWriter vrw = new VelocityResponseWriter();
    NamedList<String> nl = new NamedList<String>();
    nl.add("template.base.dir", getFile("velocity").getAbsolutePath());
    vrw.init(nl);
    SolrQueryRequest req = req(VelocityResponseWriter.TEMPLATE, "file");
    SolrQueryResponse rsp = new SolrQueryResponse();
    StringWriter buf = new StringWriter();
    vrw.write(buf, req, rsp);
    assertEquals("testing", buf.toString());
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) VelocityResponseWriter(org.apache.solr.response.VelocityResponseWriter) StringWriter(java.io.StringWriter) NamedList(org.apache.solr.common.util.NamedList) Test(org.junit.Test)

Example 2 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class VelocityResponseWriterTest method testCustomParamTemplate.

@Test
public void testCustomParamTemplate() throws Exception {
    org.apache.solr.response.VelocityResponseWriter vrw = new VelocityResponseWriter();
    NamedList<String> nl = new NamedList<String>();
    nl.add(VelocityResponseWriter.PARAMS_RESOURCE_LOADER_ENABLED, "true");
    vrw.init(nl);
    SolrQueryRequest req = req(VelocityResponseWriter.TEMPLATE, "custom", SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX + "custom", "$response.response.response_data");
    SolrQueryResponse rsp = new SolrQueryResponse();
    StringWriter buf = new StringWriter();
    rsp.add("response_data", "testing");
    vrw.write(buf, req, rsp);
    assertEquals("testing", buf.toString());
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) VelocityResponseWriter(org.apache.solr.response.VelocityResponseWriter) StringWriter(java.io.StringWriter) NamedList(org.apache.solr.common.util.NamedList) VelocityResponseWriter(org.apache.solr.response.VelocityResponseWriter) Test(org.junit.Test)

Example 3 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class VelocityResponseWriterTest method testContentType.

@Test
public void testContentType() throws Exception {
    VelocityResponseWriter vrw = new VelocityResponseWriter();
    NamedList<String> nl = new NamedList<String>();
    vrw.init(nl);
    SolrQueryResponse rsp = new SolrQueryResponse();
    // with v.json=wrf, content type should default to application/json
    assertEquals("application/json;charset=UTF-8", vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound", VelocityResponseWriter.JSON, "wrf"), rsp));
    // with no v.json specified, the default text/html should be returned
    assertEquals("text/html;charset=UTF-8", vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound"), rsp));
    // if v.contentType is specified, that should be used, even if v.json is specified
    assertEquals("text/plain", vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound", VelocityResponseWriter.CONTENT_TYPE, "text/plain"), rsp));
    assertEquals("text/plain", vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound", VelocityResponseWriter.JSON, "wrf", VelocityResponseWriter.CONTENT_TYPE, "text/plain"), rsp));
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) VelocityResponseWriter(org.apache.solr.response.VelocityResponseWriter) NamedList(org.apache.solr.common.util.NamedList) Test(org.junit.Test)

Example 4 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class QueryComponent method prepare.

@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();
    if (!params.getBool(COMPONENT_NAME, true)) {
        return;
    }
    SolrQueryResponse rsp = rb.rsp;
    // Set field flags    
    ReturnFields returnFields = new SolrReturnFields(req);
    rsp.setReturnFields(returnFields);
    int flags = 0;
    if (returnFields.wantsScore()) {
        flags |= SolrIndexSearcher.GET_SCORES;
    }
    rb.setFieldFlags(flags);
    String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
    // get it from the response builder to give a different component a chance
    // to set it.
    String queryString = rb.getQueryString();
    if (queryString == null) {
        // this is the normal way it's set.
        queryString = params.get(CommonParams.Q);
        rb.setQueryString(queryString);
    }
    try {
        QParser parser = QParser.getParser(rb.getQueryString(), defType, req);
        Query q = parser.getQuery();
        if (q == null) {
            // normalize a null query to a query that matches nothing
            q = new MatchNoDocsQuery();
        }
        rb.setQuery(q);
        String rankQueryString = rb.req.getParams().get(CommonParams.RQ);
        if (rankQueryString != null) {
            QParser rqparser = QParser.getParser(rankQueryString, defType, req);
            Query rq = rqparser.getQuery();
            if (rq instanceof RankQuery) {
                RankQuery rankQuery = (RankQuery) rq;
                rb.setRankQuery(rankQuery);
                MergeStrategy mergeStrategy = rankQuery.getMergeStrategy();
                if (mergeStrategy != null) {
                    rb.addMergeStrategy(mergeStrategy);
                    if (mergeStrategy.handlesMergeFields()) {
                        rb.mergeFieldHandler = mergeStrategy;
                    }
                }
            } else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "rq parameter must be a RankQuery");
            }
        }
        rb.setSortSpec(parser.getSortSpec(true));
        rb.setQparser(parser);
        final String cursorStr = rb.req.getParams().get(CursorMarkParams.CURSOR_MARK_PARAM);
        if (null != cursorStr) {
            final CursorMark cursorMark = new CursorMark(rb.req.getSchema(), rb.getSortSpec());
            cursorMark.parseSerializedTotem(cursorStr);
            rb.setCursorMark(cursorMark);
        }
        String[] fqs = req.getParams().getParams(CommonParams.FQ);
        if (fqs != null && fqs.length != 0) {
            List<Query> filters = rb.getFilters();
            // if filters already exists, make a copy instead of modifying the original
            filters = filters == null ? new ArrayList<>(fqs.length) : new ArrayList<>(filters);
            for (String fq : fqs) {
                if (fq != null && fq.trim().length() != 0) {
                    QParser fqp = QParser.getParser(fq, req);
                    fqp.setIsFilter(true);
                    filters.add(fqp.getQuery());
                }
            }
            // if filter cache is disabled
            if (!filters.isEmpty()) {
                rb.setFilters(filters);
            }
        }
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
    if (params.getBool(GroupParams.GROUP, false)) {
        prepareGrouping(rb);
    } else {
        //Validate only in case of non-grouping search.
        if (rb.getSortSpec().getCount() < 0) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'rows' parameter cannot be negative");
        }
    }
    //Input validation.
    if (rb.getSortSpec().getOffset() < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'start' parameter cannot be negative");
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RankQuery(org.apache.solr.search.RankQuery) CursorMark(org.apache.solr.search.CursorMark) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ArrayList(java.util.ArrayList) SolrReturnFields(org.apache.solr.search.SolrReturnFields) RankQuery(org.apache.solr.search.RankQuery) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SyntaxError(org.apache.solr.search.SyntaxError) QParser(org.apache.solr.search.QParser) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 5 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class RealTimeGetComponent method processGetUpdates.

public void processGetUpdates(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;
    SolrParams params = req.getParams();
    if (!params.getBool(COMPONENT_NAME, true)) {
        return;
    }
    String versionsStr = params.get("getUpdates");
    if (versionsStr == null)
        return;
    UpdateLog ulog = req.getCore().getUpdateHandler().getUpdateLog();
    if (ulog == null)
        return;
    // handle version ranges
    List<Long> versions = null;
    if (versionsStr.indexOf("...") != -1) {
        versions = resolveVersionRanges(versionsStr, ulog);
    } else {
        versions = StrUtils.splitSmart(versionsStr, ",", true).stream().map(Long::parseLong).collect(Collectors.toList());
    }
    // find fingerprint for max version for which updates are requested
    boolean doFingerprint = params.getBool("fingerprint", false);
    if (doFingerprint) {
        long maxVersionForUpdate = Collections.min(versions, PeerSync.absComparator);
        IndexFingerprint fingerprint = IndexFingerprint.getFingerprint(req.getCore(), Math.abs(maxVersionForUpdate));
        rb.rsp.add("fingerprint", fingerprint);
    }
    List<Object> updates = new ArrayList<>(versions.size());
    long minVersion = Long.MAX_VALUE;
    // TODO: get this from cache instead of rebuilding?
    try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
        for (Long version : versions) {
            try {
                Object o = recentUpdates.lookup(version);
                if (o == null)
                    continue;
                if (version > 0) {
                    minVersion = Math.min(minVersion, version);
                }
                // TODO: do any kind of validation here?
                updates.add(o);
            } catch (SolrException | ClassCastException e) {
                log.warn("Exception reading log for updates", e);
            }
        }
        // Must return all delete-by-query commands that occur after the first add requested
        // since they may apply.
        updates.addAll(recentUpdates.getDeleteByQuery(minVersion));
        rb.rsp.add("updates", updates);
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) IndexFingerprint(org.apache.solr.update.IndexFingerprint) UpdateLog(org.apache.solr.update.UpdateLog) AtomicLong(java.util.concurrent.atomic.AtomicLong) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)258 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)128 Test (org.junit.Test)100 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)80 NamedList (org.apache.solr.common.util.NamedList)68 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)58 SolrCore (org.apache.solr.core.SolrCore)52 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)41 SolrInputDocument (org.apache.solr.common.SolrInputDocument)40 SolrException (org.apache.solr.common.SolrException)32 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)29 ArrayList (java.util.ArrayList)26 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)24 SolrRequestHandler (org.apache.solr.request.SolrRequestHandler)22 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)21 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)20 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)20 JsonLoader (org.apache.solr.handler.loader.JsonLoader)17 IOException (java.io.IOException)16 HashMap (java.util.HashMap)16