All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.tst.TestRequestHandler Maven / Gradle / Ivy

The newest version!
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.solr.tst;

import org.apache.lucene.search.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;

import java.util.*;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URL;

import org.apache.lucene.util.OpenBitSet;
import org.apache.solr.search.*;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;

/**
 * @version $Id: TestRequestHandler.java 723994 2008-12-06 14:55:21Z gsingers $
 * 
 * @deprecated Test against the real request handlers instead.
 */
@Deprecated
public class TestRequestHandler implements SolrRequestHandler {
  private static Logger log = LoggerFactory.getLogger(SolrIndexSearcher.class);

  public void init(NamedList args) {
    SolrCore.log.info( "Unused request handler arguments:" + args);
  }

  // use test instead of assert since asserts may be turned off
  public void test(boolean condition) {
    try {
      if (!condition) {
        throw new RuntimeException("test requestHandler: assertion failed!");
      }
    } catch (RuntimeException e) {
      SolrException.log(log,e);
      throw(e);
    }
  }


  private long numRequests;
  private long numErrors;

  private final Pattern splitList=Pattern.compile(",| ");


  public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
    numRequests++;

    // TODO: test if lucene will accept an escaped ';', otherwise
    // we need to un-escape them before we pass to QueryParser
    try {
      String sreq = req.getQueryString();
      if (sreq==null) throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Missing queryString");
      List commands = StrUtils.splitSmart(sreq,';');

      String qs = commands.size() >= 1 ? commands.get(0) : "";
      Query query = QueryParsing.parseQuery(qs, req.getSchema());

      // find fieldnames to return (fieldlist)
      String fl = req.getParam("fl");
      int flags=0;
      if (fl != null) {
        // TODO - this could become more efficient if widely used.
        // TODO - should field order be maintained?
        String[] flst = splitList.split(fl,0);
        if (flst.length > 0 && !(flst.length==1 && flst[0].length()==0)) {
          Set set = new HashSet();
          for (String fname : flst) {
            if ("score".equals(fname)) flags |= SolrIndexSearcher.GET_SCORES;
            set.add(fname);
          }
          rsp.setReturnFields(set);
        }
      }


      // If the first non-query, non-filter command is a simple sort on an indexed field, then
      // we can use the Lucene sort ability.
      Sort sort = null;
      if (commands.size() >= 2) {
        sort = QueryParsing.parseSort(commands.get(1), req.getSchema());
      }

      SolrIndexSearcher searcher = req.getSearcher();

      /***
      Object o = searcher.cacheLookup("dfllNode", query);
      if (o == null) {
        searcher.cacheInsert("dfllNode",query,"Hello Bob");
      } else {
        System.out.println("User Cache Hit On " + o);
      }
      ***/

      int start=req.getStart();
      int limit=req.getLimit();

      Query filterQuery=null;
      DocSet filter=null;
      Filter lfilter=null;

      DocList results = req.getSearcher().getDocList(query, null, sort, req.getStart(), req.getLimit(), flags);
      rsp.add(null, results);


      if (qs.startsWith("values")) {
        rsp.add("testname1","testval1");

        rsp.add("testarr1",new String[]{"my val 1","my val 2"});

        NamedList nl = new NamedList();
        nl.add("myInt", 333);
        nl.add("myNullVal", null);
        nl.add("myFloat",1.414213562f);
        nl.add("myDouble", 1e100d);
        nl.add("myBool", false);
        nl.add("myLong",999999999999L);

        Document doc = new Document();
        doc.add(new Field("id","55",Field.Store.YES, Field.Index.UN_TOKENIZED));
        nl.add("myDoc",doc);

        nl.add("myResult",results);
        nl.add("myStr","&wow! test escaping: a&b




© 2015 - 2024 Weber Informatics LLC | Privacy Policy