Example usage for org.apache.solr.common.params DisMaxParams ALTQ

List of usage examples for org.apache.solr.common.params DisMaxParams ALTQ

Introduction

In this page you can find the example usage for org.apache.solr.common.params DisMaxParams ALTQ.

Prototype

String ALTQ

To view the source code for org.apache.solr.common.params DisMaxParams ALTQ.

Click Source Link

Document

Alternate query (expressed in Solr QuerySyntax) to use if main query (q) is empty

Usage

From source file:net.sr_sl.solr.ssq.SsqQueryComponent.java

License:Apache License

/**
 * Modify the query parameters//from w ww .j  av a  2s  .  c  o m
 */
private boolean modifyQueryRequest(ResponseBuilder rb) throws IOException {

    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();

    // check whether server side queries is active for this request
    if (!params.getBool(SSQ_PREFIX, false))
        return false;

    // get parameters to use
    String ssqQuery = params.get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY));
    String ssqParam = params.get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_PARAM), SSQ_PARAM_DFT);

    // when ssqQuery or ssqParam is not set, don't modify
    if (ssqQuery == null || ssqQuery.isEmpty() || ssqParam.isEmpty())
        return false;

    // Get original value for ssqParam and return when already set
    String ssqParamVal = params.get(ssqParam);
    if (ssqParamVal != null && !ssqParamVal.isEmpty())
        return false;

    // Get original query string value
    String origQueryString = rb.getQueryString();
    String origQVal = req.getOriginalParams().get(CommonParams.Q);

    // Retrieve value to use as query-term; when empty, use q.alt
    String qVal = origQVal;
    if (qVal == null || qVal.isEmpty()) {
        String alt_q = params.get(DisMaxParams.ALTQ);
        if (alt_q != null && !alt_q.isEmpty()) {
            qVal = alt_q;
        }
    }

    // Get value for ssqQuery
    String ssqQueryVal = params
            .get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY).concat(SSQ_DELIM).concat(ssqQuery));
    // When value not found, assume that ssqQuery is the query to execute
    // per default
    if (ssqQueryVal == null || ssqQueryVal.isEmpty())
        ssqQueryVal = ssqQuery;

    // Perform replacement
    ModifiableSolrParams mparams = new ModifiableSolrParams();

    // Set flag to indicate that replacement is performed
    mparams.set(SSQ_PREFIX.concat(SSQ_APPLIED_SUFFIX), Boolean.toString(true));

    // Store original querystring when <> q
    if (origQVal != null && !origQVal.equals(origQueryString))
        mparams.set(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERYSTRING).concat(SSQ_APPLIED_SUFFIX),
                origQueryString);

    // Perform the switch (qVal --> ssqParam)
    mparams.set(ssqParam, qVal);
    mparams.set(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY).concat(SSQ_APPLIED_SUFFIX), ssqQueryVal);

    // set the extra parameters
    req.setParams(SolrParams.wrapAppended(req.getParams(), mparams));

    // set queryString to query
    rb.setQueryString(ssqQueryVal);

    return true;
}