Java Code Examples for org.apache.solr.util.SolrPluginUtils#parseFieldBoosts()

The following examples show how to use org.apache.solr.util.SolrPluginUtils#parseFieldBoosts() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendedDismaxQParser.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts all the aliased fields from the requests and adds them to up
 */
private void addAliasesFromRequest(ExtendedSolrQueryParser up, float tiebreaker) {
  Iterator<String> it = config.solrParams.getParameterNamesIterator();
  while(it.hasNext()) {
    String param = it.next();
    if(param.startsWith("f.") && param.endsWith(".qf")) {
      // Add the alias
      String fname = param.substring(2,param.length()-3);
      String qfReplacement = config.solrParams.get(param);
      Map<String,Float> parsedQf = SolrPluginUtils.parseFieldBoosts(qfReplacement);
      if(parsedQf.size() == 0)
        return;
      up.addAlias(fname, tiebreaker, parsedQf);
    }
  }
}
 
Example 2
Source File: DisMaxQParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Uses {@link SolrPluginUtils#parseFieldBoosts(String)} with the 'qf' parameter. Falls back to the 'df' parameter
 */
public static Map<String, Float> parseQueryFields(final IndexSchema indexSchema, final SolrParams solrParams)
    throws SyntaxError {
  Map<String, Float> queryFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams(DisMaxParams.QF));
  if (queryFields.isEmpty()) {
    String df = solrParams.get(CommonParams.DF);
    if (df == null) {
      throw new SyntaxError("Neither "+DisMaxParams.QF+" nor "+CommonParams.DF +" are present.");
    }
    queryFields.put(df, 1.0f);
  }
  return queryFields;
}
 
Example 3
Source File: DisMaxQParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Adds the main query to the query argument. If it's blank then false is returned. */
protected boolean addMainQuery(BooleanQuery.Builder query, SolrParams solrParams) throws SyntaxError {
  Map<String, Float> phraseFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams(DisMaxParams.PF));
  float tiebreaker = solrParams.getFloat(DisMaxParams.TIE, 0.0f);

  /* a parser for dealing with user input, which will convert
   * things to DisjunctionMaxQueries
   */
  SolrPluginUtils.DisjunctionMaxQueryParser up = getParser(queryFields, DisMaxParams.QS, solrParams, tiebreaker);

  /* for parsing sloppy phrases using DisjunctionMaxQueries */
  SolrPluginUtils.DisjunctionMaxQueryParser pp = getParser(phraseFields, DisMaxParams.PS, solrParams, tiebreaker);

  /* * * Main User Query * * */
  parsedUserQuery = null;
  String userQuery = getString();
  altUserQuery = null;
  if (StringUtils.isBlank(userQuery)) {
    // If no query is specified, we may have an alternate
    altUserQuery = getAlternateUserQuery(solrParams);
    if (altUserQuery == null)
      return false;
    query.add(altUserQuery, BooleanClause.Occur.MUST);
  } else {
    // There is a valid query string
    userQuery = SolrPluginUtils.partialEscape(SolrPluginUtils.stripUnbalancedQuotes(userQuery)).toString();
    userQuery = SolrPluginUtils.stripIllegalOperators(userQuery).toString();

    parsedUserQuery = getUserQuery(userQuery, up, solrParams);
    query.add(parsedUserQuery, BooleanClause.Occur.MUST);

    Query phrase = getPhraseQuery(userQuery, pp);
    if (null != phrase) {
      query.add(phrase, BooleanClause.Occur.SHOULD);
    }
  }
  return true;
}
 
Example 4
Source File: TestExtendedDismaxParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public MultilanguageDismaxConfiguration(SolrParams localParams,
    SolrParams params, SolrQueryRequest req) {
  super(localParams, params, req);
  String language = params.get("language");
  if(language != null) {
    super.queryFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams("qf_" + language)); 
  }
}
 
