Package org.apache.solr.request

Examples of org.apache.solr.request.SolrQueryResponse


   */
  @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();
      NamedList sortVals = new NamedList(); // order is important for the
                          // sort fields
      Field field = new Field("dummy", "", Field.Store.YES, Field.Index.NO); // a
                                          // 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
View Full Code Here


          }
          public void close() {
          }
        };

        SolrQueryResponse rsp = new SolrQueryResponse();
        core.execute(req,rsp);

        // Retrieve the Document instances (not just the ids) to warm
        // the OS disk cache, and any Solr document cache.  Only the top
        // level values in the NamedList are checked for DocLists.
        NamedList values = rsp.getValues();
        for (int i=0; i<values.size(); i++) {
          Object o = values.getVal(i);
          if (o instanceof DocList) {
            DocList docs = (DocList)o;
            for (DocIterator iter = docs.iterator(); iter.hasNext();) {
View Full Code Here

    if( body != null && body.length() > 0 ) {
      streams.add( new ContentStreamBase.StringStream( body ) );
    }
   
    SolrQueryRequest req = parser.buildRequestFrom( params, streams );
    SolrQueryResponse rsp = new SolrQueryResponse();
    core.execute( handler, req, rsp );
    if( rsp.getException() != null ) {
      throw rsp.getException();
    }
   
    // Now write it out
    QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
    StringWriter out = new StringWriter();
View Full Code Here

    doGet(request,response);
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    SolrServletRequest solrReq = new SolrServletRequest(core, request);;
    SolrQueryResponse solrRsp = new SolrQueryResponse();
    try {

      SolrRequestHandler handler = core.getRequestHandler(solrReq.getQueryType());
      if (handler==null) {
        log.warning("Unknown Request Handler '" + solrReq.getQueryType() +"' :" + solrReq);
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"Unknown Request Handler '" + solrReq.getQueryType() + "'", true);
      }
      core.execute(handler, solrReq, solrRsp );
      if (solrRsp.getException() == null) {
        QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
        response.setContentType(responseWriter.getContentType(solrReq, solrRsp));
        PrintWriter out = response.getWriter();
        responseWriter.write(out, solrReq, solrRsp);
      } else {
        Exception e = solrRsp.getException();
        int rc=500;
        if (e instanceof SolrException) {
           rc=((SolrException)e).code();
        }
        sendErr(rc, SolrException.toStr(e), request, response);
View Full Code Here

        }
        if( handler != null ) {
          if( solrReq == null ) {
            solrReq = parsers.parse( path, req );
          }
          SolrQueryResponse solrRsp = new SolrQueryResponse();
          this.execute( req, handler, solrReq, solrRsp );
          if( solrRsp.getException() != null ) {
            sendError( (HttpServletResponse)response, solrRsp.getException() );
            return;
          }
         
          // Now write it out
          QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
View Full Code Here

TOP

Related Classes of org.apache.solr.request.SolrQueryResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.