Search in sources :

Example 1 with SolrQueryResponse

use of org.apache.solr.request.SolrQueryResponse in project Solbase by Photobucket.

the class SolbaseDispatchFilter method handleAdminRequest.

@SuppressWarnings({ "unused", "unchecked" })
private void handleAdminRequest(HttpServletRequest req, ServletResponse response, SolrRequestHandler handler, SolrQueryRequest solrReq) throws IOException {
    SolrQueryResponse solrResp = new SolrQueryResponse();
    final NamedList<Object> responseHeader = new SimpleOrderedMap<Object>();
    solrResp.add("responseHeader", responseHeader);
    NamedList<Object> toLog = solrResp.getToLog();
    toLog.add("webapp", req.getContextPath());
    toLog.add("path", solrReq.getContext().get("path"));
    toLog.add("params", "{" + solrReq.getParamString() + "}");
    handler.handleRequest(solrReq, solrResp);
    SolrCore.setResponseHeaderValues(handler, solrReq, solrResp);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < toLog.size(); i++) {
        String name = toLog.getName(i);
        Object val = toLog.getVal(i);
        sb.append(name).append("=").append(val).append(" ");
    }
    QueryResponseWriter respWriter = SolrCore.DEFAULT_RESPONSE_WRITERS.get(solrReq.getParams().get(CommonParams.WT));
    if (respWriter == null)
        respWriter = SolrCore.DEFAULT_RESPONSE_WRITERS.get("standard");
    writeResponse(solrResp, response, respWriter, solrReq, Method.getMethod(req.getMethod()));
}
Also used : SolrQueryResponse(org.apache.solr.request.SolrQueryResponse) QueryResponseWriter(org.apache.solr.request.QueryResponseWriter) BinaryQueryResponseWriter(org.apache.solr.request.BinaryQueryResponseWriter) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 2 with SolrQueryResponse

use of org.apache.solr.request.SolrQueryResponse in project Solbase by Photobucket.

the class SolbaseQuerySenderListener method newSearcher.

@Override
public void newSearcher(SolrIndexSearcher newSearcher, SolrIndexSearcher currentSearcher) {
    // don't warm up this core. only warm up shard cores
    if (!core.getName().equals("") && core.getName().indexOf("~") > 0) {
        final SolrIndexSearcher searcher = newSearcher;
        log.info("QuerySenderListener sending requests to " + core.getName());
        for (NamedList nlst : (List<NamedList>) args.get("queries")) {
            try {
                SolrQueryResponse rsp = new SolrQueryResponse();
                LocalSolrQueryRequest req = new LocalSolrQueryRequest(core, nlst) {

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

                    @Override
                    public void close() {
                    }
                };
                req.getContext().put("solbase-index", core.getName());
                req.getContext().put("webapp", "/solbase");
                req.getContext().put("path", "/select");
                core.execute(core.getRequestHandler(req.getParams().get(core.getName())), req, rsp);
                req.close();
            } catch (Exception e) {
            // do nothing... we want to continue with the other
            // requests.
            // the failure should have already been logged.
            }
        }
        log.info("QuerySenderListener done.");
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.request.SolrQueryResponse) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) DocList(org.apache.solr.search.DocList) NamedList(org.apache.solr.common.util.NamedList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher)

Example 3 with SolrQueryResponse

use of org.apache.solr.request.SolrQueryResponse in project Solbase by Photobucket.

the class SolbaseQueryComponent method process.

/**
	 * Actually run the query
	 */