Example 5
Source File: SimpleQParserPlugin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public SimpleQParser (String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {

      super(qstr, localParams, params, req);
      // Some of the parameters may come in through localParams, so combine them with params.
      SolrParams defaultParams = SolrParams.wrapDefaults(localParams, params);

      // This will be used to specify what fields and boosts will be used by SimpleQueryParser.
      Map<String, Float> queryFields = SolrPluginUtils.parseFieldBoosts(defaultParams.get(SimpleParams.QF));

      if (queryFields.isEmpty()) {
        // It qf is not specified setup up the queryFields map to use the defaultField.
        String defaultField = defaultParams.get(CommonParams.DF);

        if (defaultField == null) {
          // A query cannot be run without having a field or set of fields to run against.
          throw new IllegalStateException("Neither " + SimpleParams.QF + " nor " + CommonParams.DF
              + " are present.");
        }

        queryFields.put(defaultField, 1.0F);
      }
      else {
        for (Map.Entry<String, Float> queryField : queryFields.entrySet()) {
          if (queryField.getValue() == null) {
            // Some fields may be specified without a boost, so default the boost to 1.0 since a null value
            // will not be accepted by SimpleQueryParser.
            queryField.setValue(1.0F);
          }
        }
      }

      // Setup the operations that are enabled for the query.
      int enabledOps = 0;
      String opParam = defaultParams.get(SimpleParams.QO);

      if (opParam == null) {
        // All operations will be enabled.
        enabledOps = -1;
      } else {
        // Parse the specified enabled operations to be used by the query.
        String[] operations = opParam.split(",");

        for (String operation : operations) {
          Integer enabledOp = OPERATORS.get(operation.trim().toUpperCase(Locale.ROOT));

          if (enabledOp != null) {
            enabledOps |= enabledOp;
          }
        }
      }

      // Create a SimpleQueryParser using the analyzer from the schema.
      final IndexSchema schema = req.getSchema();
      parser = new SolrSimpleQueryParser(req.getSchema().getQueryAnalyzer(), queryFields, enabledOps, this, schema);

      // Set the default operator to be either 'AND' or 'OR' for the query.
      QueryParser.Operator defaultOp = QueryParsing.parseOP(defaultParams.get(QueryParsing.OP));

      if (defaultOp == QueryParser.Operator.AND) {
        parser.setDefaultOperator(BooleanClause.Occur.MUST);
      }
    }
 
Example 6
Source File: MoreLikeThisHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public MoreLikeThisHelper( SolrParams params, SolrIndexSearcher searcher )
{
  this.searcher = searcher;
  this.reader = searcher.getIndexReader();
  this.uniqueKeyField = searcher.getSchema().getUniqueKeyField();
  this.needDocSet = params.getBool(FacetParams.FACET,false);
  
  SolrParams required = params.required();
  String[] fl = required.getParams(MoreLikeThisParams.SIMILARITY_FIELDS);
  List<String> list = new ArrayList<>();
  for (String f : fl) {
    if (!StringUtils.isEmpty(f))  {
      String[] strings = splitList.split(f);
      for (String string : strings) {
        if (!StringUtils.isEmpty(string)) {
          list.add(string);
        }
      }
    }
  }
  String[] fields = list.toArray(new String[list.size()]);
  if( fields.length < 1 ) {
    throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, 
        "MoreLikeThis requires at least one similarity field: "+MoreLikeThisParams.SIMILARITY_FIELDS );
  }
  
  this.mlt = new MoreLikeThis( reader ); // TODO -- after LUCENE-896, we can use , searcher.getSimilarity() );
  mlt.setFieldNames(fields);
  mlt.setAnalyzer( searcher.getSchema().getIndexAnalyzer() );
  
  // configurable params
  
  mlt.setMinTermFreq(       params.getInt(MoreLikeThisParams.MIN_TERM_FREQ,         MoreLikeThis.DEFAULT_MIN_TERM_FREQ));
  mlt.setMinDocFreq(        params.getInt(MoreLikeThisParams.MIN_DOC_FREQ,          MoreLikeThis.DEFAULT_MIN_DOC_FREQ));
  mlt.setMaxDocFreq(        params.getInt(MoreLikeThisParams.MAX_DOC_FREQ,          MoreLikeThis.DEFAULT_MAX_DOC_FREQ));
  mlt.setMinWordLen(        params.getInt(MoreLikeThisParams.MIN_WORD_LEN,          MoreLikeThis.DEFAULT_MIN_WORD_LENGTH));
  mlt.setMaxWordLen(        params.getInt(MoreLikeThisParams.MAX_WORD_LEN,          MoreLikeThis.DEFAULT_MAX_WORD_LENGTH));
  mlt.setMaxQueryTerms(     params.getInt(MoreLikeThisParams.MAX_QUERY_TERMS,       MoreLikeThis.DEFAULT_MAX_QUERY_TERMS));
  mlt.setMaxNumTokensParsed(params.getInt(MoreLikeThisParams.MAX_NUM_TOKENS_PARSED, MoreLikeThis.DEFAULT_MAX_NUM_TOKENS_PARSED));
  mlt.setBoost(            params.getBool(MoreLikeThisParams.BOOST, false ) );
  
  // There is no default for maxDocFreqPct. Also, it's a bit oddly expressed as an integer value 
  // (percentage of the collection's documents count). We keep Lucene's convention here. 
  if (params.getInt(MoreLikeThisParams.MAX_DOC_FREQ_PCT) != null) {
    mlt.setMaxDocFreqPct(params.getInt(MoreLikeThisParams.MAX_DOC_FREQ_PCT));
  }

  boostFields = SolrPluginUtils.parseFieldBoosts(params.getParams(MoreLikeThisParams.QF));
}
 
Example 7
Source File: DismaxSearchEngineRequestAdapter.java    From querqy with Apache License 2.0 4 votes vote down vote up
@Override
public List<Query> getAdditiveBoosts(final QuerqyQuery<?> userQuery) throws SyntaxException {

    final List<Query> boostQueries = parseQueriesFromParam(BQ, null);

    final List<PhraseBoostFieldParams> phraseBoostFieldParams = getPhraseBoostFieldParams();
    final Optional<Query> phraseBoostQuery =
            (!phraseBoostFieldParams.isEmpty())
                    ? makePhraseFieldsBoostQuery(userQuery, phraseBoostFieldParams, getPhraseBoostTiebreaker(),
                        getQueryAnalyzer())
                    : Optional.empty();


    final String[] bfs = getRequestParams(BF);

    final List<Query> boosts = new ArrayList<>(boostQueries.size()
            + bfs.length
            + (phraseBoostQuery.isPresent() ? 1 : 0));

    boosts.addAll(boostQueries);
    phraseBoostQuery.ifPresent(boosts::add);

    for (final String bf : bfs) {

        if (bf != null && bf.trim().length() > 0) {

            final Map<String, Float> ff = SolrPluginUtils.parseFieldBoosts(bf);
            for (final Map.Entry<String, Float> bfAndBoost : ff.entrySet()) {

                try {
                    final Query fq = qParser.subQuery(bfAndBoost.getKey(), FunctionQParserPlugin.NAME).getQuery();
                    final Float b = bfAndBoost.getValue();
                    if (null != b && b != 1f) {
                        boosts.add(new BoostQuery(fq, b));
                    } else {
                        boosts.add(fq);
                    }
                } catch (final SyntaxError syntaxError) {
                    throw new SyntaxException(syntaxError);
                }

            }
        }
    }

    return boosts;
}