Package org.apache.solr.request

Examples of org.apache.solr.request.SolrParams


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

    try {
      U.setDefaults(req,defaults,appends,invariants);
      SolrParams p = req.getParams();
      String sreq = p.get(Q);

      String defaultField = p.get(DF);

      // find fieldnames to return (fieldlist)
      String fl = p.get(SolrParams.FL);
      int flags = 0;
      if (fl != null) {
        flags |= U.setReturnFields(fl, rsp);
      }

      if (sreq==null) throw new SolrException(400,"Missing queryString");
      List<String> commands = StrUtils.splitSmart(sreq,';');

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

      // 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) {
        QueryParsing.SortSpec sortSpec = QueryParsing.parseSort(commands.get(1), req.getSchema());
        if (sortSpec != null) {
          sort = sortSpec.getSort();
          // ignore the count for now... it's currently only controlled by start & limit on req
          // count = sortSpec.getCount();
        }
      }

      DocListAndSet results = new DocListAndSet();
      NamedList facetInfo = null;
      List<Query> filters = U.parseFilterQueries(req);
      SolrIndexSearcher s = req.getSearcher();

      if (p.getBool(FACET,false)) {
        results = s.getDocListAndSet(query, filters, sort,
                                     p.getInt(START,0), p.getInt(ROWS,10),
                                     flags);
        facetInfo = getFacetInfo(req, rsp, results.docSet);
      } else {
        results.docList = s.getDocList(query, filters, sort,
                                       p.getInt(START,0), p.getInt(ROWS,10),
                                       flags);
      }

      // pre-fetch returned documents
      U.optimizePreFetchDocs(results.docList, query, req, rsp);
