Example usage for org.apache.solr.common.params SolrParams wrapAppended

List of usage examples for org.apache.solr.common.params SolrParams wrapAppended

Introduction

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

Prototype

public static SolrParams wrapAppended(SolrParams params, SolrParams defaults) 

Source Link

Usage

From source file:com.adr.bigdata.search.product.fe.BaseSuggestionHandler.java

@Override
public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
    String cacheKey = SolrParamUtils.transform(req.getParams());
    try {/*  ww  w. j av  a2 s  . com*/
        String cacheResponse = this.rdModel.get(cacheKey);
        if (cacheResponse == null) {
            //cacheMiss
            try {
                final Object[] terms = new Object[1];
                suggestionLogic.execute(req, rsp, new Callable() {
                    @Override
                    public void call(Object... args) {
                        terms[0] = args[0];
                    }
                });
                SolrQueryResponse _rsp = new SolrQueryResponse();
                super.handleRequest(req, _rsp);
                suggestionLogic.writeRsp(req, _rsp, terms[0]);
                String result = outerString(req, _rsp);
                if (!zeroResult(_rsp)) {
                    this.rdModel.put(cacheKey, result);
                }
                ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
                params.set("vincache", true);
                SolrParams _params = SolrParams.wrapDefaults(params, defaults);
                _params = SolrParams.wrapAppended(_params, appends);
                req.setParams(_params);
                rsp.add("vincache", result);
            } catch (Exception e) {
                error(req, rsp);
                getLogger().error("", e);
            }

        } else {
            //cache hit
            ModifiableSolrParams solrParam = new ModifiableSolrParams(req.getParams());
            solrParam.set("vincache", true);
            SolrParams params = SolrParams.wrapAppended(solrParam, appends);
            params = SolrParams.wrapDefaults(params, defaults);
            req.setParams(params);
            rsp.add("vincache", cacheResponse);
        }

    } catch (Exception cacheEx) {
        getLogger().error("fail to get from redis cache.....{}", cacheEx.getMessage());
        try {
            final Object[] terms = new Object[1];

            suggestionLogic.execute(req, rsp, new Callable() {
                @Override
                public void call(Object... args) {
                    terms[0] = args[0];
                }
            });
            super.handleRequest(req, rsp);
            suggestionLogic.writeRsp(req, rsp, terms[0]);
        } catch (Exception e) {
            error(req, rsp);
            getLogger().error("", e);
        }
    }
}

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

License:Apache License

/**
 * Modify the query parameters//w w  w  .ja  v  a  2  s. co  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;
}