@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;
    SolrParams params = req.getParams();
    if (!params.getBool(COMPONENT_NAME, true)) {
        return;
    }
    SolrIndexSearcher searcher = req.getSearcher();
    if (rb.getQueryCommand().getOffset() < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'start' parameter cannot be negative");
    }
    // -1 as flag if not set.
    long timeAllowed = (long) params.getInt(CommonParams.TIME_ALLOWED, -1);
    // Optional: This could also be implemented by the top-level searcher
    // sending
    // a filter that lists the ids... that would be transparent to
    // the request handler, but would be more expensive (and would preserve
    // score
    // too if desired).
    String ids = params.get(ShardParams.IDS);
    if (ids != null) {
        List<String> idArr = StrUtils.splitSmart(ids, ",", true);
        int[] luceneIds = new int[idArr.size()];
        int docs = 0;
        for (int i = 0; i < idArr.size(); i++) {
            luceneIds[docs++] = Integer.parseInt(idArr.get(i));
        }
        // we are indexing docId as solr uniq_id. by doing this, we are
        // bound to INTEGER.MAX_VALUE ~= 2 billion
        // docs is number of docs
        DocListAndSet res = new DocListAndSet();
        res.docList = new DocSlice(0, docs, luceneIds, null, docs, 0, null);
        if (rb.isNeedDocSet()) {
            List<Query> queries = new ArrayList<Query>();
            queries.add(rb.getQuery());
            List<Query> filters = rb.getFilters();
            if (filters != null)
                queries.addAll(filters);
            res.docSet = searcher.getDocSet(queries);
        }
        rb.setResults(res);
        rsp.add("response", rb.getResults().docList);
        return;
    }
    SolrIndexSearcher.QueryCommand cmd = rb.getQueryCommand();
    cmd.setTimeAllowed(timeAllowed);
    SolrIndexSearcher.QueryResult result = new SolrIndexSearcher.QueryResult();
    searcher.search(result, cmd);
    rb.setResult(result);
    rsp.add("response", rb.getResults().docList);
    rsp.getToLog().add("hits", rb.getResults().docList.matches());
    // The query cache doesn't currently store sort field values, and
    // SolrIndexSearcher doesn't
    // currently have an option to return sort field values. Because of
    // this, we
    // take the documents given and re-derive the sort values.
    boolean fsv = req.getParams().getBool(ResponseBuilder.FIELD_SORT_VALUES, false);
    if (fsv) {
        Sort sort = rb.getSortSpec().getSort();
        SortField[] sortFields = sort == null ? new SortField[] { SortField.FIELD_SCORE } : sort.getSort();
        // order is important for the
        NamedList sortVals = new NamedList();
        // sort fields
        // a
        Field field = new Field("dummy", "", Field.Store.YES, Field.Index.NO);
        // dummy
        // Field
        SolrIndexReader reader = searcher.getReader();
        SolrIndexReader[] readers = reader.getLeafReaders();
        SolrIndexReader subReader = reader;
        if (readers.length == 1) {
            // if there is a single segment, use that subReader and avoid
            // looking up each time
            subReader = readers[0];
            readers = null;
        }
        int[] offsets = reader.getLeafOffsets();
        //TODO: need to fetch sort value from collector instead of re-derive lookup from id
        for (SortField sortField : sortFields) {
            int type = sortField.getType();
            if (type == SortField.SCORE || type == SortField.DOC)
                continue;
            FieldComparator comparator = null;
            FieldComparator[] comparators = (readers == null) ? null : new FieldComparator[readers.length];
            String fieldname = sortField.getField();
            FieldType ft = fieldname == null ? null : req.getSchema().getFieldTypeNoEx(fieldname);
            DocSlice docList = (DocSlice) rb.getResults().docList;
            ArrayList<Object> vals = new ArrayList<Object>(docList.size());
            for (int i = docList.offset; i < docList.len; i++) {
                vals.add(new Integer(docList.sorts[i][((EmbeddedSortField) sortField).getFieldNumber() - 1]));
            }
            sortVals.add(fieldname, vals);
        }
        rsp.add("sort_values", sortVals);
    }
    // pre-fetch returned documents
    if (!req.getParams().getBool(ShardParams.IS_SHARD, false) && rb.getResults().docList != null && rb.getResults().docList.size() <= 50) {
        // TODO: this may depend on the highlighter component (or other
        // components?)
        SolrPluginUtils.optimizePreFetchDocs(rb.getResults().docList, rb.getQuery(), req, rsp);
    }
}
Also used : Query(org.apache.lucene.search.Query) DocListAndSet(org.apache.solr.search.DocListAndSet) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField) EmbeddedSortField(org.apache.solr.schema.EmbeddedSortField) EmbeddedSortField(org.apache.solr.schema.EmbeddedSortField) DocSlice(org.apache.solr.search.DocSlice) SolrIndexReader(org.apache.solr.search.SolrIndexReader) SortField(org.apache.lucene.search.SortField) Field(org.apache.lucene.document.Field) EmbeddedSortField(org.apache.solr.schema.EmbeddedSortField) Sort(org.apache.lucene.search.Sort) SolrException(org.apache.solr.common.SolrException) SolrQueryResponse(org.apache.solr.request.SolrQueryResponse) NamedList(org.apache.solr.common.util.NamedList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) FieldType(org.apache.solr.schema.FieldType) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) FieldComparator(org.apache.lucene.search.FieldComparator)

Aggregations

SolrQueryResponse (org.apache.solr.request.SolrQueryResponse)3 NamedList (org.apache.solr.common.util.NamedList)2 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Field (org.apache.lucene.document.Field)1 FieldComparator (org.apache.lucene.search.FieldComparator)1 Query (org.apache.lucene.search.Query)1 Sort (org.apache.lucene.search.Sort)1 SortField (org.apache.lucene.search.SortField)1 SolrException (org.apache.solr.common.SolrException)1 SolrParams (org.apache.solr.common.params.SolrParams)1 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)1 BinaryQueryResponseWriter (org.apache.solr.request.BinaryQueryResponseWriter)1 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)1 QueryResponseWriter (org.apache.solr.request.QueryResponseWriter)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 EmbeddedSortField (org.apache.solr.schema.EmbeddedSortField)1 FieldType (org.apache.solr.schema.FieldType)1 DocList (org.apache.solr.search.DocList)1