View Full Code Here


   * @param invariants values which will be used instead of any request, or default values, regardless of context.
   */
  public static void setDefaults(SolrQueryRequest req, SolrParams defaults,
                                 SolrParams appends, SolrParams invariants) {
   
      SolrParams p = req.getParams();
      if (defaults != null) {
        p = new DefaultSolrParams(p,defaults);
      }
      if (appends != null) {
        p = new AppendedSolrParams(p,appends);
View Full Code Here

  public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
    numRequests++;
       
    try {
      U.setDefaults(req,defaults,appends,invariants);
      SolrParams params = req.getParams();
     
      int flags = 0;
     
      SolrIndexSearcher s = req.getSearcher();
      IndexSchema schema = req.getSchema();
           
      Map<String,Float> queryFields = U.parseFieldBoosts(params.get(DMP.QF));
      Map<String,Float> phraseFields = U.parseFieldBoosts(params.get(DMP.PF));

      float tiebreaker = params.getFloat(DMP.TIE, 0.0f);
           
      int pslop = params.getInt(DMP.PS, 0);

      /* a generic parser for parsing regular lucene queries */
      QueryParser p = new SolrQueryParser(schema, null);

      /* a parser for dealing with user input, which will convert
       * things to DisjunctionMaxQueries
       */
      U.DisjunctionMaxQueryParser up =
        new U.DisjunctionMaxQueryParser(schema, IMPOSSIBLE_FIELD_NAME);
      up.addAlias(IMPOSSIBLE_FIELD_NAME,
                  tiebreaker, queryFields);

      /* for parsing slopy phrases using DisjunctionMaxQueries */
      U.DisjunctionMaxQueryParser pp =
        new U.DisjunctionMaxQueryParser(schema, IMPOSSIBLE_FIELD_NAME);
      pp.addAlias(IMPOSSIBLE_FIELD_NAME,
                  tiebreaker, phraseFields);
      pp.setPhraseSlop(pslop);
           
           
      /* * * Main User Query * * */

      String userQuery = U.partialEscape
        (U.stripUnbalancedQuotes(params.get(Q))).toString();
           
      /* the main query we will execute.  we disable the coord because
       * this query is an artificial construct
       */
      BooleanQuery query = new BooleanQuery(true);

      String minShouldMatch = params.get(DMP.MM, "100%");
           
      Query dis = up.parse(userQuery);

      if (dis instanceof BooleanQuery) {
        BooleanQuery t = new BooleanQuery();
        U.flattenBooleanQuery(t, (BooleanQuery)dis);

        U.setMinShouldMatch(t, minShouldMatch);
               
        query.add(t, Occur.MUST);
      } else {
        query.add(dis, Occur.MUST);
      }

      /* * * Add on Phrases for the Query * * */
           
      /* build up phrase boosting queries */

      /* if the userQuery already has some quotes, stip them out.
       * we've already done the phrases they asked for in the main
       * part of the query, this is to boost docs that may not have
       * matched those phrases but do match looser phrases.
       */
      String userPhraseQuery = userQuery.replace("\"","");
      Query phrase = pp.parse("\"" + userPhraseQuery + "\"");
      if (null != phrase) {
        query.add(phrase, Occur.SHOULD);
      }
           
      /* * * Boosting Query * * */

      String boostQuery = params.get(DMP.BQ);
      if (null != boostQuery && !boostQuery.equals("")) {
        Query tmp = p.parse(boostQuery);
        /* if the default boost was used, and we've got a BooleanQuery
         * extract the subqueries out and use them directly
         */
        if (1.0f == tmp.getBoost() && tmp instanceof BooleanQuery) {
          for (BooleanClause c : ((BooleanQuery)tmp).getClauses()) {
            query.add(c);
          }
        } else {
          query.add(tmp, BooleanClause.Occur.SHOULD);
        }
      }

      /* * * Boosting Functions * * */

      String boostFunc = params.get(DMP.BF);
      if (null != boostFunc && !boostFunc.equals("")) {
        List<Query> funcs = U.parseFuncs(schema, boostFunc);
        for (Query f : funcs) {
          query.add(f, Occur.SHOULD);
        }
      }
           
      /* * * Restrict Results * * */

      List<Query> restrictions = U.parseFilterQueries(req);
           
      /* * * Generate Main Results * * */

      flags |= U.setReturnFields(req,rsp);
     
      DocListAndSet results = new DocListAndSet();
      NamedList facetInfo = null;
      if (params.getBool(FACET,false)) {
        results = s.getDocListAndSet(query, restrictions,
                                     SolrPluginUtils.getSort(req),
                                     req.getStart(), req.getLimit(),
                                     flags);
        facetInfo = getFacetInfo(req, rsp, results.docSet);
      } else {
        results.docList = s.getDocList(query, restrictions,
                                       SolrPluginUtils.getSort(req),
                                       req.getStart(), req.getLimit(),
                                       flags);
      }
      rsp.add("response",results.docList);
      // pre-fetch returned documents
      U.optimizePreFetchDocs(results.docList, query, req, rsp);

     
      if (null != facetInfo) rsp.add("facet_counts", facetInfo);


           
      /* * * Debugging Info * * */

      try {
        NamedList debug = U.doStandardDebug(req, userQuery, query, results.docList);
        if (null != debug) {
          debug.add("boostquery", boostQuery);
          debug.add("boostfunc", boostFunc);
          if (null != restrictions) {
            debug.add("filter_queries", params.getParams(FQ));
            List<String> fqs = new ArrayList<String>(restrictions.size());
            for (Query fq : restrictions) {
              fqs.add(QueryParsing.toString(fq, req.getSchema()));
            }
            debug.add("parsed_filter_queries",fqs);
View Full Code Here

   * @param invariants values which will be used instead of any request, or default values, regardless of context.
   */
  public static void setDefaults(SolrQueryRequest req, SolrParams defaults,
                                 SolrParams appends, SolrParams invariants) {
   
      SolrParams p = req.getParams();
      if (defaults != null) {
        p = new DefaultSolrParams(p,defaults);
      }
      if (appends != null) {
        p = new AppendedSolrParams(p,appends);
View Full Code Here

    RequestHandlerUtils.addExperimentalFormatWarning( rsp );
   
    IndexSchema schema = req.getSchema();
    SolrIndexSearcher searcher = req.getSearcher();
    IndexReader reader = searcher.getReader();
    SolrParams params = req.getParams();
    int numTerms = params.getInt( NUMTERMS, DEFAULT_COUNT );
       
    // Always show the core lucene info
    rsp.add("index", getIndexInfo(reader, numTerms>0 ) );

    Integer docId = params.getInt( DOC_ID );
    if( docId == null && params.get( ID ) != null ) {
      // Look for something with a given solr ID
      SchemaField uniqueKey = schema.getUniqueKeyField();
      String v = uniqueKey.getType().toInternal( params.get(ID) );
      Term t = new Term( uniqueKey.getName(), v );
      docId = searcher.getFirstMatch( t );
      if( docId < 0 ) {
        throw new SolrException( SolrException.ErrorCode.NOT_FOUND, "Can't find document: "+params.get( ID ) );
      }
    }
       
    // Read the document from the index
    if( docId != null ) {
      Document doc = null;
      try {
        doc = reader.document( docId );
      }
      catch( Exception ex ) {}
      if( doc == null ) {
        throw new SolrException( SolrException.ErrorCode.NOT_FOUND, "Can't find document: "+docId );
      }
     
      SimpleOrderedMap<Object> info = getDocumentFieldsInfo( doc, docId, reader, schema );
     
      SimpleOrderedMap<Object> docinfo = new SimpleOrderedMap<Object>();
      docinfo.add( "docId", docId );
      docinfo.add( "lucene", info );
      docinfo.add( "solr", doc );
      rsp.add( "doc", docinfo );
    }
    else {
      // If no doc is given, show all fields and top terms
      Set<String> fields = null;
      if( params.get( SolrParams.FL ) != null ) {
        fields = new HashSet<String>();
        for( String f : params.getParams( SolrParams.FL ) ) {
          fields.add( f );
        }
      }
      rsp.add( "fields", getIndexedFieldsInfo( searcher, fields, numTerms ) ) ;
    }
View Full Code Here


  public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception
  {
   
      SolrParams p = req.getParams();
      String qstr = p.required().get(Q);

      String defaultField = p.get(DF);

      // find fieldnames to return (fieldlist)
      String fl = p.get(SolrParams.FL);
      int flags = 0;
      if (fl != null) {
        flags |= U.setReturnFields(fl, rsp);
      }
     
      String sortStr = p.get(SORT);
      if( sortStr == null ) { 
        // TODO? should we disable the ';' syntax with config?
        // legacy mode, where sreq is query;sort
        List<String> commands = StrUtils.splitSmart(qstr,';');
        if( commands.size() == 2 ) {
          // TODO? add a deprication warning to the response header
          qstr = commands.get( 0 );
          sortStr = commands.get( 1 );
        }
        else if( commands.size() == 1 ) {
          // This is need to support the case where someone sends: "q=query;"
          qstr = commands.get( 0 );
        }
        else if( commands.size() > 2 ) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "If you want to use multiple ';' in the query, use the 'sort' param." );
        }
      }

      Sort sort = null;
      if( sortStr != null ) {
        QueryParsing.SortSpec sortSpec = QueryParsing.parseSort(sortStr, req.getSchema());
        if (sortSpec != null) {
          sort = sortSpec.getSort();
        }
      }

      // parse the query from the 'q' parameter (sort has been striped)
      Query query = QueryParsing.parseQuery(qstr, defaultField, p, req.getSchema());
     
      DocListAndSet results = new DocListAndSet();
      NamedList facetInfo = null;
      List<Query> filters = U.parseFilterQueries(req);
      SolrIndexSearcher s = req.getSearcher();

      if (p.getBool(FACET,false)) {
        results = s.getDocListAndSet(query, filters, sort,
                                     p.getInt(START,0), p.getInt(ROWS,10),
                                     flags);
        facetInfo = getFacetInfo(req, rsp, results.docSet);
      } else {
        results.docList = s.getDocList(query, filters, sort,
                                       p.getInt(START,0), p.getInt(ROWS,10),
                                       flags);
      }

      // pre-fetch returned documents
      U.optimizePreFetchDocs(results.docList, query, req, rsp);
View Full Code Here

{
  @Override
  public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception
  {
    RequestHandlerUtils.addExperimentalFormatWarning( rsp );
    SolrParams params = req.getParams();
   
    boolean stats = params.getBool( "stats", false );
    rsp.add( "plugins", getSolrInfoBeans( stats ) );
  }
View Full Code Here

   
    final int qtime=(int)(rsp.getEndTime() - req.getStartTime());
    responseHeader.add("status",rsp.getException()==null ? 0 : 500);
    responseHeader.add("QTime",qtime);
       
    SolrParams params = req.getParams();
    if( params.getBool(SolrParams.HEADER_ECHO_HANDLER, false) ) {
      responseHeader.add("handler", handler.getName() );
    }
   
    // Values for echoParams... false/true/all or false/explicit/all ???
    String ep = params.get( SolrParams.HEADER_ECHO_PARAMS, null );
    if( ep != null ) {
      EchoParamStyle echoParams = EchoParamStyle.get( ep );
      if( echoParams == null ) {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Invalid value '" + ep + "' for " + SolrParams.HEADER_ECHO_PARAMS
            + " parameter, use '" + EchoParamStyle.EXPLICIT + "' or '" + EchoParamStyle.ALL + "'" );
View Full Code Here

  private static final float DEFAULT_ACCURACY = 0.5f;
  private static final int DEFAULT_NUM_SUGGESTIONS = 1;
   
  public void init(NamedList args) {
    super.init(args);
    SolrParams p = SolrParams.toSolrParams(args);
    termSourceField = p.get("termSourceField");

    try {
      String dir = p.get("spellcheckerIndexDir");
      if (null != dir) {
        File f = new File(dir);
        if ( ! f.isAbsolute() ) {
          f = new File(SolrCore.getSolrCore().getDataDir(), dir);
        }
View Full Code Here

    }
  }

  public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
    throws Exception {
    SolrParams p = req.getParams();
    String words = p.get("q");
    String cmd = p.get("cmd");
    if (cmd != null) {
      cmd = cmd.trim();
      if (cmd.equals("rebuild")) {
        rebuild(req);
        rsp.add("cmdExecuted","rebuild");
      } else if (cmd.equals("reopen")) {
        reopen();
        rsp.add("cmdExecuted","reopen");
      } else {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Unrecognized Command: " + cmd);
      }
    }

    Float accuracy;
    int numSug;
    try {
      accuracy = p.getFloat("accuracy", DEFAULT_ACCURACY);
      spellChecker.setAccuracy(accuracy);
    } catch (NumberFormatException e) {
      throw new RuntimeException("Accuracy must be a valid positive float", e);
    }
    try {
      numSug = p.getInt("suggestionCount", DEFAULT_NUM_SUGGESTIONS);
    } catch (NumberFormatException e) {
      throw new RuntimeException("Spelling suggestion count must be a valid positive integer", e);
    }

    if (null != words && !"".equals(words.trim())) {
View Full Code Here

TOP

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

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.